Skip to content

Commit

Permalink
chore: rebuild additional serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
katharinawuensche committed Dec 3, 2023
1 parent 538ea65 commit b8efc1c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 8 deletions.
51 changes: 44 additions & 7 deletions apis_ontology/jelinek_api_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,21 @@ def get_self_contenttype(self, obj):
def create_serializer(model):
dict_meta = {
"model": model,
"exclude": [
"references",
"notes",
"review",
],
"depth": 3,
'exclude': ['vector_column_e1_set', 'vector_related_f10_set', 'vector_related_E40_set', 'vector_related_xml_note_set']
}
if model.__name__ == "Xml_File":
dict_meta["exclude"].append("file_content")
if model.__name__ in ["Chapter", "XMLNote", 'Keyword', 'Xml_Content_Dump', 'Xml_File']:
dict_meta["exclude"] = []
metaclass = type(
f"{model.__name__}MetaClass",
(),
dict_meta,
)
dict_class = {
"type": serializers.SerializerMethodField(method_name="add_type"),
"add_type": add_type,
# "type": serializers.SerializerMethodField(method_name="add_type"),
# "add_type": add_type,
"self_contenttype": serializers.SerializerMethodField(method_name="get_self_contenttype"),
"get_self_contenttype": get_self_contenttype,
"Meta": metaclass,
Expand Down Expand Up @@ -170,6 +168,32 @@ def add_related_entity(self, obj):
)
return serializer(obj.obj).data

class SimpleTripleSerializer(serializers.ModelSerializer):
prop = serializers.CharField(source="prop.name")
class Meta:
model = Triple
fields = "__all__"
depth= 1

def to_representation(self, instance):
ret = super().to_representation(instance)
return remove_null_empty_from_dict(ret)
class SimplePersonSerializer(serializers.ModelSerializer):
class Meta:
model = F10_Person
fields = ["id", "name", "surname", "forename", "entity_id", "self_contenttype"]
class PersonTripleSerializer(serializers.ModelSerializer):
prop = serializers.CharField(source="prop.name")
subj = SimplePersonSerializer(read_only=True)
# obj = serializers.SerializerMethodField(method_name='add_related_entity_from_obj')
class Meta:
model = Triple
fields = "__all__"
depth= 1
def to_representation(self, instance):
ret = super().to_representation(instance)
return remove_null_empty_from_dict(ret)

class IncludeImageSerializer(serializers.ModelSerializer):
image = serializers.SerializerMethodField()
image_for_translation = serializers.SerializerMethodField()
Expand Down Expand Up @@ -469,3 +493,16 @@ class Meta:
depth=1
def get_details_url(self, obj):
return "https://apis-jelinek.acdh-dev.oeaw.ac.at/apis/entities/entity/e1_crm_entity/{}/detail/".format(obj.id)

class NoteSerializer(serializers.ModelSerializer):
triple_set_from_obj = SimpleTripleSerializer(many=True, read_only=True)

class Meta:
model = XMLNote
fields = "__all__"
depth = 1

def to_representation(self, instance):
ret = super().to_representation(instance)
return remove_null_empty_from_dict(ret)

67 changes: 66 additions & 1 deletion apis_ontology/jelinek_api_views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ast import literal_eval
from rest_framework.response import Response
from rest_framework import viewsets
from .models import *
Expand All @@ -12,12 +13,59 @@
from django.contrib.postgres.expressions import ArraySubquery
from apis_core.apis_relations.models import Triple, Property
from django.contrib.contenttypes.models import ContentType
from django_filters.rest_framework import DjangoFilterBackend

def query_params_to_filter_dict(query_params):
params = {}
was_parsed = False
for k, v in query_params.items():
# check for pagination params:
if k == "limit" or k == "offset":
continue
# check for int
try:
v = int(v)
except:
pass
else:
was_parsed = True
# check for boolean
if not was_parsed:
if v.lower() == "true":
v = True
was_parsed = True
elif v.lower() == "false":
v = False
was_parsed = True
# check for list
if not was_parsed:
if k.endswith("__in"):
try:
v = literal_eval(v)
except:
pass
else:
was_parsed = True
params[k] = v
return params


class F3ManifestationProductType(viewsets.ReadOnlyModelViewSet):
serializer_class = F3ManifestationProductTypeSerializer
filter_class = F3ManifestationProductTypeFilter
# filter_class = F3ManifestationProductTypeFilter
queryset = F3_Manifestation_Product_Type.objects.all().prefetch_related('triple_set_from_obj', 'triple_set_from_subj')
def get_queryset(self):
qs = super().get_queryset()
params = query_params_to_filter_dict(self.request.query_params)
return qs.filter(**params)

class Notes(viewsets.ReadOnlyModelViewSet):
serializer_class = NoteSerializer
queryset = XMLNote.objects.all().prefetch_related('triple_set_from_obj')
def get_queryset(self):
qs = super().get_queryset()
params = query_params_to_filter_dict(self.request.query_params)
return qs.filter(**params)

class F31Performance(viewsets.ReadOnlyModelViewSet):
serializer_class = F31PerformanceSerializer
Expand Down Expand Up @@ -103,3 +151,20 @@ class EntitiesWithoutRelations(viewsets.ReadOnlyModelViewSet):
queryset = E1_Crm_Entity.objects.annotate(relation_count=Count("triple_set_from_obj")+Count("triple_set_from_subj", filter=Q(triple_set_from_subj__obj__name__regex=r'^(?!.*_index\.xml$).*$'))).filter(relation_count=0)
serializer_class = LonelyE1CrmEntitySerializer
filter_class=EntitiesWithoutRelationsFilter

class Triples(viewsets.ReadOnlyModelViewSet):
queryset = Triple.objects.all().select_related('obj', 'subj', 'prop')
serializer_class = SimpleTripleSerializer
def get_queryset(self):
qs = super().get_queryset()
params = query_params_to_filter_dict(self.request.query_params)
return qs.filter(**params)

class PersonTriples(viewsets.ReadOnlyModelViewSet):
queryset = Triple.objects.all().select_related('obj', 'prop').prefetch_related('subj')
serializer_class = PersonTripleSerializer
def get_queryset(self):
qs = super().get_queryset()
params = query_params_to_filter_dict(self.request.query_params)
return qs.filter(**params)

7 changes: 7 additions & 0 deletions apis_ontology/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
router.register(r'search', Search, basename="Search")
router.register(r'entities_without_relations', EntitiesWithoutRelations, basename='EntitiesWithoutRelations')

# rebuild additional serializers
router.register(r'triples', Triples, basename='Triples')
router.register(r'person_triples', PersonTriples, basename='PersonTriples')
# router.register(r'manifestation_details', ManifestationDetails, basename='ManifestationDetails')
router.register(r'notes', Notes, basename='Notes')


customurlpatterns = [
path('custom-api/', include(router.urls)),
path("accounts/", include("django.contrib.auth.urls")),
Expand Down

0 comments on commit b8efc1c

Please sign in to comment.