Skip to content

Commit

Permalink
Add xform to attachment model (#2587)
Browse files Browse the repository at this point in the history
* FA|KM|DU: Add xform to attachment model

Co-authored-by: Frankline Apiyo <[email protected]>
Co-authored-by: Kelvin Muchiri <[email protected]>

* Fix failing tests for attachment viewset

Co-authored-by: Kip <[email protected]>

* Return empty list on XFormFilter/attachments when no form selector has been specified.

* Add submitted user to Attachment model

* Handle form filter when a single record via pk is requested

* add migration to populate attachements xform

* fix typo

* fix typo

* refactor code for optimisation

* fix AttributeError: 'dict' object has no attribute 'pk'

* fix typo

---------

Co-authored-by: Frankline Apiyo <[email protected]>
Co-authored-by: Kelvin Muchiri <[email protected]>
Co-authored-by: Kip <[email protected]>
  • Loading branch information
4 people authored Apr 23, 2024
1 parent 64fd3c4 commit fc1a1ff
Show file tree
Hide file tree
Showing 18 changed files with 957 additions and 758 deletions.
2 changes: 1 addition & 1 deletion onadata/apps/api/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def has_object_permission(self, request, view, obj):
model_cls = XForm
user = request.user

return self._has_object_permission(request, model_cls, user, obj.instance.xform)
return self._has_object_permission(request, model_cls, user, obj.xform)


class ConnectViewsetPermissions(IsAuthenticated):
Expand Down
48 changes: 32 additions & 16 deletions onadata/apps/api/tests/viewsets/test_attachment_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,45 +89,56 @@ def test_attachment_pagination(self):
extension="JPG",
name=filename,
media_file=media_file,
xform=self.xform,
)

# not using pagination params
request = self.factory.get("/", **self.extra)
request = self.factory.get("/", data={"xform": self.xform.pk}, **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), 2)

# valid page and page_size
request = self.factory.get("/", data={"page": 1, "page_size": 1}, **self.extra)
request = self.factory.get(
"/", data={"xform": self.xform.pk, "page": 1, "page_size": 1}, **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), 1)

# invalid page type
request = self.factory.get("/", data={"page": "invalid"}, **self.extra)
request = self.factory.get(
"/", data={"xform": self.xform.pk, "page": "invalid"}, **self.extra
)
response = self.list_view(request)
self.assertEqual(response.status_code, 404)

# invalid page size type
request = self.factory.get("/", data={"page_size": "invalid"}, **self.extra)
request = self.factory.get(
"/", data={"xform": self.xform.pk, "page_size": "invalid"}, **self.extra
)
response = self.list_view(request)
self.assertEqual(response.status_code, 200)
self.assertTrue(isinstance(response.data, list))
self.assertEqual(len(response.data), 2)

# invalid page and page_size types
request = self.factory.get(
"/", data={"page": "invalid", "page_size": "invalid"}, **self.extra
"/",
data={"xform": self.xform.pk, "page": "invalid", "page_size": "invalid"},
**self.extra,
)
response = self.list_view(request)
self.assertEqual(response.status_code, 404)

# invalid page size
request = self.factory.get("/", data={"page": 4, "page_size": 1}, **self.extra)
request = self.factory.get(
"/", data={"xform": self.xform.pk, "page": 4, "page_size": 1}, **self.extra
)
response = self.list_view(request)
self.assertEqual(response.status_code, 404)

Expand Down Expand Up @@ -170,7 +181,7 @@ def test_retrieve_and_list_views_with_anonymous_user(self):
def test_list_view(self):
self._submit_transport_instance_w_attachment()

request = self.factory.get("/", **self.extra)
request = self.factory.get("/", data={"xform": self.xform.pk}, **self.extra)
response = self.list_view(request)
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertEqual(response.status_code, 200)
Expand All @@ -181,15 +192,15 @@ def test_list_view(self):
self.attachment.instance.deleted_at = timezone.now()
self.attachment.instance.save()

request = self.factory.get("/", **self.extra)
request = self.factory.get("/", data={"xform": self.xform.pk}, **self.extra)
response = self.list_view(request)
self.assertTrue(isinstance(response.data, list))
self.assertEqual(len(response.data), 0)

def test_data_list_with_xform_in_delete_async(self):
self._submit_transport_instance_w_attachment()

request = self.factory.get("/", **self.extra)
request = self.factory.get("/", data={"xform": self.xform.pk}, **self.extra)
response = self.list_view(request)
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertEqual(response.status_code, 200)
Expand All @@ -198,7 +209,7 @@ def test_data_list_with_xform_in_delete_async(self):

self.xform.deleted_at = timezone.now()
self.xform.save()
request = self.factory.get("/", **self.extra)
request = self.factory.get("/", data={"xform": self.xform.pk}, **self.extra)
response = self.list_view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), initial_count - 1)
Expand Down Expand Up @@ -276,6 +287,7 @@ def test_list_view_filter_by_attachment_type(self):
extension="MP4",
name=filename,
media_file=media_file,
xform=self.xform,
)

Attachment.objects.create(
Expand All @@ -284,42 +296,46 @@ def test_list_view_filter_by_attachment_type(self):
extension="PDF",
name=filename,
media_file=media_file,
xform=self.xform,
)
Attachment.objects.create(
instance=self.xform.instances.first(),
mimetype="text/plain",
extension="TXT",
name=filename,
media_file=media_file,
xform=self.xform,
)
Attachment.objects.create(
instance=self.xform.instances.first(),
mimetype="audio/mp3",
extension="MP3",
name=filename,
media_file=media_file,
xform=self.xform,
)
Attachment.objects.create(
instance=self.xform.instances.first(),
mimetype="application/geo+json",
extension="GEOJSON",
name=geojson_filename,
media_file=geojson_media_file,
xform=self.xform,
)
data = {}
data = {"xform": self.xform.pk}
request = self.factory.get("/", data, **self.extra)
response = self.list_view(request)
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertEqual(response.status_code, 200)
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertTrue(isinstance(response.data, list))
self.assertEqual(len(response.data), 6)

# Apply image Filter
data["type"] = "image"
request = self.factory.get("/", data, **self.extra)
response = self.list_view(request)
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertEqual(response.status_code, 200)
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertTrue(isinstance(response.data, list))
self.assertEqual(len(response.data), 1)
self.assertEqual(response.data[0]["mimetype"], "image/jpeg")
Expand All @@ -328,8 +344,8 @@ def test_list_view_filter_by_attachment_type(self):
data["type"] = "audio"
request = self.factory.get("/", data, **self.extra)
response = self.list_view(request)
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertEqual(response.status_code, 200)
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertTrue(isinstance(response.data, list))
self.assertEqual(len(response.data), 1)
self.assertEqual(response.data[0]["mimetype"], "audio/mp3")
Expand All @@ -338,8 +354,8 @@ def test_list_view_filter_by_attachment_type(self):
data["type"] = "video"
request = self.factory.get("/", data, **self.extra)
response = self.list_view(request)
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertEqual(response.status_code, 200)
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertTrue(isinstance(response.data, list))
self.assertEqual(len(response.data), 1)
self.assertEqual(response.data[0]["mimetype"], "video/mp4")
Expand All @@ -348,8 +364,8 @@ def test_list_view_filter_by_attachment_type(self):
data["type"] = "document"
request = self.factory.get("/", data, **self.extra)
response = self.list_view(request)
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertEqual(response.status_code, 200)
self.assertNotEqual(response.get("Cache-Control"), None)
self.assertTrue(isinstance(response.data, list))
self.assertEqual(len(response.data), 3)
self.assertEqual(response.data[0]["mimetype"], "application/pdf")
Expand Down
Loading

0 comments on commit fc1a1ff

Please sign in to comment.