add delete_blog button

This commit is contained in:
DELL 2022-10-08 09:35:21 +07:00
parent 43ffca160b
commit 9d42082b52
3 changed files with 105 additions and 49 deletions

View file

@ -1,8 +1,10 @@
from ast import Delete, arg
from itertools import chain from itertools import chain
from django import forms from django import forms
from django.conf import settings from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import User
from django.core.cache import cache from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key from django.core.cache.utils import make_template_fragment_key
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
@ -17,7 +19,7 @@ from django.http import (
HttpResponseBadRequest, HttpResponseBadRequest,
) )
from django.shortcuts import get_object_or_404 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 import timezone
from django.utils.html import format_html from django.utils.html import format_html
from django.utils.functional import cached_property from django.utils.functional import cached_property
@ -30,6 +32,7 @@ from django.views.generic import (
UpdateView, UpdateView,
View, View,
CreateView, CreateView,
DeleteView,
) )
from django.views.generic.detail import ( from django.views.generic.detail import (
SingleObjectMixin, SingleObjectMixin,
@ -131,7 +134,7 @@ class OrganizationMixin(OrganizationBase):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
try: try:
self.organization_id = int(kwargs["pk"]) self.organization_id = int(kwargs["pk"] )
self.organization = get_object_or_404(Organization, id=self.organization_id) self.organization = get_object_or_404(Organization, id=self.organization_id)
except Http404: except Http404:
key = kwargs.get(self.slug_url_kwarg, None) key = kwargs.get(self.slug_url_kwarg, None)
@ -930,8 +933,7 @@ class EditOrganizationContest(
problem_form.save() problem_form.save()
for problem_form in problem_formset.deleted_objects: for problem_form in problem_formset.deleted_objects:
problem_form.delete() problem_form.delete()
super().post(request, *args, **kwargs) return super().post(request, *args, **kwargs)
return HttpResponseRedirect(reverse("organization_contests", args=(self.organization_id,self.organization.slug)))
self.object = self.contest self.object = self.contest
return self.render_to_response( return self.render_to_response(
@ -1037,7 +1039,7 @@ class EditOrganizationBlog(
MemberOrganizationMixin, MemberOrganizationMixin,
UpdateView, UpdateView,
): ):
template_name = "organization/blog/add.html" template_name = "organization/blog/edit.html"
model = BlogPost model = BlogPost
def get_form_class(self): def get_form_class(self):
@ -1063,6 +1065,10 @@ class EditOrganizationBlog(
_("Not allowed to edit this blog"), _("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): def get(self, request, *args, **kwargs):
res = self.setup_blog(request, *args, **kwargs) res = self.setup_blog(request, *args, **kwargs)
if res: if res:
@ -1073,6 +1079,12 @@ class EditOrganizationBlog(
res = self.setup_blog(request, *args, **kwargs) res = self.setup_blog(request, *args, **kwargs)
if res: if res:
return res return res
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) return super().post(request, *args, **kwargs)
def get_object(self): def get_object(self):
@ -1081,31 +1093,35 @@ class EditOrganizationBlog(
def get_title(self): def get_title(self):
return _("Edit blog %s") % self.object.title return _("Edit blog %s") % self.object.title
def form_valid(self, form): def create_notification(self,action):
with transaction.atomic(), revisions.create_revision(): blog = BlogPost.objects.get(pk=self.blog_id)
res = super(EditOrganizationBlog, self).form_valid(form)
revisions.set_comment(_("Edited from site"))
revisions.set_user(self.request.user)
link = reverse( link = reverse(
"edit_organization_blog", "edit_organization_blog",
args=[self.organization.id, self.organization.slug, self.object.id], args=[self.organization.id, self.organization.slug, self.blog_id],
) )
html = ( html = (
f'<a href="{link}">{self.object.title} - {self.organization.name}</a>' f'<a href="{link}">{blog.title} - {self.organization.name}</a>'
) )
for user in self.organization.admins.all(): 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: if user.id == self.request.profile.id:
continue continue
notification = Notification( notification = Notification(
owner=user, owner=user,
author=self.request.profile, author=self.request.profile,
category="Edit blog", category= action,
html_link=html, html_link=html,
) )
notification.save() notification.save()
return res
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)
self.create_notification("Edit blog")
return res
class PendingBlogs( class PendingBlogs(
LoginRequiredMixin, LoginRequiredMixin,

View file

@ -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 %}
<form action="" method="post">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger alert-dismissable">
<a href="#" class="close">x</a>
{{ form.non_field_errors() }}
{{ form.errors }}
</div>
{% endif %}
{% for field in form %}
{% if not field.is_hidden %}
<div style="margin-bottom: 1em;">
{{ field.errors }}
<label for="{{field.id_for_label }}"><b>{{ field.label }}{% if field.field.required %}<span style="color:red"> * </span>{% endif %}:</b> </label>
<div class="org-field-wrapper" id="org-field-wrapper-{{field.html_name }}">
{{ field }}
</div>
{% if field.help_text %}
<i style="display: block">{{ field.help_text|safe }}</i>
{% endif %}
</div>
{% endif %}
{% endfor %}
<button type="submit" style="display: inline;" name="action" value="Save" >{{ _('Save') }} </button>
<button type="submit" style="background-color: red; float: right;" name="action" value="Delete" > {{ _('Delete') }} </button>
</form>
{% endblock %}

View file

@ -21,5 +21,6 @@
</div> </div>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
<button type="submit">{{ _('Save') }}</button> <button type="submit" style="display: inline;" name="action" value="Save" >{{ _('Save') }} </button>
<!-- <button type="submit" style="background-color: red; float: right;" name="action" value="Delete" > {{ _('Delete') }} </button> -->
</form> </form>