From b8efc1cc85da54f9c08716e2ff903d69e7e5ab92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katharina=20W=C3=BCnsche?= Date: Sun, 3 Dec 2023 20:32:50 +0100 Subject: [PATCH] chore: rebuild additional serializers --- apis_ontology/jelinek_api_serializers.py | 51 +++++++++++++++--- apis_ontology/jelinek_api_views.py | 67 +++++++++++++++++++++++- apis_ontology/urls.py | 7 +++ 3 files changed, 117 insertions(+), 8 deletions(-) diff --git a/apis_ontology/jelinek_api_serializers.py b/apis_ontology/jelinek_api_serializers.py index 5389fea..6645a75 100644 --- a/apis_ontology/jelinek_api_serializers.py +++ b/apis_ontology/jelinek_api_serializers.py @@ -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, @@ -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() @@ -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) + \ No newline at end of file diff --git a/apis_ontology/jelinek_api_views.py b/apis_ontology/jelinek_api_views.py index ca7a252..68f0f73 100644 --- a/apis_ontology/jelinek_api_views.py +++ b/apis_ontology/jelinek_api_views.py @@ -1,3 +1,4 @@ +from ast import literal_eval from rest_framework.response import Response from rest_framework import viewsets from .models import * @@ -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 @@ -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) + \ No newline at end of file diff --git a/apis_ontology/urls.py b/apis_ontology/urls.py index e710ff3..7b3f4c9 100644 --- a/apis_ontology/urls.py +++ b/apis_ontology/urls.py @@ -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")),