diff --git a/apis_core/apis_entities/api_views.py b/apis_core/apis_entities/api_views.py new file mode 100644 index 0000000..4743c73 --- /dev/null +++ b/apis_core/apis_entities/api_views.py @@ -0,0 +1,74 @@ +from django.conf import settings +from django.http import Http404 +from django.shortcuts import redirect +from django.urls import reverse +from rest_framework.decorators import api_view +from rest_framework.generics import GenericAPIView +from rest_framework.response import Response +from rest_framework.settings import api_settings + +from apis_core.apis_metainfo.models import TempEntityClass, Uri +from .api_renderers import ( + EntityToTEI, +) + + +class GetEntityGeneric(GenericAPIView): + queryset = TempEntityClass.objects.all() + renderer_classes = tuple(api_settings.DEFAULT_RENDERER_CLASSES) + (EntityToTEI,) + if getattr(settings, "APIS_RENDERERS", None) is not None: + rend_add = tuple() + for rd in settings.APIS_RENDERERS: + rend_mod = __import__(rd) + for name, cls in rend_mod.__dict__.items(): + rend_add + (cls,) + renderer_classes += rend_add + + def get_object(self, pk, request): + try: + return TempEntityClass.objects_inheritance.get_subclass(pk=pk) + except TempEntityClass.DoesNotExist: + uri2 = Uri.objects.filter(uri=request.build_absolute_uri()) + if uri2.count() == 1: + return TempEntityClass.objects_inheritance.get_subclass( + pk=uri2[0].entity_id + ) + else: + raise Http404 + + def get(self, request, pk): + ent = self.get_object(pk, request) + data_view = request.GET.get("data-view", False) + format_param = request.GET.get("format", False) + requested_format = request.META.get("HTTP_ACCEPT") + if requested_format is not None: + if ( + requested_format.startswith("text/html") + and not data_view + and not format_param + ): + return redirect(ent) + res = EntitySerializer(ent, context={"request": request}) + return Response(res.data) + + +@api_view(["GET"]) +def uri_resolver(request): + uri = request.query_params.get("uri", None) + f = request.query_params.get("target_format", "gui") + if uri is None: + raise Http404 + else: + uri = Uri.objects.get(uri=uri) + if f == "gui": + ent = TempEntityClass.objects_inheritance.get_subclass(pk=uri.entity_id) + c_name = ent.__class__.__name__ + url = reverse( + "apis_core:apis_entities:generic_entities_detail_view", + kwargs={"pk": uri.entity_id, "entity": c_name.lower()}, + ) + else: + url = reverse( + "apis_core:apis_api2:GetEntityGeneric", kwargs={"pk": uri.entity_id} + ) + "?format={}".format(f) + return redirect(url) diff --git a/apis_core/apis_entities/autocomplete3.py b/apis_core/apis_entities/autocomplete3.py index 849765c..7a7b93f 100644 --- a/apis_core/apis_entities/autocomplete3.py +++ b/apis_core/apis_entities/autocomplete3.py @@ -129,7 +129,7 @@ def get(self, request, *args, **kwargs): test_stanbol = False more = True if not db_include: - for r in res[offset: offset + page_size]: + for r in res[offset : offset + page_size]: if int(r.pk) == int(ent_merge_pk): continue @@ -185,7 +185,7 @@ def get(self, request, *args, **kwargs): {"id": x.pk, "text": x.label} for x in vocab_model.objects.filter(name__icontains=q).order_by( "parent_class__name", "name" - )[offset: offset + page_size] + )[offset : offset + page_size] ] else: choices = [ @@ -193,7 +193,7 @@ def get(self, request, *args, **kwargs): for x in vocab_model.objects.filter( Q(name__icontains=q) | Q(name_reverse__icontains=q) ).order_by("parent_class__name", "name")[ - offset: offset + page_size + offset : offset + page_size ] ] elif direct == "reverse": @@ -201,7 +201,7 @@ def get(self, request, *args, **kwargs): {"id": x.pk, "text": x.label_reverse} for x in vocab_model.objects.filter( Q(name__icontains=q) | Q(name_reverse__icontains=q) - ).order_by("parent_class__name", "name")[offset: offset + page_size] + ).order_by("parent_class__name", "name")[offset : offset + page_size] ] if len(choices) == page_size: more = True diff --git a/apis_core/apis_entities/detail_views.py b/apis_core/apis_entities/detail_views.py index 36c6e99..aa66839 100644 --- a/apis_core/apis_entities/detail_views.py +++ b/apis_core/apis_entities/detail_views.py @@ -1,7 +1,7 @@ from django.conf import settings from django.db.models import Q -from django.http import Http404, HttpResponse -from django.shortcuts import get_object_or_404, redirect +from django.http import HttpResponse +from django.shortcuts import redirect from django.template.loader import select_template from django.views import View from django_tables2 import RequestConfig @@ -10,32 +10,7 @@ from apis_core.apis_metainfo.models import Uri from apis_core.apis_relations.models import AbstractRelation from apis_core.apis_relations.tables import LabelTableBase, get_generic_relations_table - -from .models import BASE_URI, TempEntityClass - - -def get_object_from_pk_or_uri(request, pk): - """checks if the given pk exists, if not checks if a matching apis-default uri exists - and returns its entity""" - try: - instance = TempEntityClass.objects_inheritance.get_subclass(pk=pk) - return instance - except TempEntityClass.DoesNotExist: - domain = BASE_URI - new_uri = f"{domain}entity/{pk}/" - uri2 = Uri.objects.filter(uri=new_uri) - if uri2.count() == 1: - instance = TempEntityClass.objects_inheritance.get_subclass( - pk=uri2[0].entity_id - ) - elif uri2.count() == 0: - temp_obj = get_object_or_404(Uri, uri=new_uri[:-1]) - instance = TempEntityClass.objects_inheritance.get_subclass( - pk=temp_obj.entity_id - ) - else: - raise Http404 - return instance +from apis_core.utils import get_object_from_pk_or_uri class GenericEntitiesDetailView(View): @@ -44,8 +19,7 @@ class GenericEntitiesDetailView(View): def get(self, request, *args, **kwargs): entity = kwargs["entity"].lower() pk = kwargs["pk"] - instance = get_object_from_pk_or_uri(request, pk) - # print(instance.id, pk) + instance = get_object_from_pk_or_uri(pk) if f"{instance.id}" == f"{pk}": pass else: diff --git a/apis_core/apis_entities/models.py b/apis_core/apis_entities/models.py index 8cb447f..1592bf3 100644 --- a/apis_core/apis_entities/models.py +++ b/apis_core/apis_entities/models.py @@ -8,6 +8,7 @@ from django.db.models import Q from django.db.models.signals import post_save from django.dispatch import receiver +from django.urls import reverse from apis_core.apis_metainfo.models import TempEntityClass, Uri from apis_core.apis_vocabularies.models import ( @@ -515,6 +516,12 @@ class Meta: "id", ] + def get_tei_url(self): + return reverse("apis_core:apis_tei:person_as_tei", kwargs={"pk": self.id}) + + def get_api_url(self): + return f"/apis/api/entities/person/{self.id}/" + class Place(AbstractEntity): kind = models.ForeignKey( @@ -534,6 +541,12 @@ class Meta: "id", ] + def get_tei_url(self): + return reverse("apis_core:apis_tei:place_as_tei", kwargs={"pk": self.id}) + + def get_api_url(self): + return f"/apis/api/entities/{self.__class__.__name__.lower()}/{self.id}/" + class Institution(AbstractEntity): kind = models.ForeignKey( @@ -545,6 +558,12 @@ class Meta: "id", ] + def get_tei_url(self): + return reverse("apis_core:apis_tei:org_as_tei", kwargs={"pk": self.id}) + + def get_api_url(self): + return f"/apis/api/entities/{self.__class__.__name__.lower()}/{self.id}/" + class Event(AbstractEntity): kind = models.ForeignKey( @@ -556,6 +575,9 @@ class Meta: "id", ] + def get_api_url(self): + return f"/apis/api/entities/{self.__class__.__name__.lower()}/{self.id}/" + class Work(AbstractEntity): kind = models.ForeignKey(WorkType, blank=True, null=True, on_delete=models.SET_NULL) @@ -565,6 +587,12 @@ class Meta: "id", ] + def get_tei_url(self): + return reverse("apis_core:apis_tei:work_as_tei", kwargs={"pk": self.id}) + + def get_api_url(self): + return f"/apis/api/entities/{self.__class__.__name__.lower()}/{self.id}/" + a_ents = getattr(settings, "APIS_ADDITIONAL_ENTITIES", False) diff --git a/apis_core/apis_entities/resolver_views.py b/apis_core/apis_entities/resolver_views.py new file mode 100644 index 0000000..a19bbae --- /dev/null +++ b/apis_core/apis_entities/resolver_views.py @@ -0,0 +1,35 @@ +from django.core.exceptions import ObjectDoesNotExist +from django.http import Http404 +from django.shortcuts import redirect +from django.urls.exceptions import NoReverseMatch + +from apis_core.apis_metainfo.models import TempEntityClass, Uri + + +def uri_resolver(request): + uri = request.GET.get("uri", None) + # format_param = request.GET.get("format", False) + # requested_format = request.META.get("HTTP_ACCEPT") + if uri is None: + raise Http404 + else: + try: + uri = Uri.objects.get(uri=uri) + except ObjectDoesNotExist: + raise Http404 + entity = TempEntityClass.objects_inheritance.get_subclass(pk=uri.entity_id) + url = entity.get_absolute_url() + return redirect(url) + + +def entity_resolver(request, pk): + try: + TempEntityClass.objects.get(id=pk) + except ObjectDoesNotExist: + raise Http404 + entity = TempEntityClass.objects_inheritance.get_subclass(pk=pk) + try: + url = entity.get_absolute_url() + except NoReverseMatch: + raise Http404 + return redirect(url) diff --git a/apis_core/apis_entities/templates/apis_entities/detail_views/entity_detail_generic.html b/apis_core/apis_entities/templates/apis_entities/detail_views/entity_detail_generic.html index 8f95c60..b6f24f3 100644 --- a/apis_core/apis_entities/templates/apis_entities/detail_views/entity_detail_generic.html +++ b/apis_core/apis_entities/templates/apis_entities/detail_views/entity_detail_generic.html @@ -55,7 +55,7 @@