Commit a9f9b294d80b16a0a097b543c54c062396d592e6
1 parent
d1ed30df
Exists in
master
and in
5 other branches
Adding crud post log functions [Issue: #243]
Showing
3 changed files
with
82 additions
and
5 deletions
Show diff stats
forum/static/js/forum.js
@@ -223,6 +223,8 @@ function delete_post(url, post) { | @@ -223,6 +223,8 @@ function delete_post(url, post) { | ||
223 | }, | 223 | }, |
224 | url: url, | 224 | url: url, |
225 | success: function(data) { | 225 | success: function(data) { |
226 | + alertify.success(data); | ||
227 | + | ||
226 | $("#post_"+post).remove(); | 228 | $("#post_"+post).remove(); |
227 | } | 229 | } |
228 | }); | 230 | }); |
forum/templates/post/post_render.html
@@ -8,7 +8,7 @@ | @@ -8,7 +8,7 @@ | ||
8 | <a href="javascript:answer('{{ post.id }}', '{% url 'course:forum:reply_post' %}');"> | 8 | <a href="javascript:answer('{{ post.id }}', '{% url 'course:forum:reply_post' %}');"> |
9 | <i class="material-icons">{% trans 'reply' %}</i> | 9 | <i class="material-icons">{% trans 'reply' %}</i> |
10 | </a> | 10 | </a> |
11 | - {% if request.user|has_role:'system_admin' or request.user|has_role:'professor' and request.user == post.user %} | 11 | + {% if request.user|has_role:'system_admin' or request.user == post.user %} |
12 | {% csrf_token %} | 12 | {% csrf_token %} |
13 | <div class="btn-group icon-more-horiz"> | 13 | <div class="btn-group icon-more-horiz"> |
14 | <a class="btn btn-default btn-xs dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> | 14 | <a class="btn btn-default btn-xs dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> |
forum/views.py
@@ -282,7 +282,12 @@ def load_posts(request, forum_id): | @@ -282,7 +282,12 @@ def load_posts(request, forum_id): | ||
282 | 282 | ||
283 | return JsonResponse({'num_pages': paginator.num_pages, 'page': page_obj.number, 'btn_text': _('Load more posts'), 'html': html}) | 283 | return JsonResponse({'num_pages': paginator.num_pages, 'page': page_obj.number, 'btn_text': _('Load more posts'), 'html': html}) |
284 | 284 | ||
285 | -class CreatePostView(LoginRequiredMixin, generic.edit.CreateView, NotificationMixin): | 285 | +class CreatePostView(LoginRequiredMixin, generic.edit.CreateView, LogMixin, NotificationMixin): |
286 | + log_component = "forum" | ||
287 | + log_action = "create" | ||
288 | + log_resource = "post" | ||
289 | + log_context = {} | ||
290 | + | ||
286 | login_url = reverse_lazy("core:home") | 291 | login_url = reverse_lazy("core:home") |
287 | redirect_field_name = 'next' | 292 | redirect_field_name = 'next' |
288 | 293 | ||
@@ -298,6 +303,23 @@ class CreatePostView(LoginRequiredMixin, generic.edit.CreateView, NotificationMi | @@ -298,6 +303,23 @@ class CreatePostView(LoginRequiredMixin, generic.edit.CreateView, NotificationMi | ||
298 | resource_slug = self.object.forum.slug, actor=self.request.user, users= self.object.forum.topic.subject.students.all(), | 303 | resource_slug = self.object.forum.slug, actor=self.request.user, users= self.object.forum.topic.subject.students.all(), |
299 | resource_link = reverse('course:forum:view', args=[self.object.forum.slug])) | 304 | resource_link = reverse('course:forum:view', args=[self.object.forum.slug])) |
300 | 305 | ||
306 | + self.log_context['post_id'] = self.object.id | ||
307 | + self.log_context['forum_id'] = self.object.forum.id | ||
308 | + self.log_context['forum_name'] = self.object.forum.name | ||
309 | + self.log_context['topic_id'] = self.object.forum.topic.id | ||
310 | + self.log_context['topic_name'] = self.object.forum.topic.name | ||
311 | + self.log_context['topic_slug'] = self.object.forum.topic.slug | ||
312 | + self.log_context['subject_id'] = self.object.forum.topic.subject.id | ||
313 | + self.log_context['subject_name'] = self.object.forum.topic.subject.name | ||
314 | + self.log_context['subject_slug'] = self.object.forum.topic.subject.slug | ||
315 | + self.log_context['course_id'] = self.object.forum.topic.subject.course.id | ||
316 | + self.log_context['course_name'] = self.object.forum.topic.subject.course.name | ||
317 | + self.log_context['course_slug'] = self.object.forum.topic.subject.course.slug | ||
318 | + self.log_context['course_category_id'] = self.object.forum.topic.subject.course.category.id | ||
319 | + self.log_context['course_category_name'] = self.object.forum.topic.subject.course.category.name | ||
320 | + | ||
321 | + super(CreatePostView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | ||
322 | + | ||
301 | return super(CreatePostView, self).form_valid(form) | 323 | return super(CreatePostView, self).form_valid(form) |
302 | 324 | ||
303 | def get_success_url(self): | 325 | def get_success_url(self): |
@@ -315,7 +337,12 @@ def render_post(request, post): | @@ -315,7 +337,12 @@ def render_post(request, post): | ||
315 | 337 | ||
316 | return JsonResponse({'new_id': last_post.id, 'html': html}) | 338 | return JsonResponse({'new_id': last_post.id, 'html': html}) |
317 | 339 | ||
318 | -class PostUpdateView(LoginRequiredMixin, generic.UpdateView): | 340 | +class PostUpdateView(LoginRequiredMixin, LogMixin, generic.UpdateView): |
341 | + log_component = "forum" | ||
342 | + log_action = "update" | ||
343 | + log_resource = "post" | ||
344 | + log_context = {} | ||
345 | + | ||
319 | login_url = reverse_lazy("core:home") | 346 | login_url = reverse_lazy("core:home") |
320 | redirect_field_name = 'next' | 347 | redirect_field_name = 'next' |
321 | 348 | ||
@@ -323,18 +350,66 @@ class PostUpdateView(LoginRequiredMixin, generic.UpdateView): | @@ -323,18 +350,66 @@ class PostUpdateView(LoginRequiredMixin, generic.UpdateView): | ||
323 | model = Post | 350 | model = Post |
324 | template_name = "post/post_update_form.html" | 351 | template_name = "post/post_update_form.html" |
325 | 352 | ||
353 | + def form_valid(self, form): | ||
354 | + self.object = form.save() | ||
355 | + | ||
356 | + self.log_context['post_id'] = self.object.id | ||
357 | + self.log_context['forum_id'] = self.object.forum.id | ||
358 | + self.log_context['forum_name'] = self.object.forum.name | ||
359 | + self.log_context['topic_id'] = self.object.forum.topic.id | ||
360 | + self.log_context['topic_name'] = self.object.forum.topic.name | ||
361 | + self.log_context['topic_slug'] = self.object.forum.topic.slug | ||
362 | + self.log_context['subject_id'] = self.object.forum.topic.subject.id | ||
363 | + self.log_context['subject_name'] = self.object.forum.topic.subject.name | ||
364 | + self.log_context['subject_slug'] = self.object.forum.topic.subject.slug | ||
365 | + self.log_context['course_id'] = self.object.forum.topic.subject.course.id | ||
366 | + self.log_context['course_name'] = self.object.forum.topic.subject.course.name | ||
367 | + self.log_context['course_slug'] = self.object.forum.topic.subject.course.slug | ||
368 | + self.log_context['course_category_id'] = self.object.forum.topic.subject.course.category.id | ||
369 | + self.log_context['course_category_name'] = self.object.forum.topic.subject.course.category.name | ||
370 | + | ||
371 | + super(PostUpdateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | ||
372 | + | ||
373 | + return super(PostUpdateView, self).form_valid(form) | ||
374 | + | ||
326 | def get_success_url(self): | 375 | def get_success_url(self): |
327 | self.success_url = reverse('course:forum:render_post', args = (self.object.id, )) | 376 | self.success_url = reverse('course:forum:render_post', args = (self.object.id, )) |
328 | 377 | ||
329 | return self.success_url | 378 | return self.success_url |
330 | 379 | ||
331 | -class PostDeleteView(LoginRequiredMixin, generic.DeleteView): | 380 | +class PostDeleteView(LoginRequiredMixin, LogMixin, generic.DeleteView): |
381 | + log_component = "forum" | ||
382 | + log_action = "delete" | ||
383 | + log_resource = "post" | ||
384 | + log_context = {} | ||
385 | + | ||
332 | login_url = reverse_lazy("core:home") | 386 | login_url = reverse_lazy("core:home") |
333 | redirect_field_name = 'next' | 387 | redirect_field_name = 'next' |
334 | 388 | ||
335 | model = Post | 389 | model = Post |
336 | pk_url_kwarg = 'pk' | 390 | pk_url_kwarg = 'pk' |
337 | - success_url = reverse_lazy('course:forum:deleted_post') | 391 | + |
392 | + def get_success_url(self): | ||
393 | + self.success_url = reverse_lazy('course:forum:deleted_post') | ||
394 | + | ||
395 | + self.log_context['post_id'] = self.object.id | ||
396 | + self.log_context['forum_id'] = self.object.forum.id | ||
397 | + self.log_context['forum_name'] = self.object.forum.name | ||
398 | + self.log_context['topic_id'] = self.object.forum.topic.id | ||
399 | + self.log_context['topic_name'] = self.object.forum.topic.name | ||
400 | + self.log_context['topic_slug'] = self.object.forum.topic.slug | ||
401 | + self.log_context['subject_id'] = self.object.forum.topic.subject.id | ||
402 | + self.log_context['subject_name'] = self.object.forum.topic.subject.name | ||
403 | + self.log_context['subject_slug'] = self.object.forum.topic.subject.slug | ||
404 | + self.log_context['course_id'] = self.object.forum.topic.subject.course.id | ||
405 | + self.log_context['course_name'] = self.object.forum.topic.subject.course.name | ||
406 | + self.log_context['course_slug'] = self.object.forum.topic.subject.course.slug | ||
407 | + self.log_context['course_category_id'] = self.object.forum.topic.subject.course.category.id | ||
408 | + self.log_context['course_category_name'] = self.object.forum.topic.subject.course.category.name | ||
409 | + | ||
410 | + super(PostDeleteView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | ||
411 | + | ||
412 | + return self.success_url | ||
338 | 413 | ||
339 | def post_deleted(request): | 414 | def post_deleted(request): |
340 | return HttpResponse(_("Post deleted successfully.")) | 415 | return HttpResponse(_("Post deleted successfully.")) |