Skip to content

Commit

Permalink
fix: DEV-2372: Delete action doesn't decrease total annotations count…
Browse files Browse the repository at this point in the history
…er (HumanSignal#2354)

* fix: DEV-2372: Delete action doesn't decrease total annotations counter

* Update test_api_tasks.py

* Fix negative total annotations

* Update models.py
  • Loading branch information
KonstantinKorotaev authored May 16, 2022
1 parent 22cb394 commit 323578f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion label_studio/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ def update_tasks_counters(self, queryset):
new_total_predictions=total_predictions)

for task in queryset:
task.total_annotations = task.new_total_annotations - task.new_cancelled_annotations
task.total_annotations = task.new_total_annotations
task.cancelled_annotations = task.new_cancelled_annotations
task.total_predictions = task.new_total_predictions
objs.append(task)
Expand Down
6 changes: 3 additions & 3 deletions label_studio/tasks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,16 +607,16 @@ def remove_project_summary_annotations(sender, instance, **kwargs):
"""Remove annotation counters in project summary followed by deleting an annotation"""
instance.decrease_project_summary_counters()
if instance.was_cancelled:
instance.task.cancelled_annotations = instance.task.annotations.all().filter(was_cancelled=True).count()
instance.task.cancelled_annotations = instance.task.annotations.all().filter(was_cancelled=True).count() - 1
else:
instance.task.total_annotations = instance.task.annotations.all().filter(was_cancelled=False).count()
instance.task.total_annotations = instance.task.annotations.all().filter(was_cancelled=False).count() - 1
instance.task.save(update_fields=['total_annotations', 'cancelled_annotations'])


@receiver(pre_delete, sender=Prediction)
def remove_predictions_from_project(sender, instance, **kwargs):
"""Remove predictions counters"""
instance.task.total_predictions = instance.task.predictions.all().count()
instance.task.total_predictions = instance.task.predictions.all().count() - 1
instance.task.save(update_fields=['total_predictions'])

@receiver(post_save, sender=Prediction)
Expand Down
25 changes: 25 additions & 0 deletions label_studio/tests/data_manager/test_api_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ def test_views_tasks_api(business_client, project_id):
assert response_data["tasks"][0]["total_predictions"] == 1
assert "predictions_results" in response_data["tasks"][0]

num_anno1 = response_data["tasks"][0]['annotations'][0]['id']
num_anno2 = response_data["tasks"][0]['annotations'][1]['id']
num_pred = response_data["tasks"][0]['predictions'][0]['id']

# delete annotations and check counters

business_client.delete(f"/api/annotations/{num_anno1}")
business_client.delete(f"/api/annotations/{num_anno2}")

response = business_client.get(f"/api/tasks?fields=all&view={view_id}")
assert response.status_code == 200, response.content
response_data = response.json()
assert response_data["tasks"][0]["cancelled_annotations"] == 0
assert response_data["tasks"][0]["total_annotations"] == 0

# delete prediction and check counters
business_client.delete(f"/api/predictions/{num_pred}")

response = business_client.get(f"/api/tasks?fields=all&view={view_id}")
assert response.status_code == 200, response.content
response_data = response.json()
assert response_data["tasks"][0]["cancelled_annotations"] == 0
assert response_data["tasks"][0]["total_annotations"] == 0
assert response_data["tasks"][0]["total_predictions"] == 0


@pytest.mark.parametrize(
"tasks_count, annotations_count, predictions_count",
Expand Down

0 comments on commit 323578f

Please sign in to comment.