Coverage for apis_core/apis_entities/urls.py: 62%
24 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-16 07:42 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-16 07:42 +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
6# from .views import ReversionCompareView TODO: add again when import is fixed
7from apis_core.apis_entities.models import AbstractEntity
8from apis_core.apis_entities.views import (
9 EntitiesAutocomplete,
10 EntitiesDuplicate,
11 EntitiesMerge,
12 EntitiesUpdate,
13)
14from apis_core.generic.views import Create, Delete, Detail, List
17class EntityToContenttypeConverter:
18 """
19 A converter that converts from a the name of an entity class
20 (i.e. `person`) to the actual Django model class.
21 """
23 regex = r"\w+"
25 def to_python(self, value):
26 candiates = get_list_or_404(ContentType, model=value)
27 candiates = list(
28 filter(
29 lambda ct: ct.model_class() is not None
30 and issubclass(ct.model_class(), AbstractEntity),
31 candiates,
32 )
33 )
34 if len(candiates) > 1:
35 raise Http404("Multiple entities match the <%s> identifier" % value)
36 return candiates[0]
38 def to_url(self, value):
39 if isinstance(value, ContentType):
40 return value.model
41 if isinstance(value, str):
42 return value
45register_converter(EntityToContenttypeConverter, "entitytocontenttype")
47app_name = "apis_entities"
49entity_patterns = [
50 path(
51 "list/",
52 List.as_view(),
53 name="generic_entities_list",
54 ),
55 path(
56 "create/",
57 Create.as_view(),
58 name="generic_entities_create_view",
59 ),
60 path(
61 "<int:pk>/detail/",
62 Detail.as_view(),
63 name="generic_entities_detail_view",
64 ),
65 path(
66 "<int:pk>/edit/",
67 EntitiesUpdate.as_view(),
68 name="generic_entities_edit_view",
69 ),
70 path(
71 "<int:pk>/delete/",
72 Delete.as_view(),
73 name="generic_entities_delete_view",
74 ),
75 path(
76 "<int:pk>/duplicate/",
77 EntitiesDuplicate.as_view(),
78 name="generic_entities_duplicate_view",
79 ),
80 path(
81 "<int:pk>/merge/",
82 EntitiesMerge.as_view(),
83 name="generic_entities_merge_view",
84 ),
85]
87urlpatterns = [
88 path(
89 "entity/<entitytocontenttype:contenttype>/",
90 include(entity_patterns),
91 ),
92 path("autocomplete/", EntitiesAutocomplete.as_view(), name="autocomplete"),
93]