1 from django.contrib.auth.mixins import LoginRequiredMixin

2 from django.http import HttpResponse

3 from django.shortcuts import get_object_or_404

4 from django.template.loader import render_to_string

5 from django.urls import reverse_lazy

6 from django.views.generic import CreateView, DeleteView, DetailView, UpdateView, View

7

8 from meshadmin.server.networks.forms import CAForm

9 from meshadmin.server.networks.mixins import NetworkPermissionMixin

10 from meshadmin.server.networks.models import CA, Host, Network, SigningCA

11

12

13 class CADetailView(LoginRequiredMixin, NetworkPermissionMixin, DetailView):

14 model = CA

15 template_name = "networks/ca/detail.html"

16

17 def get_network(self):

18 return self.get_object().network

19

20 def get_context_data(self, **kwargs):

21 context = super().get_context_data(**kwargs)

22 fields = [

23 {

24 "name": field.name,

25 "verbose_name": field.verbose_name,

26 "value": getattr(self.object, field.name),

27 }

28 for field in self.model._meta.fields

29 ]

30 context.update(

31 {

32 "fields": fields,

33 "network": self.object.network,

34 "hosts": Host.objects.filter(

35 id__in=self.object.hostcert_set.values_list("host_id", flat=True)

36 ).select_related("network"),

37 }

38 )

39 return context

40

41

42 class CACreateView(LoginRequiredMixin, NetworkPermissionMixin, CreateView):

43 model = CA

44 form_class = CAForm

45 template_name = "networks/shared/form.html"

46

47 def get_network(self):

48 return get_object_or_404(Network, id=self.kwargs.get("network_id"))

49

50 def get_success_url(self) -> str:

51 return reverse_lazy("networks:ca-detail", kwargs={"pk": self.object.pk})

52

53 def get_form_kwargs(self):

54 kwargs = super().get_form_kwargs()

55 network_id = self.kwargs.get("network_id")

56 if network_id:

57 network = get_object_or_404(Network, id=network_id)

58 kwargs["network"] = network

59 return kwargs

60

61 def get_context_data(self, **kwargs):

62 context = super().get_context_data(**kwargs)

63 context.update(

64 {

65 "action": "Create",

66 "model_name": self.model._meta.verbose_name,

67 "network_id": self.kwargs.get("network_id"),

68 }

69 )

70 return context

71

72

73 class CAUpdateView(LoginRequiredMixin, NetworkPermissionMixin, UpdateView):

74 model = CA

75 form_class = CAForm

76 template_name = "networks/shared/form.html"

77

78 def get_network(self):

79 return self.get_object().network

80

81 def get_success_url(self):

82 return reverse_lazy("networks:ca-detail", kwargs={"pk": self.object.pk})

83

84 def get_context_data(self, **kwargs):

85 context = super().get_context_data(**kwargs)

86 context["action"] = "Update"

87 context["model_name"] = self.model._meta.verbose_name

88 return context

89

90

91 class CADeleteView(LoginRequiredMixin, NetworkPermissionMixin, DeleteView):

92 model = CA

93 template_name = "networks/shared/delete.html"

94

95 def get_network(self):

96 return self.get_object().network

97

98 def get_success_url(self):

99 return (

100 reverse_lazy(

101 "networks:network-detail", kwargs={"pk": self.object.network.pk}

102 )

103 + "#cas-section"

104 )

105

106 def get_context_data(self, **kwargs):

107 context = super().get_context_data(**kwargs)

108 context["model_name"] = self.model._meta.verbose_name

109 return context

110

111

112 class CAMakeSigningView(LoginRequiredMixin, View):

113 def post(self, request, pk):

114 ca = get_object_or_404(CA, pk=pk)

115 network = ca.network

116 SigningCA.objects.filter(network=network).delete()

117 SigningCA.objects.create(network=network, ca=ca)

118 context = {"cas": CA.objects.filter(network=network)}

119 return HttpResponse(

120 render_to_string(

121 "networks/network/_cas_table.html", context, request=request

122 )

123 )