Skip to content

Commit

Permalink
Exclude deleted submissions when updating instances with geoms
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <[email protected]>
  • Loading branch information
KipSigei committed Nov 21, 2022
1 parent e033ef1 commit 9d36be5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
25 changes: 22 additions & 3 deletions onadata/apps/api/tests/viewsets/test_data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2112,7 +2112,7 @@ def test_geojson_format(self):
self.assertEqual(response.data, data)

def test_instances_with_geopoints(self):
# publist sample geo submissions
# publish sample geo submissions
self._publish_submit_geojson()

view = DataViewSet.as_view({"get": "list"})
Expand All @@ -2128,14 +2128,33 @@ def test_instances_with_geopoints(self):
self.assertTrue(self.xform.instances_with_geopoints)

def test_instances_with_empty_geopoints(self):
# publist sample geo submissions
# publish sample geo submissions
self._publish_submit_geojson(has_empty_geoms=True)

view = DataViewSet.as_view({"get": "list"})
view = DataViewSet.as_view({"delete": "destroy", "get": "list"})
request = self.factory.get("/", **self.extra)
response = view(request, pk=self.xform.pk, format="geojson")

# should return 200 if it has atleast one valid geom
self.assertEqual(response.status_code, 200)
self.assertEqual(self.xform.instances.count(), 2)

# check if instances_with_geopoints is False for the form
self.xform.refresh_from_db()
self.assertTrue(self.xform.instances_with_geopoints)

# soft delete instance with geoms
dataid = self.xform.instances.all().order_by("id")[0].pk
request = self.factory.delete("/", **self.extra)
response = view(request, pk=self.xform.pk, dataid=dataid)

# get the soft deleted instance
first_xform_instance = self.xform.instances.get(pk=dataid)
self.assertEqual(first_xform_instance.deleted_by, request.user)

# return 404 if all instances dont have geoms
request = self.factory.get("/", **self.extra)
response = view(request, pk=self.xform.pk, format="geojson")
self.assertEqual(response.status_code, 404)
self.assertEqual(self.xform.instances.count(), 2)

Expand Down
11 changes: 11 additions & 0 deletions onadata/apps/api/viewsets/data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,17 @@ def destroy(self, request, *args, **kwargs):
if request.user.has_perm(CAN_DELETE_SUBMISSION, self.object.xform):
instance_id = self.object.pk
delete_instance(self.object, request.user)

# update xform if no instance has geoms
if (
self.object.xform.instances.filter(
deleted_at__isnull=True, geom=None
).count()
< 1
):
self.object.xform.instances_with_geopoints = False
self.object.xform.save()

# send message
send_message(
instance_id=instance_id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
today,location,_location_latitude,_location_longitude,_location_altitude,_location_precision,path,shape,meta/instanceID,_uuid,_submission_time,_tags,_notes,_version,_duration,_submitted_by
2015-01-15,n/a,n/a,n/a,n/a,n/a,-1.294234 36.78722 0 30;-1.292286 36.787152 0 0;-1.295182 36.800439 0 0;-1.295289 36.802215 0 0;-1.286708 36.817235 0 0,-1.2942 36.78723 0 22;-1.292415 36.787356 0 0;-1.292586 36.779084 0 0;-1.29988 36.779546 0 0;-1.299666 36.788096 0 0;-1.2942 36.78723 0 22,uuid:7c0d8b2d-7ad0-4100-923d-f2767d532258,7c0d8b2d-7ad0-4100-923d-f2767d532258,2015-01-15T06:19:23,,,201501150616,,
2015-01-15,-1.294197 36.787219 0 34,-1.294197,36.787219,0,34,-1.294234 36.78722 0 30;-1.292286 36.787152 0 0;-1.295182 36.800439 0 0;-1.295289 36.802215 0 0;-1.286708 36.817235 0 0,-1.2942 36.78723 0 22;-1.292415 36.787356 0 0;-1.292586 36.779084 0 0;-1.29988 36.779546 0 0;-1.299666 36.788096 0 0;-1.2942 36.78723 0 22,uuid:7c0d8b2d-7ad0-4100-923d-f2767d532258,7c0d8b2d-7ad0-4100-923d-f2767d532258,2015-01-15T06:19:23,,,201501150616,,
2015-01-15,n/a,n/a,n/a,n/a,n/a,-1.294234 36.78722 0 30;-1.292286 36.787152 0 0;-1.295182 36.800439 0 0;-1.295289 36.802215 0 0;-1.286708 36.817235 0 0,-1.2942 36.78723 0 22;-1.292415 36.787356 0 0;-1.292586 36.779084 0 0;-1.29988 36.779546 0 0;-1.299666 36.788096 0 0;-1.2942 36.78723 0 22,uuid:7c0d8b2d-7ad0-4100-923d-f2767d532259,7c0d8b2d-7ad0-4100-923d-f2767d532259,2015-01-15T06:19:23,,,201501150616,,

0 comments on commit 9d36be5

Please sign in to comment.