Skip to content

Commit

Permalink
Merge pull request #2721 from onaio/fix-2720
Browse files Browse the repository at this point in the history
Customized deleted_by serializer field for Dataviews
  • Loading branch information
FrankApiyo authored Oct 23, 2024
2 parents 24469e5 + 4676786 commit 4c8cba7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
40 changes: 40 additions & 0 deletions onadata/apps/api/tests/viewsets/test_dataview_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,16 @@ def test_list_dataview(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 2)

# delete DataView and check that we don't get it in response
dataview = DataView.objects.get(name="My DataView2")
deleted_dataview_id = dataview.id
dataview.soft_delete(user=self.user)
response = view(request)

self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 1)
self.assertNotEqual(response.data[0]["dataviewid"], deleted_dataview_id)

anon_request = request = self.factory.get("/")
anon_response = view(anon_request)
self.assertEqual(anon_response.status_code, 401)
Expand All @@ -477,6 +487,36 @@ def test_get_dataview_no_perms(self):

self.assertEqual(response.status_code, 200)

def test_can_not_get_deleted_dataview(self):
data = {
"name": "Agriculture Dataview",
"xform": f"http://testserver/api/v1/forms/{self.xform.pk}",
"project": f"http://testserver/api/v1/projects/{self.project.pk}",
"columns": '["name", "age", "gender"]',
"query": '[{"column":"age","filter":">","value":"20"},'
'{"column":"age","filter":"<","value":"50"}]',
}

self._create_dataview(data=data)

view = DataViewViewSet.as_view(
{
"get": "retrieve",
}
)

request = self.factory.get("/", **self.extra)
response = view(request, pk=self.data_view.pk)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["dataviewid"], self.data_view.pk)

dataview = DataView.objects.get(id=response.data["dataviewid"])
dataview.soft_delete(user=self.user)

request = self.factory.get("/", **self.extra)
response = view(request, pk=self.data_view.pk)
self.assertEqual(response.status_code, 404)

# pylint: disable=invalid-name
def test_dataview_data_filter_integer(self):
data = {
Expand Down
2 changes: 1 addition & 1 deletion onadata/apps/api/viewsets/dataview_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class DataViewViewSet(
A simple ViewSet for viewing and editing DataViews.
"""

queryset = DataView.objects.select_related()
queryset = DataView.objects.filter(deleted_at__isnull=True).select_related()
serializer_class = DataViewSerializer
permission_classes = [DataViewViewsetPermissions]
lookup_field = "pk"
Expand Down
2 changes: 0 additions & 2 deletions onadata/libs/serializers/dataview_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ class Meta:
"has_hxl_support",
"url",
"date_created",
"deleted_at",
"deleted_by",
)
validators = [
serializers.UniqueTogetherValidator(
Expand Down

0 comments on commit 4c8cba7

Please sign in to comment.