2021-05-19 21:55:01 +00:00
|
|
|
""" make announcements """
|
|
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
2021-05-20 16:43:29 +00:00
|
|
|
from django.core.paginator import Paginator
|
2021-05-20 19:08:12 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2021-05-19 21:55:01 +00:00
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views import View
|
|
|
|
|
|
|
|
from bookwyrm import forms, models
|
2021-05-20 16:43:29 +00:00
|
|
|
from bookwyrm.settings import PAGE_LENGTH
|
2021-05-19 21:55:01 +00:00
|
|
|
|
|
|
|
|
2021-05-20 16:43:29 +00:00
|
|
|
# pylint: disable=no-self-use
|
2021-05-19 21:55:01 +00:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
@method_decorator(
|
|
|
|
permission_required("bookwyrm.edit_instance_settings", raise_exception=True),
|
|
|
|
name="dispatch",
|
|
|
|
)
|
|
|
|
class Announcements(View):
|
|
|
|
"""tell everyone"""
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
"""view and create announcements"""
|
2021-05-20 16:43:29 +00:00
|
|
|
announcements = models.Announcement.objects
|
|
|
|
|
|
|
|
sort = request.GET.get("sort", "-created_date")
|
|
|
|
sort_fields = [
|
|
|
|
"created_date",
|
|
|
|
"preview",
|
|
|
|
"start_date",
|
|
|
|
"end_date",
|
|
|
|
"active",
|
|
|
|
]
|
|
|
|
if sort in sort_fields + ["-{:s}".format(f) for f in sort_fields]:
|
|
|
|
announcements = announcements.order_by(sort)
|
2021-05-19 21:55:01 +00:00
|
|
|
data = {
|
2021-05-20 16:43:29 +00:00
|
|
|
"announcements": Paginator(announcements, PAGE_LENGTH).get_page(
|
|
|
|
request.GET.get("page")
|
|
|
|
),
|
2021-05-19 21:55:01 +00:00
|
|
|
"form": forms.AnnouncementForm(),
|
2021-05-20 16:43:29 +00:00
|
|
|
"sort": sort,
|
2021-05-19 21:55:01 +00:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, "settings/announcements.html", data)
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
"""edit the site settings"""
|
|
|
|
form = forms.AnnouncementForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
2021-05-20 15:09:35 +00:00
|
|
|
# reset the create form
|
|
|
|
form = forms.AnnouncementForm()
|
2021-05-19 21:55:01 +00:00
|
|
|
data = {
|
2021-05-20 16:43:29 +00:00
|
|
|
"announcements": Paginator(
|
2021-05-20 19:36:42 +00:00
|
|
|
models.Announcement.objects.all(), PAGE_LENGTH
|
2021-05-20 16:43:29 +00:00
|
|
|
).get_page(request.GET.get("page")),
|
2021-05-20 15:09:35 +00:00
|
|
|
"form": form,
|
2021-05-19 21:55:01 +00:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, "settings/announcements.html", data)
|
2021-05-20 15:09:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
@method_decorator(
|
|
|
|
permission_required("bookwyrm.edit_instance_settings", raise_exception=True),
|
|
|
|
name="dispatch",
|
|
|
|
)
|
|
|
|
class Announcement(View):
|
2021-05-20 19:08:12 +00:00
|
|
|
"""edit an announcement"""
|
2021-05-20 15:09:35 +00:00
|
|
|
|
|
|
|
def get(self, request, announcement_id):
|
|
|
|
"""view announcement"""
|
|
|
|
announcement = get_object_or_404(models.Announcement, id=announcement_id)
|
|
|
|
data = {
|
|
|
|
"announcement": announcement,
|
|
|
|
"form": forms.AnnouncementForm(instance=announcement),
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, "settings/announcement.html", data)
|
|
|
|
|
|
|
|
def post(self, request, announcement_id):
|
2021-05-20 19:08:12 +00:00
|
|
|
"""edit announcement"""
|
2021-05-20 15:09:35 +00:00
|
|
|
announcement = get_object_or_404(models.Announcement, id=announcement_id)
|
|
|
|
form = forms.AnnouncementForm(request.POST, instance=announcement)
|
|
|
|
if form.is_valid():
|
2021-05-20 15:56:17 +00:00
|
|
|
announcement = form.save()
|
2021-05-21 03:21:04 +00:00
|
|
|
form = forms.AnnouncementForm(instance=announcement)
|
2021-05-20 15:09:35 +00:00
|
|
|
data = {
|
2021-05-20 15:56:17 +00:00
|
|
|
"announcement": announcement,
|
2021-05-20 15:09:35 +00:00
|
|
|
"form": form,
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, "settings/announcement.html", data)
|
2021-05-20 19:08:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@permission_required("bookwyrm.edit_instance_settings", raise_exception=True)
|
|
|
|
def delete_announcement(_, announcement_id):
|
|
|
|
"""delete announcement"""
|
|
|
|
announcement = get_object_or_404(models.Announcement, id=announcement_id)
|
|
|
|
announcement.delete()
|
|
|
|
return redirect("settings-announcements")
|