diff --git a/onadata/apps/api/tests/viewsets/test_dataview_viewset.py b/onadata/apps/api/tests/viewsets/test_dataview_viewset.py index 9bb5835186..fa74b14e26 100644 --- a/onadata/apps/api/tests/viewsets/test_dataview_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_dataview_viewset.py @@ -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) @@ -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 = { diff --git a/onadata/apps/api/viewsets/dataview_viewset.py b/onadata/apps/api/viewsets/dataview_viewset.py index 1ee101c2e3..742c2c78d4 100644 --- a/onadata/apps/api/viewsets/dataview_viewset.py +++ b/onadata/apps/api/viewsets/dataview_viewset.py @@ -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" diff --git a/onadata/libs/serializers/dataview_serializer.py b/onadata/libs/serializers/dataview_serializer.py index 90f7e94262..bdf4c51219 100644 --- a/onadata/libs/serializers/dataview_serializer.py +++ b/onadata/libs/serializers/dataview_serializer.py @@ -133,8 +133,6 @@ class Meta: "has_hxl_support", "url", "date_created", - "deleted_at", - "deleted_by", ) validators = [ serializers.UniqueTogetherValidator(