1 from django.conf import settings

2 from django.contrib.auth.mixins import LoginRequiredMixin

3 from django.shortcuts import get_object_or_404

4 from django.urls import reverse_lazy

5 from django.views.generic import CreateView, DeleteView, DetailView, UpdateView

6

7 from meshadmin.server.networks.forms import TemplateForm

8 from meshadmin.server.networks.mixins import NetworkPermissionMixin

9 from meshadmin.server.networks.models import Network, Template

10 from meshadmin.server.networks.services import generate_enrollment_token

11

12

13 class TemplateDetailView(LoginRequiredMixin, NetworkPermissionMixin, DetailView):

14 model = Template

15 template_name = "networks/template/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 context["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 "network": self.object.network,

33 "groups": self.object.groups.all(),

34 "server_url": settings.MESH_SERVER_URL,

35 "enrollment_token": generate_enrollment_token(self.object),

36 }

37 )

38 return context

39

40

41 class TemplateCreateView(LoginRequiredMixin, NetworkPermissionMixin, CreateView):

42 model = Template

43 form_class = TemplateForm

44 template_name = "networks/template/form.html"

45

46 def get_network(self):

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

48

49 def get_success_url(self):

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

51

52 def get_form_kwargs(self):

53 kwargs = super().get_form_kwargs()

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

55 if network_id:

56 network = get_object_or_404(Network, id=network_id)

57 kwargs["initial"] = {"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["action"] = "Create"

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

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

66 context["network_id"] = network_id

67 if network_id:

68 context["network"] = get_object_or_404(Network, id=network_id)

69 return context

70

71 def form_valid(self, form):

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

73 if network_id:

74 form.instance.network_id = network_id

75 return super().form_valid(form)

76

77

78 class TemplateUpdateView(LoginRequiredMixin, NetworkPermissionMixin, UpdateView):

79 model = Template

80 form_class = TemplateForm

81 template_name = "networks/template/form.html"

82

83 def get_network(self):

84 return self.get_object().network

85

86 def get_success_url(self):

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

88

89 def get_form_kwargs(self):

90 kwargs = super().get_form_kwargs()

91 kwargs["initial"] = {"network_id": self.object.network_id}

92 return kwargs

93

94 def get_context_data(self, **kwargs):

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

96 context["action"] = "Update"

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

98 return context

99

100

101 class TemplateDeleteView(LoginRequiredMixin, NetworkPermissionMixin, DeleteView):

102 model = Template

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

104 success_url = reverse_lazy("networks:template-list")

105

106 def get_network(self):

107 return self.get_object().network

108

109 def get_success_url(self):

110 return (

111 reverse_lazy(

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

113 )

114 + "#templates-section"

115 )

116

117 def get_context_data(self, **kwargs):

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

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

120 return context