Skip to content

Commit

Permalink
refactor(api): rename, reorder, remove code
Browse files Browse the repository at this point in the history
Reorder code, rename variables, classes for clarity
(e.g. pluralise where multiples are expected), update
class references for consistency (imports, inheritance),
remove leftover function for retrieving Choices labels.
  • Loading branch information
koeaw committed Sep 10, 2024
1 parent 6b1a8be commit 7a0d4bb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 40 deletions.
36 changes: 10 additions & 26 deletions apis_ontology/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,16 @@
)


def get_choices_labels(values: list, text_choices):
"""
Return labels for enumeration type TextChoices.
:param values: an array of (valid) TextChoices values
:param text_choices: the TextChoices class against which to match the values
:return: a list of labels
"""
labels = []
class RelatedWorksDict(TypedDict):
id: int
title: str
subtitle: str | None
relation_type: str

for v in values:
labels.append(text_choices(v).label)

return labels
class NameAndIdSerializer(serializers.Serializer):
id = serializers.IntegerField()
name = serializers.CharField()


class PlaceDataSerializerMin(serializers.ModelSerializer):
Expand Down Expand Up @@ -90,13 +86,8 @@ class Meta:
]


class SimpleDetailSerializer(serializers.Serializer):
id = serializers.IntegerField()
name = serializers.CharField()


class ExpressionDataDetailSerializer(ExpressionDataSerializer):
publisher = SimpleDetailSerializer(required=False, allow_null=True)
publisher = NameAndIdSerializer(required=False, allow_null=True)
place_of_publication = PlaceDataSerializerMin(
required=False, allow_null=True, many=True
)
Expand All @@ -118,13 +109,6 @@ class Meta:
]


class RelatedWorksDict(TypedDict):
id: int
title: str
subtitle: str | None
relation_type: str


class CharacterDataSerializer(serializers.ModelSerializer):
fictionality = serializers.ListField(
child=serializers.CharField(allow_null=True), required=False, allow_empty=True
Expand Down Expand Up @@ -195,7 +179,7 @@ class WorkDetailSerializer(serializers.ModelSerializer):
)
related_works = serializers.SerializerMethodField()
characters = CharacterDataSerializer(
source="character_data",
source="related_characters",
required=False,
allow_empty=True,
many=True,
Expand Down
28 changes: 14 additions & 14 deletions apis_ontology/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from django.db.models import OuterRef, Q
from django.db.models.functions import JSONObject
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import pagination, permissions, viewsets
from rest_framework.mixins import RetrieveModelMixin
from rest_framework import mixins, pagination, permissions, viewsets

from apis_ontology.models import (
Archive,
Expand Down Expand Up @@ -215,7 +214,7 @@ def get_queryset(self):
triple_set_from_subj__prop__name_reverse__in=["has publisher"],
).values("name")

expression_place = Place.objects.filter(
expression_places = Place.objects.filter(
triple_set_from_obj__subj_id=OuterRef("pk"),
triple_set_from_obj__prop__name_forward__in=["is published in"],
).values_list("name")
Expand All @@ -226,7 +225,7 @@ def get_queryset(self):
triple_set_from_obj__prop__name_reverse__in=["realises"],
).annotate(
publisher=Subquery(expression_publisher[:1]),
place=ArraySubquery(expression_place),
places=ArraySubquery(expression_places),
)
).values(
json=JSONObject(
Expand All @@ -237,7 +236,7 @@ def get_queryset(self):
language="language",
publication_date="publication_date_iso_formatted",
publisher="publisher",
place_of_publication="place",
place_of_publication="places",
)
)

Expand All @@ -251,7 +250,7 @@ def get_queryset(self):
.values("language")
)

facet_topic = Topic.objects.filter(
facet_topics = Topic.objects.filter(
triple_set_from_obj__subj_id=OuterRef("pk"),
triple_set_from_obj__prop__name_forward__in=["is about topic"],
).values_list("name")
Expand All @@ -262,15 +261,15 @@ def get_queryset(self):
expression_data=ArraySubquery(related_expressions),
work_type=ArraySubquery(work_types),
facet_language=ArraySubquery(facet_languages),
facet_topic=ArraySubquery(facet_topic),
facet_topic=ArraySubquery(facet_topics),
)
.order_by("title", "subtitle")
)

return works


class WorkDetailViewSet(RetrieveModelMixin, viewsets.GenericViewSet):
class WorkDetailViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
"""API endpoint which returns full Work objects.
The full result set is meant to populate the detail view of the
Expand Down Expand Up @@ -326,7 +325,7 @@ def get_queryset(self):
)
)

expression_place = related_places.filter(
expression_places = related_places.filter(
triple_set_from_obj__subj_id=OuterRef("pk"),
triple_set_from_obj__prop__name_forward__in=["is published in"],
).distinct()
Expand All @@ -337,7 +336,7 @@ def get_queryset(self):
triple_set_from_obj__prop__name_reverse__in=["realises"],
).annotate(
publisher=Subquery(expression_publisher),
place=ArraySubquery(expression_place),
places=ArraySubquery(expression_places),
)
).values(
json=JSONObject(
Expand All @@ -348,7 +347,7 @@ def get_queryset(self):
language="language",
publication_date="publication_date_iso_formatted",
publisher="publisher",
place_of_publication="place",
place_of_publication="places",
)
)

Expand All @@ -366,9 +365,10 @@ def get_queryset(self):
)
)

related_places_2 = related_places.filter(
work_places = related_places.filter(
triple_set_from_obj__subj_id=OuterRef("pk"),
).distinct()

related_archive = Archive.objects.filter(
triple_set_from_subj__obj_id=OuterRef("pk"),
triple_set_from_subj__prop__name_forward__in=["holds"],
Expand Down Expand Up @@ -462,13 +462,13 @@ def get_queryset(self):
.annotate(
expression_data=ArraySubquery(related_expressions),
work_type=ArraySubquery(work_types),
character_data=ArraySubquery(related_characters),
related_characters=ArraySubquery(related_characters),
related_physical_objects=ArraySubquery(related_physical_objects),
related_topics=ArraySubquery(topics),
related_persons=ArraySubquery(related_persons),
related_places=ArraySubquery(work_places),
forward_work_relations=ArraySubquery(forward_work_relations),
reverse_work_relations=ArraySubquery(reverse_work_relations),
related_places=ArraySubquery(related_places_2),
)
.order_by("title", "subtitle")
)
Expand Down

0 comments on commit 7a0d4bb

Please sign in to comment.