Skip to content

Commit

Permalink
Merge pull request #19 from arthur-schnitzler/main
Browse files Browse the repository at this point in the history
URI and Entity Resolver pages added
  • Loading branch information
csae8092 authored Jan 3, 2024
2 parents f25b5ca + 14dd477 commit a0a3266
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 77 deletions.
74 changes: 74 additions & 0 deletions apis_core/apis_entities/api_views.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 4 additions & 4 deletions apis_core/apis_entities/autocomplete3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -185,23 +185,23 @@ 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 = [
{"id": x.pk, "text": x.label}
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":
choices = [
{"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
Expand Down
34 changes: 4 additions & 30 deletions apis_core/apis_entities/detail_views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand All @@ -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:
Expand Down
28 changes: 28 additions & 0 deletions apis_core/apis_entities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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)

Expand Down
35 changes: 35 additions & 0 deletions apis_core/apis_entities/resolver_views.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h2 class="text-center">
<div class="col-md-2">
{% if object.get_next_url %}
<h2>
<a href="{{ object.get_next_url}}" style="float:left">
<a href="{{ object.get_next_url }}" style="float:left">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-chevron-right">
<polyline points="9 18 15 12 9 6" />
</svg>
Expand All @@ -69,7 +69,7 @@ <h2>
<div class="col-md-5">
<h3 class="text-center">
General Info
<a href="/entity/{{object.id}}/?data-view=true">
<a href="{{ object.get_api_url }}">
<small>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-database">
<ellipse cx="12" cy="5" rx="9" ry="3" />
Expand All @@ -78,7 +78,7 @@ <h3 class="text-center">
</svg>
</small>
</a>
{% if entity_type != 'event' %}| <a href="/apis/tei/{{ entity_type }}/{{ object.id }}"><abbr title="link to a project specific TEI-serialisation of this entity">TEI</abbr></a>{% endif %}
{% if entity_type != 'event' %}| <a href="{{ object.get_tei_url }}"><abbr title="link to a project specific TEI-serialisation of this entity">TEI</abbr></a>{% endif %}
</h3>
{% block info-table %}
<table class="table">
Expand Down
36 changes: 34 additions & 2 deletions apis_core/apis_entities/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from icecream import ic

from apis_core.apis_entities.forms import get_entities_form
from apis_core.apis_entities.models import Person
from apis_core.apis_entities.models import Person, Place
from apis_core.apis_metainfo.models import Uri
from normdata.forms import NormDataImportForm
from normdata.utils import (
Expand All @@ -30,9 +30,19 @@ class EntitiesTestCase(TestCase):
]

def setUp(self):
# Create two users
User.objects.create_user(**USER)

def test_001a_entity_resolver(self):
url = reverse("entity-resolver", kwargs={"pk": 4})
r = client.get(url)
self.assertEqual(r.status_code, 404)
url = reverse("entity-resolver", kwargs={"pk": 44442344})
r = client.get(url)
self.assertEqual(r.status_code, 404)
url = reverse("entity-resolver", kwargs={"pk": 1})
r = client.get(url)
self.assertEqual(r.status_code, 302)

def test_001_list_view(self):
for x in MODELS:
try:
Expand Down Expand Up @@ -275,3 +285,25 @@ def test_020_api_list_view(self):
except Exception as e:
print(value, e)
continue

def test_021_api_detail_view(self):
item = Person.objects.last()
r = client.get(item.get_api_url())
self.assertTrue(r.status_code, 200)
item = Place.objects.last()
r = client.get(item.get_api_url())
self.assertTrue(r.status_code, 200)

def test_022_resolver_view(self):
target = Person.objects.last()
source = Person.objects.create(**{"name": "wirdgleichgemerged"})
source_id = source.id
source_uri = f"https://pmb.acdh.oeaw.ac.at/entity/{source.id}/"
target.merge_with(source_id)
url = reverse("uri-resolver")
r = client.get(url)
self.assertTrue(r.status_code, 404)
r = client.get(f"{url}?uri={source_uri}")
self.assertTrue(r.status_code, 302)
r = client.get(f"{url}?uri=https://dasgibtsjagarnicht.com")
self.assertTrue(r.status_code, 404)
2 changes: 1 addition & 1 deletion apis_core/apis_tei/tei_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
path("org/<int:pk>", views.org_as_tei, name="org_as_tei"),
path("institution/<int:pk>", views.org_as_tei, name="org_as_tei"),
path("work/<int:pk>", views.work_as_tei, name="work_as_tei"),
path("uri-to-tei", views.uri_to_tei, name="uri_to_tei"),
path("uri-to-tei/", views.uri_to_tei, name="uri_to_tei"),
]
Loading

0 comments on commit a0a3266

Please sign in to comment.