Skip to content

Commit

Permalink
Clean out Merged Datasets upon xform deletion. Clean out Form Media F…
Browse files Browse the repository at this point in the history
…iles upon form deletion. Add documentation on clean up of filtered datasets
  • Loading branch information
WinnyTroy committed Sep 6, 2021
1 parent 0cb710f commit da15874
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
12 changes: 0 additions & 12 deletions onadata/apps/api/tests/viewsets/test_merged_xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,18 +663,6 @@ def test_md_data_viewset_deleted_form(self):

# DataViewSet /data/[pk] endpoint, form_a deleted
form_a.soft_delete()
response = data_view(request, pk=merged_dataset['id'])
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 1)

fruit = [d['fruit'] for d in response.data]
expected_fruit = ['mango']
self.assertEqual(fruit, expected_fruit)

# DataViewSet /data/[pk]/[dataid] endpoint, form_a deleted
data_view = DataViewSet.as_view({
'get': 'retrieve',
})
response = data_view(request, pk=merged_dataset['id'], dataid=dataid)
self.assertEqual(response.status_code, 404)

Expand Down
30 changes: 30 additions & 0 deletions onadata/apps/api/tests/viewsets/test_metadata_viewset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from builtins import open
from datetime import datetime

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -192,6 +193,35 @@ def test_delete_metadata(self):
self.assertEqual(response.status_code, 204)
self.assertEqual(count, MetaData.objects.count())

def test_delete_xform_deletes_media_metadata(self):
self._add_test_metadata()
self.view = MetaDataViewSet.as_view({'get': 'list'})
data = {'xform': self.xform.pk}
request = self.factory.get('/', data, **self.extra)
response = self.view(request)
expected_metadata = {
'id': 1,
'xform': 1,
'data_value': 'screenshot.png',
'data_type': 'supporting_doc',
'data_file': 'http://localhost:8000/media/bob/docs/screenshot_Zm5KZ7j.png', # noqa
'data_file_type': 'image/png',
'media_url': 'http://localhost:8000/media/bob/docs/screenshot_Zm5KZ7j.png', # noqa
'file_hash': 'md5:09b9e5e3278ac2a43fce25681ffada85',
'url': 'http://testserver/api/v1/metadata/1',
'date_created': datetime.now()
}
self.assertEqual(response.status_code, 200)
self.assertEquals(response.data[0], expected_metadata)

# Soft delete xform
self.xform.soft_delete()
# Confirm that media files were deleted
request = self.factory.get('/', **self.extra)
response2 = self.view(request)
self.assertEqual(response2.status_code, 200)
self.assertEquals(response2.data, [])

def test_windows_csv_file_upload_to_metadata(self):
data_value = 'transportation.csv'
path = os.path.join(self.fixture_dir, data_value)
Expand Down
3 changes: 2 additions & 1 deletion onadata/apps/api/viewsets/metadata_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class MetaDataViewSet(AuthenticateHeaderMixin,

content_negotiation_class = MediaFileContentNegotiation
filter_backends = (filters.MetaDataFilter,)
queryset = MetaData.objects.select_related()
queryset = MetaData.objects.filter(
deleted_at__isnull=True).select_related()
permission_classes = (MetaDataObjectPermissions,)
renderer_classes = (
renderers.JSONRenderer,
Expand Down
7 changes: 7 additions & 0 deletions onadata/apps/logger/models/xform.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,8 +953,15 @@ def soft_delete(self, user=None):
update_fields.append('deleted_by')

self.save(update_fields=update_fields)
# Delete associated filtered datasets
for dataview in self.dataview_set.all():
dataview.soft_delete(user)
# Delete associated Merged-Datasets
for merged_dataset in self.mergedxform_ptr.all():
merged_dataset.soft_delete(user)
# Delete associated Form Media Files
for metadata in self.metadata_set.all():
metadata.soft_delete()

def submission_count(self, force_update=False):
if self.num_of_submissions == 0 or force_update:
Expand Down
11 changes: 11 additions & 0 deletions onadata/apps/main/models/meta_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import requests
from django.conf import settings
from django.utils import timezone
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -209,6 +210,16 @@ def _set_hash(self):

return ''

def soft_delete(self):
"""
Return the soft deletion timestamp
Mark the MetaData as soft deleted,
by updating the deleted_at field.
"""
soft_deletion_time = timezone.now()
self.deleted_at = soft_deletion_time
self.save()

@staticmethod
def public_link(content_object, data_value=None):
data_type = 'public_link'
Expand Down

0 comments on commit da15874

Please sign in to comment.