Commit ee6914a14f7c664dc88d7ecce6c92dee49179786
1 parent
61d40e31
Exists in
master
and in
5 other branches
Adding link crud log functions [Issue: #246]
Showing
2 changed files
with
133 additions
and
29 deletions
Show diff stats
files/views.py
... | ... | @@ -138,7 +138,7 @@ def render_file(request, id): |
138 | 138 | |
139 | 139 | class UpdateFile(LoginRequiredMixin, HasRoleMixin, LogMixin, generic.UpdateView): |
140 | 140 | log_component = 'file' |
141 | - log_resource = 'resource' | |
141 | + log_resource = 'file' | |
142 | 142 | log_action = 'update' |
143 | 143 | log_context = {} |
144 | 144 | |
... | ... | @@ -157,6 +157,7 @@ class UpdateFile(LoginRequiredMixin, HasRoleMixin, LogMixin, generic.UpdateView) |
157 | 157 | |
158 | 158 | return context |
159 | 159 | |
160 | + | |
160 | 161 | def form_valid(self, form): |
161 | 162 | self.object = form.save() |
162 | 163 | ... | ... |
links/views.py
... | ... | @@ -11,6 +11,10 @@ from django.urls import reverse |
11 | 11 | from django.core.files.base import ContentFile |
12 | 12 | from rolepermissions.verifications import has_role |
13 | 13 | |
14 | +from core.models import Log | |
15 | +from core.mixins import LogMixin | |
16 | +from core.decorators import log_decorator | |
17 | + | |
14 | 18 | from .image_crawler import * |
15 | 19 | from courses.models import Topic |
16 | 20 | from .models import Link |
... | ... | @@ -18,12 +22,18 @@ from .forms import * |
18 | 22 | |
19 | 23 | |
20 | 24 | # Create your views here. |
21 | -class CreateLink(LoginRequiredMixin, HasRoleMixin, NotificationMixin, generic.CreateView): | |
25 | +class CreateLink(LoginRequiredMixin, HasRoleMixin, LogMixin, NotificationMixin, generic.CreateView): | |
26 | + log_component = 'link' | |
27 | + log_resource = 'link' | |
28 | + log_action = 'create' | |
29 | + log_context = {} | |
30 | + | |
22 | 31 | allowed_roles = ['professor', 'system_admin'] |
23 | 32 | template_name = 'links/create_link.html' |
24 | 33 | form_class = CreateLinkForm |
25 | 34 | success_url = reverse_lazy('course:manage') |
26 | 35 | context_object_name = 'form' |
36 | + | |
27 | 37 | def form_invalid(self,form): |
28 | 38 | context = super(CreateLink, self).form_invalid(form) |
29 | 39 | context.status_code = 400 |
... | ... | @@ -42,8 +52,27 @@ class CreateLink(LoginRequiredMixin, HasRoleMixin, NotificationMixin, generic.Cr |
42 | 52 | super(CreateLink, self).createNotification(message="created a Link at "+ self.object.topic.name, actor=self.request.user, |
43 | 53 | resource_name=self.object.name, resource_link= reverse('course:view_topic', args=[self.object.topic.slug]), |
44 | 54 | users=self.object.topic.subject.students.all()) |
55 | + | |
56 | + self.log_context['link_id'] = self.object.id | |
57 | + self.log_context['link_name'] = self.object.name | |
58 | + self.log_context['topic_id'] = self.object.topic.id | |
59 | + self.log_context['topic_name'] = self.object.topic.name | |
60 | + self.log_context['topic_slug'] = self.object.topic.slug | |
61 | + self.log_context['subject_id'] = self.object.topic.subject.id | |
62 | + self.log_context['subject_name'] = self.object.topic.subject.name | |
63 | + self.log_context['subject_slug'] = self.object.topic.subject.slug | |
64 | + self.log_context['course_id'] = self.object.topic.subject.course.id | |
65 | + self.log_context['course_name'] = self.object.topic.subject.course.name | |
66 | + self.log_context['course_slug'] = self.object.topic.subject.course.slug | |
67 | + self.log_context['course_category_id'] = self.object.topic.subject.course.category.id | |
68 | + self.log_context['course_category_name'] = self.object.topic.subject.course.category.name | |
69 | + | |
70 | + super(CreateLink, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
71 | + | |
45 | 72 | self.setImage() |
73 | + | |
46 | 74 | return self.get_success_url() |
75 | + | |
47 | 76 | def setImage(self): |
48 | 77 | if self.baixado: |
49 | 78 | with open(self.caminho,'rb') as f: |
... | ... | @@ -54,6 +83,7 @@ class CreateLink(LoginRequiredMixin, HasRoleMixin, NotificationMixin, generic.Cr |
54 | 83 | with open('links/static/images/default.jpg','rb') as f: |
55 | 84 | data = f.read() |
56 | 85 | self.link.image.save('default.jpg',ContentFile(data)) |
86 | + | |
57 | 87 | def get_context_data(self,**kwargs): |
58 | 88 | context = {} |
59 | 89 | context['links'] = Link.objects.all() |
... | ... | @@ -61,33 +91,55 @@ class CreateLink(LoginRequiredMixin, HasRoleMixin, NotificationMixin, generic.Cr |
61 | 91 | topic = get_object_or_404(Topic, slug = self.kwargs.get('slug')) |
62 | 92 | context["topic"] = topic |
63 | 93 | return context |
94 | + | |
64 | 95 | def get_success_url(self): |
65 | 96 | self.success_url = redirect('course:links:render_link', slug = self.object.slug) |
66 | 97 | return self.success_url |
67 | 98 | |
68 | -class DeleteLink(LoginRequiredMixin, HasRoleMixin, generic.DeleteView): | |
69 | - allowed_roles = ['professor', 'system_admin'] | |
70 | - login_url = reverse_lazy("core:home") | |
71 | - redirect_field_name = 'next' | |
72 | - model = Link | |
73 | - template_name = 'links/delete_link.html' | |
74 | - | |
75 | - def dispatch(self, *args, **kwargs): | |
76 | - link = get_object_or_404(Link, slug = self.kwargs.get('slug')) | |
77 | - if(not (link.topic.owner == self.request.user) and not(has_role(self.request.user, 'system_admin')) ): | |
78 | - return self.handle_no_permission() | |
79 | - return super(DeleteLink, self).dispatch(*args, **kwargs) | |
80 | - | |
81 | - def get_context_data(self, **kwargs): | |
82 | - context = super(DeleteLink, self).get_context_data(**kwargs) | |
83 | - context['course'] = self.object.topic.subject.course | |
84 | - context['subject'] = self.object.topic.subject | |
85 | - context['link'] = self.object | |
86 | - context["topic"] = self.object.topic | |
87 | - return context | |
88 | - | |
89 | - def get_success_url(self): | |
90 | - return reverse_lazy('course:view_topic', kwargs={'slug' : self.object.topic.slug}) | |
99 | +class DeleteLink(LoginRequiredMixin, HasRoleMixin, LogMixin, generic.DeleteView): | |
100 | + log_component = 'link' | |
101 | + log_resource = 'link' | |
102 | + log_action = 'delete' | |
103 | + log_context = {} | |
104 | + | |
105 | + allowed_roles = ['professor', 'system_admin'] | |
106 | + login_url = reverse_lazy("core:home") | |
107 | + redirect_field_name = 'next' | |
108 | + model = Link | |
109 | + template_name = 'links/delete_link.html' | |
110 | + | |
111 | + def dispatch(self, *args, **kwargs): | |
112 | + link = get_object_or_404(Link, slug = self.kwargs.get('slug')) | |
113 | + if(not (link.topic.owner == self.request.user) and not(has_role(self.request.user, 'system_admin')) ): | |
114 | + return self.handle_no_permission() | |
115 | + return super(DeleteLink, self).dispatch(*args, **kwargs) | |
116 | + | |
117 | + def get_context_data(self, **kwargs): | |
118 | + context = super(DeleteLink, self).get_context_data(**kwargs) | |
119 | + context['course'] = self.object.topic.subject.course | |
120 | + context['subject'] = self.object.topic.subject | |
121 | + context['link'] = self.object | |
122 | + context["topic"] = self.object.topic | |
123 | + return context | |
124 | + | |
125 | + def get_success_url(self): | |
126 | + self.log_context['link_id'] = self.object.id | |
127 | + self.log_context['link_name'] = self.object.name | |
128 | + self.log_context['topic_id'] = self.object.topic.id | |
129 | + self.log_context['topic_name'] = self.object.topic.name | |
130 | + self.log_context['topic_slug'] = self.object.topic.slug | |
131 | + self.log_context['subject_id'] = self.object.topic.subject.id | |
132 | + self.log_context['subject_name'] = self.object.topic.subject.name | |
133 | + self.log_context['subject_slug'] = self.object.topic.subject.slug | |
134 | + self.log_context['course_id'] = self.object.topic.subject.course.id | |
135 | + self.log_context['course_name'] = self.object.topic.subject.course.name | |
136 | + self.log_context['course_slug'] = self.object.topic.subject.course.slug | |
137 | + self.log_context['course_category_id'] = self.object.topic.subject.course.category.id | |
138 | + self.log_context['course_category_name'] = self.object.topic.subject.course.category.name | |
139 | + | |
140 | + super(DeleteLink, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
141 | + | |
142 | + return reverse_lazy('course:view_topic', kwargs={'slug' : self.object.topic.slug}) | |
91 | 143 | |
92 | 144 | def render_link(request, slug): |
93 | 145 | template_name = 'links/render_link.html' |
... | ... | @@ -97,7 +149,12 @@ def render_link(request, slug): |
97 | 149 | return render(request, template_name, context) |
98 | 150 | |
99 | 151 | #Referencia no delete link para adicionar quando resolver o problema do context {% url 'course:delete' link.name %} |
100 | -class UpdateLink(LoginRequiredMixin, HasRoleMixin, generic.UpdateView): | |
152 | +class UpdateLink(LoginRequiredMixin, HasRoleMixin, LogMixin, generic.UpdateView): | |
153 | + log_component = 'link' | |
154 | + log_resource = 'link' | |
155 | + log_action = 'update' | |
156 | + log_context = {} | |
157 | + | |
101 | 158 | allowed_roles = ['professor', 'system_admin'] |
102 | 159 | template_name = 'links/update_link.html' |
103 | 160 | form_class = UpdateLinkForm |
... | ... | @@ -108,6 +165,7 @@ class UpdateLink(LoginRequiredMixin, HasRoleMixin, generic.UpdateView): |
108 | 165 | context.status_code = 400 |
109 | 166 | |
110 | 167 | return context |
168 | + | |
111 | 169 | def form_valid(self, form): |
112 | 170 | formulario = form |
113 | 171 | if formulario.has_changed(): |
... | ... | @@ -118,10 +176,28 @@ class UpdateLink(LoginRequiredMixin, HasRoleMixin, generic.UpdateView): |
118 | 176 | self.caminho = 'links/static/images/%s'%(self.link.slug)+'%s'%(self.formato) |
119 | 177 | self.setImage() |
120 | 178 | else: |
121 | - form.save() | |
179 | + self.object = form.save() | |
122 | 180 | else: |
123 | - form.save() | |
181 | + self.object = form.save() | |
182 | + | |
183 | + self.log_context['link_id'] = self.object.id | |
184 | + self.log_context['link_name'] = self.object.name | |
185 | + self.log_context['topic_id'] = self.object.topic.id | |
186 | + self.log_context['topic_name'] = self.object.topic.name | |
187 | + self.log_context['topic_slug'] = self.object.topic.slug | |
188 | + self.log_context['subject_id'] = self.object.topic.subject.id | |
189 | + self.log_context['subject_name'] = self.object.topic.subject.name | |
190 | + self.log_context['subject_slug'] = self.object.topic.subject.slug | |
191 | + self.log_context['course_id'] = self.object.topic.subject.course.id | |
192 | + self.log_context['course_name'] = self.object.topic.subject.course.name | |
193 | + self.log_context['course_slug'] = self.object.topic.subject.course.slug | |
194 | + self.log_context['course_category_id'] = self.object.topic.subject.course.category.id | |
195 | + self.log_context['course_category_name'] = self.object.topic.subject.course.category.name | |
196 | + | |
197 | + super(UpdateLink, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
198 | + | |
124 | 199 | return super(UpdateLink, self).form_valid(form) |
200 | + | |
125 | 201 | def setImage(self): |
126 | 202 | if self.baixado: |
127 | 203 | with open(self.caminho,'rb') as f: |
... | ... | @@ -138,19 +214,46 @@ class UpdateLink(LoginRequiredMixin, HasRoleMixin, generic.UpdateView): |
138 | 214 | def get_success_url(self): |
139 | 215 | self.success_url = reverse_lazy('course:links:render_link', args = (self.object.slug, )) |
140 | 216 | return self.success_url |
141 | -class ViewLink(LoginRequiredMixin,HasRoleMixin,generic.DetailView): | |
217 | + | |
218 | +class ViewLink(LoginRequiredMixin, HasRoleMixin, LogMixin, generic.DetailView): | |
219 | + log_component = 'link' | |
220 | + log_resource = 'link' | |
221 | + log_action = 'viewed' | |
222 | + log_context = {} | |
223 | + | |
142 | 224 | allowed_roles = ['professor', 'system_admin'] |
143 | 225 | template_name = 'links/view_link.html' |
144 | 226 | success_url = reverse_lazy('course:links:render_link') |
145 | 227 | context_object_name = 'link' |
228 | + | |
146 | 229 | def get_context_data(self,**kwargs): |
147 | 230 | context = {} |
148 | 231 | link = Link.objects.get(slug = self.kwargs.get('slug')) |
149 | 232 | context['link'] = link |
233 | + | |
234 | + self.log_context['link_id'] = link.id | |
235 | + self.log_context['link_name'] = link.name | |
236 | + self.log_context['topic_id'] = link.topic.id | |
237 | + self.log_context['topic_name'] = link.topic.name | |
238 | + self.log_context['topic_slug'] = link.topic.slug | |
239 | + self.log_context['subject_id'] = link.topic.subject.id | |
240 | + self.log_context['subject_name'] = link.topic.subject.name | |
241 | + self.log_context['subject_slug'] = link.topic.subject.slug | |
242 | + self.log_context['course_id'] = link.topic.subject.course.id | |
243 | + self.log_context['course_name'] = link.topic.subject.course.name | |
244 | + self.log_context['course_slug'] = link.topic.subject.course.slug | |
245 | + self.log_context['course_category_id'] = link.topic.subject.course.category.id | |
246 | + self.log_context['course_category_name'] = link.topic.subject.course.category.name | |
247 | + | |
248 | + super(ViewLink, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
249 | + | |
150 | 250 | return context |
251 | + | |
151 | 252 | def get_success_url(self): |
152 | 253 | self.success_url = redirect('course:links:render_link', slug = self.object.slug) |
254 | + | |
153 | 255 | return self.success_url |
256 | + | |
154 | 257 | def get_queryset(self): |
155 | 258 | self.queryset = Link.objects.filter(slug = self.kwargs.get('slug')) |
156 | 259 | return self.queryset | ... | ... |