Coverage for apis_core/generic/urls.py: 64%

25 statements  

« 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_object_or_404 

4from django.urls import include, path, register_converter 

5from rest_framework import routers 

6 

7from apis_core.generic import api_views, views 

8 

9from .abc import GenericModel 

10 

11app_name = "generic" 

12 

13 

14class ContenttypeConverter: 

15 """ 

16 A converter that converts from a string representation of a 

17 model (`app_label.model`) to the actual Django model class. 

18 """ 

19 

20 regex = r"\w+\.\w+" 

21 

22 def to_python(self, value): 

23 app_label, model = value.split(".") 

24 contenttype = get_object_or_404(ContentType, app_label=app_label, model=model) 

25 if issubclass(contenttype.model_class(), GenericModel): 

26 return contenttype 

27 raise Http404 

28 

29 def to_url(self, value): 

30 if isinstance(value, ContentType): 

31 return f"{value.app_label}.{value.model}" 

32 if isinstance(value, str): 

33 return value 

34 

35 

36register_converter(ContenttypeConverter, "contenttype") 

37 

38router = routers.DefaultRouter() 

39router.register(r"", api_views.ModelViewSet, basename="genericmodelapi") 

40 

41urlpatterns = [ 

42 path("overview/", views.Overview.as_view(), name="overview"), 

43 path( 

44 "<contenttype:contenttype>/", 

45 include( 

46 [ 

47 path("", views.List.as_view(), name="list"), 

48 path("<int:pk>", views.Detail.as_view(), name="detail"), 

49 path("create", views.Create.as_view(), name="create"), 

50 path("delete/<int:pk>", views.Delete.as_view(), name="delete"), 

51 path("update/<int:pk>", views.Update.as_view(), name="update"), 

52 path( 

53 "selectmergeorenrich/<int:pk>", 

54 views.SelectMergeOrEnrich.as_view(), 

55 name="selectmergeorenrich", 

56 ), 

57 path( 

58 "merge/<int:pk>/<int:otherpk>", 

59 views.MergeWith.as_view(), 

60 name="merge", 

61 ), 

62 path("enrich/<int:pk>", views.Enrich.as_view(), name="enrich"), 

63 path("autocomplete", views.Autocomplete.as_view(), name="autocomplete"), 

64 path("import", views.Import.as_view(), name="import"), 

65 path( 

66 "autocomplete/externalonly", 

67 views.Autocomplete.as_view(), 

68 {"external_only": True}, 

69 name="autocompleteexternalonly", 

70 ), 

71 ] 

72 ), 

73 ), 

74 path("api/<contenttype:contenttype>/", include(router.urls)), 

75]