add delete blog

This commit is contained in:
DELL 2022-10-07 10:11:46 +07:00
parent aeab9e8d0e
commit 87e8f3d966
12 changed files with 252 additions and 68 deletions

View file

@ -405,6 +405,10 @@ DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
'USER': 'dmoj',
'PASSWORD': 'YES',
'HOST': 'localhost',
'PORT': '',
},
}

View file

@ -665,6 +665,11 @@ urlpatterns = [
organization.EditOrganizationBlog.as_view(),
name="edit_organization_blog",
),
# url(
# r"^/blog/deleting$",
# organization.PendingBlogs.as_view(),
# name="organization_pending_blogs",
# ),
url(
r"^/blog/pending$",
organization.PendingBlogs.as_view(),

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,14 @@
# Generated by Django 2.2.28 on 2022-09-15 23:36
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('judge', '0132_auto_20220915_1837'),
('judge', '0132_auto_20220915_1349'),
]
operations = [
]

View file

@ -0,0 +1,18 @@
# Generated by Django 2.2.28 on 2022-10-06 16:08
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('judge', '0133_merge_20220916_0636'),
]
operations = [
migrations.AddField(
model_name='blogpost',
name='is_delete',
field=models.BooleanField(default=False, verbose_name='is delete'),
),
]

View file

@ -0,0 +1,17 @@
# Generated by Django 2.2.28 on 2022-10-06 17:03
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('judge', '0134_blogpost_is_delete'),
]
operations = [
migrations.RemoveField(
model_name='blogpost',
name='is_delete',
),
]

View file

@ -1,4 +1,6 @@
from email.policy import default
import re
from tabnanny import verbose
from django.core.exceptions import ValidationError
from django.db import models
@ -96,7 +98,12 @@ class BlogPost(models.Model):
return self.title
def get_absolute_url(self):
return reverse("blog_post", args=(self.id, self.slug))
try:
BlogPost.objects.get(id=self.id)
return reverse("blog_post", args=(self.id , self.slug))
except BlogPost.DoesNotExist:
return reverse("organization_pending_blogs",
args=(self.organizations.first().pk, self.organizations.first().slug))
def can_see(self, user):
if self.visible and self.publish_on <= timezone.now():

View file

@ -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,
@ -126,12 +129,11 @@ class OrganizationMixin(OrganizationBase):
context["logo_override_image"] = self.organization.logo_override_image
if "organizations" in context:
context.pop("organizations")
print(context)
return context
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)
@ -432,7 +434,6 @@ class OrganizationContests(
self.set_editable_contest(contest)
for contest in context["future_contests"]:
self.set_editable_contest(contest)
print(context)
return context
@ -1026,7 +1027,7 @@ class EditOrganizationBlog(
MemberOrganizationMixin,
UpdateView,
):
template_name = "organization/blog/add.html"
template_name = "organization/blog/edit.html"
model = BlogPost
def get_form_class(self):
@ -1052,6 +1053,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:
@ -1062,7 +1067,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
@ -1070,32 +1081,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'<a href="{link}">{blog.title} - {self.organization.name}</a>'
)
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'<a href="{link}">{self.object.title} - {self.organization.name}</a>'
)
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,

View file

@ -1,40 +1,86 @@
Django>=2.2,<3
django_compressor
django-mptt
django-pagedown
django-registration-redux
django-reversion
django-reversion-compare
django-social-share
django-sortedm2m @ git+https://github.com/DMOJ/django-sortedm2m.git
django-impersonate
dmoj-wpadmin @ git+https://github.com/LQDJudge/dmoj-wpadmin.git
lxml
Pygments
mistune<2
social-auth-app-django
pytz
django-statici18n
pika
ua-parser
pyyaml
jinja2
django_jinja
llist
requests
django-fernet-fields
pyotp
qrcode[pil]
jsonfield
pymoss
packaging
celery
ansi2html @ git+https://github.com/DMOJ/ansi2html.git
sqlparse
django-newsletter
netaddr
redis
lupa
websocket-client
python-memcached
numpy
amqp==5.1.1
ansi2html @ git+https://github.com/DMOJ/ansi2html.git@1791b4a468942eba19cb29242d9075f5e4284450
async-timeout==4.0.2
billiard==3.6.4.0
celery==5.2.7
certifi==2022.6.15.1
cffi==1.15.1
chardet==5.0.0
charset-normalizer==2.1.1
click==8.1.3
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.2.0
cryptography==38.0.1
defusedxml==0.7.1
Deprecated==1.2.13
diff-match-patch==20200713
Django==2.2.28
django-appconf==1.0.5
django-compressor==4.1
django-fernet-fields==0.6
django-impersonate==1.8.2
django-jinja==2.10.2
django-js-asset==2.0.0
django-mptt==0.13.4
django-newsletter==0.9.1
django-pagedown==2.2.1
django-registration-redux==2.11
django-reversion==5.0.2
django-reversion-compare==0.15.0
django-social-share==2.3.0
django-sortedm2m==2.0.0
django-statici18n==2.3.1
dmoj-wpadmin==1.10.2
Flask==2.2.2
idna==3.3
itsdangerous==2.1.2
Jinja2==3.1.2
jsonfield==3.1.0
kombu==5.2.4
ldif3==3.1.1
llist==0.7.1
lupa==1.13
lxml==4.9.1
MarkupSafe==2.1.1
mistune==0.8.4
mysqlclient==2.1.1
netaddr==0.8.0
numpy==1.23.3
oauthlib==3.2.1
packaging==21.3
pika==1.3.0
Pillow==9.2.0
prompt-toolkit==3.0.31
pycparser==2.21
Pygments==2.13.0
PyJWT==2.4.0
pymoss==0.2
pyotp==2.7.0
pyparsing==3.0.9
python-card-me==0.9.3
python-dateutil==2.8.2
python-memcached==1.59
python3-openid==3.2.0
pytz==2022.2.1
PyYAML==6.0
qrcode==7.3.1
rcssmin==1.1.0
redis==4.3.4
requests==2.28.1
requests-oauthlib==1.3.1
rjsmin==1.2.0
six==1.16.0
social-auth-app-django==5.0.0
social-auth-core==4.3.0
sorl-thumbnail==12.9.0
sqlparse==0.4.2
surlex==0.2.0
ua-parser==0.16.1
unicodecsv==0.14.1
urllib3==1.26.12
vine==5.0.0
wcwidth==0.2.5
websocket-client==1.4.1
Werkzeug==2.2.2
wrapt==1.14.1

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>
{% endif %}
{% 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>

View file

@ -10,7 +10,7 @@
{% block users_media %}{% endblock %}
{% endblock %}
{% block body %}
{% block body %}z
<div id="common-content">
<div id="content-left" class="users">
{% if page_obj and page_obj.num_pages > 1 %}