1 from django.contrib.auth.mixins import LoginRequiredMixin

2 from django.http import HttpResponse, HttpResponseRedirect

3 from django.shortcuts import get_object_or_404, render

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 RuleForm

8 from meshadmin.server.networks.mixins import NetworkPermissionMixin

9 from meshadmin.server.networks.models import Group, Rule

10

11

12 class RuleDetailView(LoginRequiredMixin, NetworkPermissionMixin, DetailView):

13 model = Rule

14 template_name = "networks/rule/detail.html"

15

16 def get_network(self):

17 return self.get_object().security_group.network

18

19 def get_context_data(self, **kwargs):

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

21 context["fields"] = [

22 {

23 "name": field.name,

24 "verbose_name": field.verbose_name,

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

26 }

27 for field in self.model._meta.fields

28 ]

29 return context

30

31

32 class RuleCreateView(LoginRequiredMixin, NetworkPermissionMixin, CreateView):

33 model = Rule

34 form_class = RuleForm

35 template_name = "networks/rule/form.html"

36

37 def get_network(self):

38 security_group_id = self.kwargs.get("security_group_id")

39 if security_group_id:

40 security_group = get_object_or_404(Group, id=security_group_id)

41 return security_group.network

42 return None

43

44 def get_success_url(self):

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

46

47 def get_form_kwargs(self):

48 kwargs = super().get_form_kwargs()

49 security_group_id = self.kwargs.get("security_group_id")

50 if security_group_id:

51 kwargs["initial"] = {"security_group_id": security_group_id}

52 return kwargs

53

54 def get_context_data(self, **kwargs):

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

56 context["action"] = "Create"

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

58 security_group_id = self.kwargs.get("security_group_id")

59 if security_group_id:

60 context["security_group"] = get_object_or_404(Group, id=security_group_id)

61 return context

62

63 def form_invalid(self, form):

64 return super().form_invalid(form)

65

66 def form_valid(self, form):

67 security_group_id = self.kwargs.get("security_group_id")

68 if security_group_id:

69 form.instance.security_group_id = security_group_id

70 return super().form_valid(form)

71

72

73 class RuleUpdateView(LoginRequiredMixin, NetworkPermissionMixin, UpdateView):

74 model = Rule

75 form_class = RuleForm

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

77

78 def get_network(self):

79 return self.get_object().security_group.network

80

81 def get_context_data(self, **kwargs):

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

83 context["action"] = "Update"

84 context["security_group"] = self.object.security_group

85 return context

86

87 def get_form_kwargs(self):

88 kwargs = super().get_form_kwargs()

89 kwargs["initial"] = {"security_group_id": self.object.security_group_id}

90 return kwargs

91

92 def form_invalid(self, form):

93 return super().form_invalid(form)

94

95 def get_success_url(self):

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

97

98 def form_valid(self, form):

99 response = super().form_valid(form)

100 if self.request.htmx:

101 context = {

102 "rules": self.object.security_group.rules.all(),

103 }

104 return render(self.request, "networks/group/_rules_list.html", context)

105 return response

106

107

108 class RuleDeleteView(LoginRequiredMixin, NetworkPermissionMixin, DeleteView):

109 model = Rule

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

111

112 def get_network(self):

113 return self.get_object().security_group.network

114

115 def delete(self, request, *args, **kwargs):

116 self.object = self.get_object()

117 self.object.delete()

118

119 if request.htmx:

120 return HttpResponse("")

121

122 return HttpResponseRedirect(self.get_success_url())

123

124 def get_success_url(self):

125 return (

126 reverse_lazy(

127 "networks:network-detail",

128 kwargs={"pk": self.object.security_group.network.pk},

129 )

130 + "#groups-section"

131 )

132

133 def get_context_data(self, **kwargs):

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

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

136 return context