Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
feat: add endpoint to remove all project data
Browse files Browse the repository at this point in the history
  • Loading branch information
obdulia-losantos committed Aug 16, 2021
1 parent a659369 commit 7e957dd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
17 changes: 17 additions & 0 deletions aether-kernel/aether/kernel/api/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,23 @@ def test_schema_validate_definition__errors(self):
)
self.assertEqual(response.status_code, 400)

def test_project_delete_data(self):
self.assertEqual(reverse('project-erase_data', kwargs={'pk': 1}),
'/projects/1/delete-data/')
url = reverse('project-erase_data', kwargs={'pk': self.project.pk})

submissions = models.Submission.objects.filter(mappingset__project=self.project)
entities = models.Entity.objects.filter(project=self.project)

self.assertGreater(submissions.count(), 0)
self.assertGreater(entities.count(), 0)

response = self.client.patch(url)
self.assertEqual(response.status_code, 204)

self.assertEqual(submissions.count(), 0)
self.assertEqual(entities.count(), 0)

def test_project__schemas_skeleton(self):
self.assertEqual(reverse('project-skeleton', kwargs={'pk': 1}),
'/projects/1/schemas-skeleton/')
Expand Down
13 changes: 13 additions & 0 deletions aether-kernel/aether/kernel/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ class ProjectViewSet(MtViewSetMixin, FilteredMixin, ExtractMixin, viewsets.Model
filter_class = filters.ProjectFilter
search_fields = ('name',)

@action(detail=True, methods=['patch'], url_name='erase_data', url_path='delete-data')
def delete_data(self, request, pk=None, *args, **kwargs):
instance = self.get_object_or_404(pk=pk)
try:
instance.submissions.all().delete()
instance.entities.all().delete()
return Response(status=status.HTTP_204_NO_CONTENT)
except Exception as e: # pragma: no cover
return Response(
str(e),
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)

@action(detail=True, methods=['get'], url_name='skeleton', url_path='schemas-skeleton')
def schemas_skeleton(self, request, pk=None, *args, **kwargs):
'''
Expand Down

0 comments on commit 7e957dd

Please sign in to comment.