Coverage for apis_core/relations/views.py: 37%

49 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-22 07:51 +0000

1from django.contrib.contenttypes.models import ContentType 

2from django.shortcuts import get_object_or_404 

3from django.urls import reverse 

4from django.views.generic.base import TemplateView 

5 

6from apis_core.generic.views import Create 

7from apis_core.relations.templatetags.relations import ( 

8 possible_relation_types_from, 

9 relations_from, 

10) 

11 

12 

13class CreateRelation(Create): 

14 def get_form_kwargs(self, *args, **kwargs) -> dict: 

15 kwargs = super().get_form_kwargs(*args, **kwargs) 

16 kwargs["initial"]["obj_content_type"] = self.request.GET.get("obj_content_type") 

17 kwargs["initial"]["obj_object_id"] = self.request.GET.get("obj_object_id") 

18 kwargs["initial"]["subj_content_type"] = self.request.GET.get( 

19 "subj_content_type" 

20 ) 

21 kwargs["initial"]["subj_object_id"] = self.request.GET.get("subj_object_id") 

22 return kwargs 

23 

24 

25class CreateRelationForm(CreateRelation): 

26 template_name = "relations/create_relation_form.html" 

27 

28 def setup(self, *args, **kwargs) -> None: 

29 super().setup(*args, **kwargs) 

30 self.reverse = self.request.GET.get("reverse", "false").lower() == "true" 

31 

32 def get(self, *args, **kwargs): 

33 resp = super().get(*args, **kwargs) 

34 content_type = ContentType.objects.get_for_model(self.model) 

35 resp["HX-Trigger-After-Settle"] = ( 

36 '{"reinit_select2": "relation_' + content_type.model + '_form"}' 

37 ) 

38 return resp 

39 

40 def get_form_kwargs(self, *args, **kwargs) -> dict: 

41 kwargs = super().get_form_kwargs(*args, **kwargs) 

42 kwargs["reverse"] = self.reverse 

43 content_type = ContentType.objects.get_for_model(self.model) 

44 kwargs["hx_post_route"] = reverse( 

45 "apis_core:relations:create_relation_form", args=[content_type] 

46 ) 

47 return kwargs 

48 

49 def get_success_url(self) -> str: 

50 args = [ 

51 self.object.obj_content_type, 

52 self.object.subj_content_type, 

53 self.object.subj_object_id, 

54 ] 

55 if self.reverse: 

56 args = [ 

57 self.object.subj_content_type, 

58 self.object.obj_content_type, 

59 self.object.obj_object_id, 

60 ] 

61 return reverse("apis_core:relations:list_relations", args=args) 

62 

63 

64class ListRelations(TemplateView): 

65 template_name = "relations/list_relations.html" 

66 

67 def get_context_data(self, *args, **kwargs) -> dict: 

68 ctx = super().get_context_data(*args, **kwargs) 

69 ctx["target"] = kwargs.get("target_contenttype") 

70 ctx["object"] = get_object_or_404( 

71 kwargs.get("object_contenttype").model_class(), pk=kwargs.get("object_id") 

72 ) 

73 ctx["relations"] = relations_from(ctx["object"]) 

74 ctx["possible_relations"] = possible_relation_types_from(ctx["object"]) 

75 ctx["edit"] = True 

76 return ctx 

77 

78 def get(self, *args, **kwargs): 

79 resp = super().get(*args, **kwargs) 

80 resp["HX-Trigger"] = "dismissModal" 

81 return resp