Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update project updated_time (#3810) #3814

Merged
merged 6 commits into from
Nov 2, 2021
Merged

Update project updated_time (#3810) #3814

merged 6 commits into from
Nov 2, 2021

Conversation

jaesuny
Copy link
Contributor

@jaesuny jaesuny commented Oct 20, 2021

Motivation and context

Resolved #3810

Update db_project.updated_date when a task in the project is created, deleted or moved.

How has this been tested?

Manually tested.

Checklist

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.
  • I have updated the license header for each file (see an example below)
# Copyright (C) 2021 Intel Corporation
#
# SPDX-License-Identifier: MIT

Update project.updated_date
when included tasks are deleted, created or moved
ActiveChooN
ActiveChooN previously approved these changes Oct 25, 2021
Copy link
Contributor

@ActiveChooN ActiveChooN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for your contribution!

@ActiveChooN
Copy link
Contributor

@dvkruchinin, hi, can you adjust our project tests to cover these changes?

@nmanovic
Copy link
Contributor

@jaesuny , thanks for the great contribution. Could you please add a line about the fix into CHANGELOG?

@@ -564,14 +564,36 @@ def retrieve(self, request, pk=None):
raise serializers.ValidationError(
"Unexpected action specified for the request")

def update(self, request, *args, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaesuny , do we really need to have the method? Can we move all logic into perform_update which is called from update?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

instance = serializer.save(owner=self.request.user)
if instance.project:
db_project = instance.project
db_project.updated_date = timezone.now()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to fix in models.py instead. After that the field will be updated each time when the model is saved.
updated_date = models.DateTimeField(auto_now=True)

https://docs.djangoproject.com/en/3.2/ref/models/fields/#django.db.models.DateField.auto_now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

@@ -564,14 +564,25 @@ def retrieve(self, request, pk=None):
raise serializers.ValidationError(
"Unexpected action specified for the request")

def perform_update(self, serializer):
instance = self.get_object()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Serializer should already have the instance. Please use it: serializer.instance.
https://www.django-rest-framework.org/api-guide/serializers/#accessing-the-initial-data-and-instance

It is critical, because self.get_object will call check_object_permissions which ins't good here (it is already called in update method).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. updated.

nmanovic
nmanovic previously approved these changes Oct 29, 2021
Copy link
Contributor

@nmanovic nmanovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaesuny , Thanks for your contribution! It is a great job!

@jaesuny
Copy link
Contributor Author

jaesuny commented Oct 29, 2021

@jaesuny , Thanks for your contribution! It is a great job!

I learned a lot thanks to you.
Thank you.

@nmanovic
Copy link
Contributor

@jaesuny , could you please merge develop (to fix most of problems with tests)? Also I see that there are some problems with Django tests. Looks like the PR breaks something.

======================================================================
ERROR: test_move_task (engine.tests.test_rest_api.TaskMoveAPITestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/django/cvat/apps/engine/tests/test_rest_api.py", line 1952, in test_move_task
    self._check_api_v1_tasks(self.task.id, data)
  File "/home/django/cvat/apps/engine/tests/test_rest_api.py", line 1931, in _check_api_v1_tasks
    response = self._run_api_v1_tasks_id(tid, data)
  File "/home/django/cvat/apps/engine/tests/test_rest_api.py", line 1915, in _run_api_v1_tasks_id
    response = self.client.patch('/api/v1/tasks/{}'.format(tid),
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/test.py", line 309, in patch
    response = super().patch(
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/test.py", line 215, in patch
    return self.generic('PATCH', path, data, content_type, **extra)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/test.py", line 231, in generic
    return super().generic(
  File "/opt/venv/lib/python3.8/site-packages/django/test/client.py", line 470, in generic
    return self.request(**r)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/test.py", line 283, in request
    return super().request(**kwargs)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/test.py", line 235, in request
    request = super().request(**kwargs)
  File "/opt/venv/lib/python3.8/site-packages/django/test/client.py", line 716, in request
    self.check_exception(response)
  File "/opt/venv/lib/python3.8/site-packages/django/test/client.py", line 577, in check_exception
    raise exc_value
  File "/opt/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/opt/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/viewsets.py", line 114, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
    raise exc
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/mixins.py", line 82, in partial_update
    return self.update(request, *args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/rest_framework/mixins.py", line 68, in update
    self.perform_update(serializer)
  File "/home/django/cvat/apps/engine/views.py", line 572, in perform_update
    Project.objects.get(id=project_id).save()
  File "/opt/venv/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/cacheops/query.py", line 352, in get
    return qs._no_monkey.get(qs, *args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/django/db/models/query.py", line 429, in get
    raise self.model.DoesNotExist(
cvat.apps.engine.models.Project.DoesNotExist: Project matching query does not exist.

----------------------------------------------------------------------
Ran 275 tests in 391.306s

FAILED (errors=1, skipped=30)
Destroying test database for alias 'default'...

Copy link
Contributor

@nmanovic nmanovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaesuny , thanks again. One test was failed, but it is an issue with our infra. We will address it soon. I will merge the PR now.

@nmanovic nmanovic merged commit edb3df0 into cvat-ai:develop Nov 2, 2021
@nmanovic
Copy link
Contributor

nmanovic commented Nov 2, 2021

@dvkruchinin , could you please prepare a test for the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

db_project.updated_date should be updated when the included task is deleted
3 participants