diff --git a/rdmo/projects/tests/test_viewset_project_progress.py b/rdmo/projects/tests/test_viewset_project_progress.py index 6c39a52798..8b6af4bd9c 100644 --- a/rdmo/projects/tests/test_viewset_project_progress.py +++ b/rdmo/projects/tests/test_viewset_project_progress.py @@ -91,3 +91,45 @@ def test_progress_post(db, client, username, password, project_id): assert response.status_code == 404 else: assert response.status_code == 401 + + +def test_progress_post_changed(db, client): + client.login(username='owner', password='owner') + + project = Project.objects.get(id=1) + project.progress_count = progress_count = 0 + project.progress_total = progress_total = 0 + project.save() + project.refresh_from_db() + project_updated = project.updated + + url = reverse(urlnames['progress'], args=[1]) + response = client.post(url) + + project.refresh_from_db() + + assert response.status_code == 200 + assert project.updated > project_updated + assert project.progress_count > progress_count + assert project.progress_total > progress_total + + +def test_progress_post_unchanged(db, client): + client.login(username='owner', password='owner') + + project = Project.objects.get(id=1) + project.progress_count = progress_count = 58 # the progres in the fixture is not up-to-date + project.progress_total = progress_total = 81 + project.save() + project.refresh_from_db() + project_updated = project.updated + + url = reverse(urlnames['progress'], args=[1]) + response = client.post(url) + + project.refresh_from_db() + + assert response.status_code == 200 + assert project.progress_count == progress_count + assert project.progress_total == progress_total + assert project.updated == project_updated diff --git a/rdmo/projects/viewsets.py b/rdmo/projects/viewsets.py index 35558fc562..9a974895a5 100644 --- a/rdmo/projects/viewsets.py +++ b/rdmo/projects/viewsets.py @@ -213,11 +213,12 @@ def progress(self, request, pk=None): project = self.get_object() if request.method == 'POST' or project.progress_count is None or project.progress_total is None: - # compute the progress and store + # compute the progress, but store it only, if it has changed project.catalog.prefetch_elements() - project.progress_count, project.progress_total = compute_progress(project) - project.save() - + progress_count, progress_total = compute_progress(project) + if progress_count != project.progress_count or progress_total != project.progress_total: + project.progress_count, project.progress_total = progress_count, progress_total + project.save() try: ratio = project.progress_count / project.progress_total except ZeroDivisionError: