Skip to content

Commit 81c1862

Browse files
Ken LippoldKen Lippold
authored andcommitted
Updated tag keys endpoint.
1 parent 05b9658 commit 81c1862

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

sta/services/thing.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from typing import Optional, Literal
33
from ninja.errors import HttpError
44
from django.contrib.auth import get_user_model
5+
from django.contrib.postgres.aggregates import ArrayAgg
6+
from django.db.models import F
57
from iam.services.utils import ServiceUtils
68
from sta.models import Thing, Location, Tag, Photo
79
from sta.schemas import ThingPostBody, ThingPatchBody, TagPostBody, TagDeleteBody, PhotoDeleteBody
@@ -107,7 +109,9 @@ def get_tag_keys(user: Optional[User], workspace_id: Optional[uuid.UUID], thing_
107109
if thing_id:
108110
queryset = queryset.filter(thing_id=thing_id)
109111

110-
return list(queryset.visible(user=user).values_list("key", flat=True).distinct())
112+
tags = queryset.visible(user=user).values("key").annotate(values=ArrayAgg(F("value"), distinct=True))
113+
114+
return {entry["key"]: entry["values"] for entry in tags}
111115

112116
def add_tag(self, user: User, uid: uuid.UUID, data: TagPostBody):
113117
thing = self.get_thing_for_action(user=user, uid=uid, action="edit")

sta/views/tag.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def get_tags(request: HydroServerHttpRequest, thing_id: Path[uuid.UUID]):
3636
"keys",
3737
auth=[session_auth, bearer_auth, anonymous_auth],
3838
response={
39-
200: list[str],
39+
200: dict[str, list[str]],
4040
401: str,
4141
}
4242
)

0 commit comments

Comments
 (0)