From 9d42082b52e0ac4469747288149e5ef22363b609 Mon Sep 17 00:00:00 2001 From: DELL Date: Sat, 8 Oct 2022 09:35:21 +0700 Subject: [PATCH] add delete_blog button --- judge/views/organization.py | 66 +++++++++++++++++---------- templates/organization/blog/edit.html | 39 ++++++++++++++++ templates/organization/form.html | 49 ++++++++++---------- 3 files changed, 105 insertions(+), 49 deletions(-) create mode 100644 templates/organization/blog/edit.html diff --git a/judge/views/organization.py b/judge/views/organization.py index 53727ca..daebc9f 100644 --- a/judge/views/organization.py +++ b/judge/views/organization.py @@ -1,8 +1,10 @@ +from ast import Delete, arg from itertools import chain from django import forms from django.conf import settings from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin +from django.contrib.auth.models import User from django.core.cache import cache from django.core.cache.utils import make_template_fragment_key from django.core.exceptions import PermissionDenied @@ -17,7 +19,7 @@ from django.http import ( HttpResponseBadRequest, ) from django.shortcuts import get_object_or_404 -from django.urls import reverse +from django.urls import reverse , reverse_lazy from django.utils import timezone from django.utils.html import format_html from django.utils.functional import cached_property @@ -30,6 +32,7 @@ from django.views.generic import ( UpdateView, View, CreateView, + DeleteView, ) from django.views.generic.detail import ( SingleObjectMixin, @@ -131,7 +134,7 @@ class OrganizationMixin(OrganizationBase): def dispatch(self, request, *args, **kwargs): try: - self.organization_id = int(kwargs["pk"]) + self.organization_id = int(kwargs["pk"] ) self.organization = get_object_or_404(Organization, id=self.organization_id) except Http404: key = kwargs.get(self.slug_url_kwarg, None) @@ -930,8 +933,7 @@ class EditOrganizationContest( problem_form.save() for problem_form in problem_formset.deleted_objects: problem_form.delete() - super().post(request, *args, **kwargs) - return HttpResponseRedirect(reverse("organization_contests", args=(self.organization_id,self.organization.slug))) + return super().post(request, *args, **kwargs) self.object = self.contest return self.render_to_response( @@ -1037,7 +1039,7 @@ class EditOrganizationBlog( MemberOrganizationMixin, UpdateView, ): - template_name = "organization/blog/add.html" + template_name = "organization/blog/edit.html" model = BlogPost def get_form_class(self): @@ -1063,6 +1065,10 @@ class EditOrganizationBlog( _("Not allowed to edit this blog"), ) + def delete_blog(self , request , *args , **kwargs): + self.blog_id = kwargs["blog_pk"] + BlogPost.objects.get(pk = self.blog_id).delete() + def get(self, request, *args, **kwargs): res = self.setup_blog(request, *args, **kwargs) if res: @@ -1073,7 +1079,13 @@ class EditOrganizationBlog( res = self.setup_blog(request, *args, **kwargs) if res: return res - return super().post(request, *args, **kwargs) + if request.POST['action'] == 'Delete': + self.create_notification("Delete blog") + self.delete_blog(request , *args , **kwargs) + cur_url = reverse("organization_pending_blogs", args=(self.organization_id,self.organization.slug) ) + return HttpResponseRedirect(cur_url) + else: + return super().post(request, *args, **kwargs) def get_object(self): return self.blog @@ -1081,32 +1093,36 @@ class EditOrganizationBlog( def get_title(self): return _("Edit blog %s") % self.object.title + def create_notification(self,action): + blog = BlogPost.objects.get(pk=self.blog_id) + link = reverse( + "edit_organization_blog", + args=[self.organization.id, self.organization.slug, self.blog_id], + ) + html = ( + f'{blog.title} - {self.organization.name}' + ) + post_authors = blog.authors.all() + posible_user = self.organization.admins.all() | post_authors + for user in posible_user: + if user.id == self.request.profile.id: + continue + notification = Notification( + owner=user, + author=self.request.profile, + category= action, + html_link=html, + ) + notification.save() + def form_valid(self, form): with transaction.atomic(), revisions.create_revision(): res = super(EditOrganizationBlog, self).form_valid(form) revisions.set_comment(_("Edited from site")) revisions.set_user(self.request.user) - - link = reverse( - "edit_organization_blog", - args=[self.organization.id, self.organization.slug, self.object.id], - ) - html = ( - f'{self.object.title} - {self.organization.name}' - ) - for user in self.organization.admins.all(): - if user.id == self.request.profile.id: - continue - notification = Notification( - owner=user, - author=self.request.profile, - category="Edit blog", - html_link=html, - ) - notification.save() + self.create_notification("Edit blog") return res - class PendingBlogs( LoginRequiredMixin, TitleMixin, diff --git a/templates/organization/blog/edit.html b/templates/organization/blog/edit.html new file mode 100644 index 0000000..e2cca8e --- /dev/null +++ b/templates/organization/blog/edit.html @@ -0,0 +1,39 @@ +{% extends "organization/home-base.html" %} + +{% block three_col_js %} + {{ form.media.js }} + {% include "organization/home-js.html" %} +{% endblock %} + +{% block three_col_media %} + {{ form.media.css }} +{% endblock %} + +{% block middle_content %} +
+ {% csrf_token %} + {% if form.errors %} +
+ x + {{ form.non_field_errors() }} + {{ form.errors }} +
+ {% endif %} + {% for field in form %} + {% if not field.is_hidden %} +
+ {{ field.errors }} + +
+ {{ field }} +
+ {% if field.help_text %} + {{ field.help_text|safe }} + {% endif %} +
+ {% endif %} + {% endfor %} + + +
+{% endblock %} \ No newline at end of file diff --git a/templates/organization/form.html b/templates/organization/form.html index f23df3c..0c85003 100644 --- a/templates/organization/form.html +++ b/templates/organization/form.html @@ -1,25 +1,26 @@ -
- {% csrf_token %} - {% if form.errors %} -
- x - {{ form.non_field_errors() }} - {{ form.errors }} -
- {% endif %} - {% for field in form %} - {% if not field.is_hidden %} -
- {{ field.errors }} - -
- {{ field }} -
- {% if field.help_text %} - {{ field.help_text|safe }} - {% endif %} -
- {% endif %} - {% endfor %} - + + {% csrf_token %} + {% if form.errors %} +
+ x + {{ form.non_field_errors() }} + {{ form.errors }} +
+ {% endif %} + {% for field in form %} + {% if not field.is_hidden %} +
+ {{ field.errors }} + +
+ {{ field }} +
+ {% if field.help_text %} + {{ field.help_text|safe }} + {% endif %} +
+ {% endif %} + {% endfor %} + +
\ No newline at end of file