Skip to content

Commit

Permalink
Add deleted_at Field to Attachments. Create soft_delete action that w…
Browse files Browse the repository at this point in the history
…ill populate deleted_at field for attachments. Fixes #1696
  • Loading branch information
WinnyTroy committed Nov 12, 2019
1 parent 8952fdb commit e00d772
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
49 changes: 49 additions & 0 deletions onadata/apps/api/tests/viewsets/test_attachment_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,55 @@ def test_list_view(self):
self.assertTrue(isinstance(response.data, list))
self.assertEqual(len(response.data), 0)

def test_list_view_surfaces_current_attachment(self):
self._submit_transport_instance_w_attachment()
filename = "1335783522564.JPG"
path = os.path.join(self.main_directory, 'fixtures', 'transportation',
'instances', self.surveys[0], filename)
media_file = django_file(path, 'video2', 'image/jpeg')

Attachment.objects.create(
instance=self.xform.instances.first(),
mimetype='video/mp4',
extension='MP4',
name=filename,
media_file=media_file)

Attachment.objects.create(
instance=self.xform.instances.first(),
mimetype='application/pdf',
extension='PDF',
name=filename,
media_file=media_file)
Attachment.objects.create(
instance=self.xform.instances.first(),
mimetype='text/plain',
extension='TXT',
name=filename,
media_file=media_file)
Attachment.objects.create(
instance=self.xform.instances.first(),
mimetype='audio/mp3',
extension='MP3',
name=filename,
media_file=media_file)

request = self.factory.get('/', **self.extra)
response = self.list_view(request)
self.assertNotEqual(response.get('Cache-Control'), None)
self.assertEqual(response.status_code, 200)
self.assertTrue(isinstance(response.data, list))
self.assertEqual(len(response.data), 5)

# test when the attachment is soft deleted
self.attachment.soft_delete()
self.attachment.save()

request = self.factory.get('/', **self.extra)
response = self.list_view(request)

self.assertEqual(len(response.data), 4)

def test_data_list_with_xform_in_delete_async(self):
self._submit_transport_instance_w_attachment()

Expand Down
4 changes: 3 additions & 1 deletion onadata/apps/api/viewsets/attachment_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ def list(self, request, *args, **kwargs):
if not xform.shared_data:
raise Http404(_("Not Found"))

self.object_list = self.filter_queryset(self.get_queryset())
request_data = self.filter_queryset(self.get_queryset())
self.object_list = self.object_list = [
a for a in request_data if a.deleted_at is None]
page = self.paginate_queryset(self.object_list)
if page is not None:
serializer = self.get_serializer(page, many=True)
Expand Down
12 changes: 12 additions & 0 deletions onadata/apps/logger/models/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from hashlib import md5

from django.db import models
from django.utils import timezone


def get_original_filename(filename):
Expand Down Expand Up @@ -86,3 +87,14 @@ def file_hash(self):
def filename(self):
if self.media_file:
return os.path.basename(self.media_file.name)

def soft_delete(self, user=None):
"""
Soft deletes an attachment by adding a deleted_at timestamp.
"""

soft_deletion_time = timezone.now()
self.deleted_at = soft_deletion_time
if user is not None:
self.deleted_by = user
self.save()
1 change: 1 addition & 0 deletions onadata/apps/logger/models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def _get_attachments_from_instance(instance):
attachment['mimetype'] = a.mimetype
attachment['filename'] = a.media_file.name
attachment['name'] = a.name
attachment['deleted_at'] = a.deleted_at
attachment['instance'] = a.instance.pk
attachment['xform'] = instance.xform.id
attachment['id'] = a.id
Expand Down
2 changes: 1 addition & 1 deletion onadata/libs/serializers/attachment_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AttachmentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
fields = ('url', 'filename', 'mimetype', 'field_xpath', 'id', 'xform',
'instance', 'download_url', 'small_download_url',
'medium_download_url')
'medium_download_url', 'deleted_at')
model = Attachment

@check_obj
Expand Down

0 comments on commit e00d772

Please sign in to comment.