Coverage for apis_core/apis_entities/urls.py: 64%
25 statements
« prev ^ index » next coverage.py v7.6.8, created at 2024-12-20 09:24 +0000
« prev ^ index » next coverage.py v7.6.8, created at 2024-12-20 09:24 +0000
1from django.contrib.contenttypes.models import ContentType
2from django.http import Http404
3from django.shortcuts import get_list_or_404
4from django.urls import include, path, register_converter
6from apis_core.apis_entities.api_views import GetEntityGeneric, ListEntityGeneric
8# from .views import ReversionCompareView TODO: add again when import is fixed
9from apis_core.apis_entities.models import AbstractEntity
10from apis_core.apis_entities.views import (
11 EntitiesAutocomplete,
12 EntitiesDuplicate,
13)
15api_routes = [
16 path("entities/", ListEntityGeneric.as_view()),
17 path(
18 "entity/<int:pk>/",
19 GetEntityGeneric.as_view(),
20 name="GetEntityGeneric",
21 ),
22]
25class EntityToContenttypeConverter:
26 """
27 A converter that converts from a the name of an entity class
28 (i.e. `person`) to the actual Django model class.
29 """
31 regex = r"\w+"
33 def to_python(self, value):
34 candiates = get_list_or_404(ContentType, model=value)
35 candiates = list(
36 filter(
37 lambda ct: ct.model_class() is not None
38 and issubclass(ct.model_class(), AbstractEntity),
39 candiates,
40 )
41 )
42 if len(candiates) > 1:
43 raise Http404("Multiple entities match the <%s> identifier" % value)
44 return candiates[0]
46 def to_url(self, value):
47 if isinstance(value, ContentType):
48 return value.model
49 if isinstance(value, str):
50 return value
53register_converter(EntityToContenttypeConverter, "entitytocontenttype")
55app_name = "apis_entities"
57entity_patterns = [
58 path(
59 "<int:pk>/duplicate/",
60 EntitiesDuplicate.as_view(),
61 name="generic_entities_duplicate_view",
62 ),
63]
65urlpatterns = [
66 path(
67 "entity/<entitytocontenttype:contenttype>/",
68 include(entity_patterns),
69 ),
70 path("autocomplete/", EntitiesAutocomplete.as_view(), name="autocomplete"),
71]