Skip to content

Commit 9eae6a3

Browse files
committed
Add delete-tasks endpoint
1 parent 43e7227 commit 9eae6a3

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

isic/studies/api.py

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ class StudyViewSet(viewsets.ReadOnlyModelViewSet):
3434

3535
swagger_schema = None
3636

37+
@action(detail=True, methods=['delete'], pagination_class=None, url_path='delete-tasks')
38+
def delete_tasks(self, request, *args, **kwargs):
39+
study: Study = self.get_object()
40+
if not request.user.has_perm('studies.modify_study', study):
41+
raise PermissionDenied
42+
elif study.tasks.filter(annotation__isnull=False).exists():
43+
raise Conflict('Study has answered questions, tasks cannot be deleted.')
44+
else:
45+
# TODO: this will timeout for larger studies
46+
study.tasks.all().delete()
47+
return JsonResponse({})
48+
3749
@action(detail=True, methods=['post'], pagination_class=None, url_path='set-tasks')
3850
def set_tasks(self, request, *args, **kwargs):
3951
study: Study = self.get_object()

isic/studies/tests/test_permissions.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -307,19 +307,21 @@ def test_study_api_list_owners_permissions(api_client, study_scenario, study_fac
307307

308308

309309
@pytest.mark.django_db
310-
def test_study_api_set_tasks_on_study_with_responses_permissions(
311-
api_client, private_study_with_responses
310+
@pytest.mark.parametrize('method,path', [['post', 'set-tasks'], ['delete', 'delete-tasks']])
311+
def test_study_api_modify_tasks_on_study_with_responses_permissions(
312+
api_client, private_study_with_responses, method, path
312313
):
313314
study = private_study_with_responses[0]
314315
api_client.force_login(study.creator)
315-
r = api_client.post(f'/api/v2/studies/{study.pk}/set-tasks/')
316+
r = api_client.generic(method, f'/api/v2/studies/{study.pk}/{path}/')
316317
assert r.status_code == 409
317318

318319

319320
@pytest.mark.django_db
320-
def test_study_api_set_tasks_on_someone_elses_study_permissions(
321-
api_client, user_factory, public_study
321+
@pytest.mark.parametrize('method,path', [['post', 'set-tasks'], ['delete', 'delete-tasks']])
322+
def test_study_api_modify_tasks_on_someone_elses_study_permissions(
323+
api_client, user_factory, public_study, method, path
322324
):
323325
api_client.force_login(user_factory())
324-
r = api_client.post(f'/api/v2/studies/{public_study.pk}/set-tasks/')
326+
r = api_client.generic(method, f'/api/v2/studies/{public_study.pk}/{path}/')
325327
assert r.status_code == 403

0 commit comments

Comments
 (0)