Skip to content

Commit

Permalink
Merge pull request #1015 from rdmorganiser/fix_progress_action
Browse files Browse the repository at this point in the history
Fix progress action if progress did not change
  • Loading branch information
jochenklar authored Jun 20, 2024
2 parents a0b05ed + eccc07d commit 673618b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
42 changes: 42 additions & 0 deletions rdmo/projects/tests/test_viewset_project_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 5 additions & 4 deletions rdmo/projects/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 673618b

Please sign in to comment.