Coverage for src/mesh/views/views_journal_section.py: 36%

88 statements  

« prev     ^ index     » next       coverage.py v7.7.0, created at 2025-04-28 07:45 +0000

1from typing import Any 

2 

3from django.contrib import messages 

4from django.http import HttpRequest, HttpResponse, HttpResponseRedirect 

5from django.shortcuts import get_object_or_404 

6from django.urls import reverse_lazy 

7from django.utils.translation import gettext_lazy as _ 

8from django.views.generic.edit import CreateView, UpdateView 

9 

10from mesh.views.forms.base_forms import DEFAULT_FORM_BUTTONS, FormAction 

11from mesh.views.forms.submission_forms import JournalSectionForm 

12from mesh.views.mixins import RoleMixin 

13 

14from ..models.journal_models import JournalSection 

15from .components.breadcrumb import get_base_breadcrumb 

16from .components.button import Button 

17from .components.tree_node import build_tree_recursive 

18 

19 

20class JournalSectionListView(RoleMixin, CreateView): 

21 """ 

22 View to display all existing sections of the journal. 

23 """ 

24 

25 model = JournalSection 

26 form_class = JournalSectionForm 

27 template_name = "mesh/journal_section_list.html" 

28 

29 def get_form(self, form_class=None) -> JournalSectionForm | None: 

30 if not self.role_handler.check_rights("can_edit_journal_sections"): 

31 return None 

32 return super().get_form(form_class) # type:ignore 

33 

34 def get_form_kwargs(self) -> dict[str, Any]: 

35 kwargs = super().get_form_kwargs() 

36 kwargs["_parents"] = JournalSection.objects.all_journal_sections() 

37 return kwargs 

38 

39 def restrict_dispatch(self, request: HttpRequest, *args, **kwargs) -> bool: 

40 if not self.role_handler.check_rights("can_access_journal_sections"): 

41 return True 

42 

43 if request.method == "POST" and not self.role_handler.check_rights( 

44 "can_edit_journal_sections" 

45 ): 

46 return True 

47 

48 return False 

49 

50 def get_success_url(self) -> str: 

51 return reverse_lazy("mesh:journal_section_list") 

52 

53 def get_context_data(self, *args, **kwargs): 

54 context = super().get_context_data(*args, **kwargs) 

55 

56 context["journal_sections_tree"] = build_tree_recursive(JournalSection.objects) 

57 context["page_title"] = _("Journal sections") 

58 

59 breadcrumb = get_base_breadcrumb() 

60 breadcrumb.add_item( 

61 title=_("Journal sections"), url=reverse_lazy("mesh:journal_section_list") 

62 ) 

63 context["breadcrumb"] = breadcrumb 

64 

65 return context 

66 

67 def form_valid(self, form) -> HttpResponse: 

68 form.instance._user = self.request.user 

69 return super().form_valid(form) 

70 

71 

72class JournalSectionEditView(RoleMixin, UpdateView): 

73 model = JournalSection 

74 form_class = JournalSectionForm 

75 journal_section: JournalSection 

76 template_name = "mesh/forms/submission_journal_section_update.html" 

77 

78 def restrict_dispatch(self, request: HttpRequest, *args, **kwargs) -> bool: 

79 if not self.role_handler.check_rights("can_edit_journal_sections"): 

80 return True 

81 self.journal_section = get_object_or_404(JournalSection, pk=kwargs["pk"]) 

82 return False 

83 

84 def get_success_url(self) -> str: 

85 return reverse_lazy("mesh:journal_section_list") 

86 

87 def get_form(self, form_class=None) -> JournalSectionForm: 

88 form: JournalSectionForm = super().get_form(form_class) # type:ignore 

89 form.buttons = [ 

90 *DEFAULT_FORM_BUTTONS, 

91 Button( 

92 id="form_delete", 

93 title=_("Delete"), 

94 icon_class="fa-trash", 

95 attrs={ 

96 "type": ["submit"], 

97 "class": ["button-error"], 

98 "name": [FormAction.DELETE.value], 

99 }, 

100 ), 

101 ] 

102 return form 

103 

104 def get_form_kwargs(self) -> dict[str, Any]: 

105 kwargs = super().get_form_kwargs() 

106 kwargs["_parents"] = JournalSection.objects.all_journal_sections().exclude( 

107 pk__in=[ 

108 *[c.pk for c in self.journal_section.all_children()], 

109 self.journal_section.pk, 

110 ] 

111 ) 

112 return kwargs 

113 

114 def get_context_data(self, *args, **kwargs): 

115 context = super().get_context_data(*args, **kwargs) 

116 

117 context["journal_section"] = self.journal_section 

118 context["journal_sections_tree"] = build_tree_recursive( 

119 JournalSection.objects, self.journal_section 

120 ) 

121 

122 parent = self.journal_section.parent 

123 if parent is None: 

124 title_button = Button( 

125 id="title-back-to", 

126 title=_("Back to index"), 

127 icon_class="fa-folder-tree", 

128 attrs={ 

129 "href": [reverse_lazy("mesh:journal_section_list")], 

130 "class": ["as-button"], 

131 }, 

132 ) 

133 else: 

134 title_button = Button( 

135 id="title-back-to", 

136 title=_("Back to") + f" {parent.name}", 

137 icon_class="fa-arrow-right-long", 

138 attrs={ 

139 "href": [ 

140 reverse_lazy( 

141 "mesh:submission_journal_section_update", 

142 kwargs={"pk": parent.pk}, 

143 ) 

144 ], 

145 "class": ["as-button"], 

146 }, 

147 ) 

148 context["title_buttons"] = [title_button] 

149 

150 # Generate breadcrumb 

151 breadcrumb = get_base_breadcrumb() 

152 breadcrumb.add_item( 

153 title=_("Journal sections"), url=reverse_lazy("mesh:journal_section_list") 

154 ) 

155 breadcrumb.add_item( 

156 title=self.journal_section.name, 

157 url=reverse_lazy( 

158 "mesh:submission_journal_section_update", 

159 kwargs={"pk": self.journal_section.pk}, 

160 ), 

161 ) 

162 context["breadcrumb"] = breadcrumb 

163 return context 

164 

165 def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: 

166 if FormAction.DELETE.value in request.POST: 

167 self.journal_section.delete() 

168 messages.success( 

169 request, 

170 _(f"The journal_section {self.journal_section.name} was successfully deleted."), 

171 ) 

172 return HttpResponseRedirect(self.get_success_url()) 

173 return super().post(request, *args, **kwargs) 

174 

175 def form_valid(self, form: JournalSectionForm): 

176 form.instance._user = self.request.user 

177 return super().form_valid(form)