Skip to content

Commit a2622b1

Browse files
Ken LippoldKen Lippold
authored andcommitted
Updated SensorThings DELETE Observations endpoints.
1 parent 69e6881 commit a2622b1

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

sta/models/observation.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ def visible(self, user: Optional["User"]):
3434
datastream__thing__workspace__collaborators__role__permissions__permission_type__in=["*", "view"])
3535
)
3636

37+
def removable(self, user: Optional["User"]):
38+
if user is None:
39+
return self.none()
40+
elif user.account_type == "admin":
41+
return self
42+
else:
43+
return self.filter(
44+
Q(datastream__thing__workspace__owner=user) |
45+
Q(datastream__thing__workspace__collaborators__user=user,
46+
datastream__thing__workspace__collaborators__role__permissions__resource_type__in=[
47+
"*", "Observation"
48+
],
49+
datastream__thing__workspace__collaborators__role__permissions__permission_type__in=["*", "delete"])
50+
)
51+
3752

3853
class Observation(models.Model, PermissionChecker):
3954
pk = models.CompositePrimaryKey("datastream_id", "phenomenon_time", "id")

sta/services/observation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def get_observation_for_action(user: User, uid: uuid.UUID, action: Literal["view
2424
try:
2525
observation = Observation.objects.select_related(
2626
"datastream", "datastream__thing", "datastream__thing__workspace"
27-
).get(pk=uid)
27+
).get(id=uid)
2828
except Observation.DoesNotExist:
2929
raise HttpError(404, "Observation does not exist")
3030

sta/services/sensorthings/observation.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def create_observation(
127127
observation: ObservationPostBody
128128
) -> UUID:
129129
datastream = datastream_service.get_datastream_for_action(
130-
user=self.request.authenticated_user, uid=observation.datastream, action="view" # noqa
130+
user=self.request.authenticated_user, uid=observation.datastream.id, action="view" # noqa
131131
)
132132

133133
if not Observation.can_user_create(user=self.request.authenticated_user, # noqa
@@ -159,7 +159,7 @@ def create_observations(
159159

160160
for datastream_id, datastream_observations in observations.items():
161161
datastream = datastream_service.get_datastream_for_action(
162-
user=self.request.authenticated_user, uid=observation.datastream, action="view" # noqa
162+
user=self.request.authenticated_user, uid=datastream_id, action="view" # noqa
163163
)
164164

165165
if not Observation.can_user_create(user=self.request.authenticated_user, # noqa
@@ -214,15 +214,48 @@ def delete_observation(
214214
self,
215215
observation_id: str
216216
) -> None:
217-
pass
217+
218+
observation = observation_service.get_observation_for_action(
219+
user=self.request.authenticated_user, uid=observation_id, action="delete" # noqa
220+
)
221+
222+
datastream_id = observation.datastream.id
223+
observation.delete()
224+
225+
self.update_value_count(datastream_id=datastream_id)
218226

219227
def delete_observations(
220228
self,
221229
datastream_id: UUID,
222230
start_time: Optional[datetime] = None,
223231
end_time: Optional[datetime] = None
224232
) -> None:
225-
return None
233+
234+
datastream = datastream_service.get_datastream_for_action(
235+
user=self.request.authenticated_user, uid=datastream_id, action="view" # noqa
236+
)
237+
238+
observations_count = Observation.objects.filter(
239+
datastream=datastream,
240+
phenomenon_time__gte=start_time,
241+
phenomenon_time__lte=end_time
242+
).count()
243+
244+
observations = Observation.objects.filter(
245+
datastream_id=datastream_id,
246+
phenomenon_time__gte=start_time,
247+
phenomenon_time__lte=end_time
248+
).removable(user=self.request.authenticated_user) # noqa
249+
250+
if observations_count == 0:
251+
raise HttpError(400, "No observations to delete within the given range")
252+
253+
if observations_count != observations.count():
254+
raise HttpError(403, "You do not have permission to delete one or more of these observations")
255+
256+
observations.delete()
257+
258+
self.update_value_count(datastream_id=datastream.id)
226259

227260
@staticmethod
228261
def update_value_count(datastream_id: UUID) -> None:
@@ -231,9 +264,7 @@ def update_value_count(datastream_id: UUID) -> None:
231264
datastream_id=datastream_id
232265
)
233266

234-
datastream = Datastream.objects.get(
235-
datastream_id=datastream_id
236-
)
267+
datastream = Datastream.objects.get(pk=datastream_id)
237268

238269
datastream.value_count = int(observation_query.count())
239270
datastream.save()

0 commit comments

Comments
 (0)