Skip to content

Commit

Permalink
feat(api_views): add related entities to api facet output
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed May 29, 2024
1 parent 78ea65e commit b78130d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
26 changes: 22 additions & 4 deletions apis_ontology/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,34 @@ class SicprodModelViewSet(ModelViewSet):
We override the generic ModelViewSet so we can add the facet
information to the -list output
"""
facet_attributes = ["gender", "type"]
facet_attributes = ["gender", "type", "related_persons", "related_functions", "related_places", "related_institutions", "related_events", "related_salaries"]

def generate_facet_data(self):
facets = {}
for el in self.filter_queryset(self.get_queryset()):
for attribute in self.facet_attributes:
val = getattr(el, attribute, None)
if val is not None:
values = getattr(el, attribute, [])
if isinstance(values, str):
values = [values]
if values == None:
values = []

for value in values:
facets[attribute] = facets.get(attribute, {})
facets[attribute][val] = facets[attribute].get(val, 0) + 1
match value:
case dict():
facetdict = facets[attribute].get(value["id"], {"name": value["name"], "count": 0 })
facetdict["count"] += 1
facets[attribute][value["id"]] = facetdict
case str():
id = value or "emtpy"
facetdict = facets[attribute].get(id, {"name": value, "count": 0})
facetdict["count"] += 1
facets[attribute][id] = facetdict
case None:
pass
case other:
print(f"Unusable value for facetlist: {other}")
return {"facets": facets}

def list(self, request, *args, **kwargs):
Expand Down
24 changes: 24 additions & 0 deletions apis_ontology/querysets.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
from django.contrib.postgres.expressions import ArraySubquery
from django.db.models import OuterRef, Value
from django.db.models.functions import JSONObject, Concat
from apis_ontology.models import Person, Function, Place, Institution, Event, Salary


def SalaryViewSetQueryset(queryset):
return queryset.filter(typ__in=["Sold", "Provision", "Sonstiges"])


def LegacyStuffMixinViewSetQueryset(queryset):
space = Value(" ")
person_subquery = Person.objects.filter(triple_set_from_obj__subj_id=OuterRef("pk")).values(json=JSONObject(name=Concat("first_name", space, "name"), id="id"))
function_subquery = Function.objects.filter(triple_set_from_obj__subj_id=OuterRef("pk")).values(json=JSONObject(name="name", id="id"))
place_subquery = Place.objects.filter(triple_set_from_obj__subj_id=OuterRef("pk")).values(json=JSONObject(name="name", id="id"))
institution_subquery = Institution.objects.filter(triple_set_from_obj__subj_id=OuterRef("pk")).values(json=JSONObject(name="name", id="id"))
event_subquery = Event.objects.filter(triple_set_from_obj__subj_id=OuterRef("pk")).values(json=JSONObject(name="name", id="id"))
salary_subquery = Salary.objects.filter(triple_set_from_obj__subj_id=OuterRef("pk")).values(json=JSONObject(name="name", id="id"))
return queryset.annotate(
related_persons=ArraySubquery(person_subquery),
related_functions=ArraySubquery(function_subquery),
related_places=ArraySubquery(place_subquery),
related_institutions=ArraySubquery(institution_subquery),
related_events=ArraySubquery(event_subquery),
related_salaries=ArraySubquery(salary_subquery),
)

0 comments on commit b78130d

Please sign in to comment.