From 515d5bb237b428c08a4fbc4800a3c13aff8bb7ec Mon Sep 17 00:00:00 2001
From: Mouse Reeve
Date: Wed, 19 May 2021 14:55:01 -0700
Subject: [PATCH] Create announcements admin view
---
bookwyrm/forms.py | 6 ++
bookwyrm/templates/settings/admin_layout.html | 4 +
.../templates/settings/announcement_form.html | 80 +++++++++++++++++++
.../templates/settings/announcements.html | 49 ++++++++++++
bookwyrm/urls.py | 10 +++
bookwyrm/views/__init__.py | 1 +
bookwyrm/views/announcements.py | 36 +++++++++
7 files changed, 186 insertions(+)
create mode 100644 bookwyrm/templates/settings/announcement_form.html
create mode 100644 bookwyrm/templates/settings/announcements.html
create mode 100644 bookwyrm/views/announcements.py
diff --git a/bookwyrm/forms.py b/bookwyrm/forms.py
index b6197f33a..25b72a119 100644
--- a/bookwyrm/forms.py
+++ b/bookwyrm/forms.py
@@ -269,6 +269,12 @@ class SiteForm(CustomForm):
exclude = []
+class AnnouncementForm(CustomForm):
+ class Meta:
+ model = models.Announcement
+ exclude = ["remote_id"]
+
+
class ListForm(CustomForm):
class Meta:
model = models.List
diff --git a/bookwyrm/templates/settings/admin_layout.html b/bookwyrm/templates/settings/admin_layout.html
index 4f71a2284..635e86072 100644
--- a/bookwyrm/templates/settings/admin_layout.html
+++ b/bookwyrm/templates/settings/admin_layout.html
@@ -43,6 +43,10 @@
{% if perms.bookwyrm.edit_instance_settings %}
+
+
+
+
+
+ {% for error in form.start_date.errors %}
+
{{ error | escape }}
+ {% endfor %}
+
+
+
+
+
+
+ {% for error in form.end_date.errors %}
+
{{ error | escape }}
+ {% endfor %}
+
+
+
+
+
+ {{ form.active }}
+ {% for error in form.active.errors %}
+
{{ error | escape }}
+ {% endfor %}
+
+
+
+
+
+
+ {{ form.preview }}
+ {% for error in form.preview.errors %}
+
{{ error | escape }}
+ {% endfor %}
+
+
+
+ {{ form.content }}
+ {% for error in form.content.errors %}
+
{{ error | escape }}
+ {% endfor %}
+
+
+
+
+ {% for error in form.event_date.errors %}
+
{{ error | escape }}
+ {% endfor %}
+
+
+
+
+
+
+
+
+{% endblock %}
+
diff --git a/bookwyrm/templates/settings/announcements.html b/bookwyrm/templates/settings/announcements.html
new file mode 100644
index 000000000..009dcee07
--- /dev/null
+++ b/bookwyrm/templates/settings/announcements.html
@@ -0,0 +1,49 @@
+{% extends 'settings/admin_layout.html' %}
+{% load i18n %}{% load humanize %}
+{% block title %}{% trans "Announcements" %}{% endblock %}
+
+{% block header %}{% trans "Announcements" %}{% endblock %}
+
+{% block edit-button %}
+{% trans "Create Announcement" as button_text %}
+{% include 'snippets/toggle/open_button.html' with controls_text="create-announcement" icon_with_text="plus" text=button_text focus="create-announcement-header" %}
+
+{% endblock %}
+
+{% block panel %}
+
+ {% include 'settings/announcement_form.html' with controls_text="create-announcement" %}
+
+
+
+
+ {% url 'settings-announcements' as url %}
+
+ {% trans "Name" as text %}
+ {% include 'snippets/table-sort-header.html' with field="name" sort=sort text=text %}
+ |
+
+ {% trans "Start date" as text %}
+ {% include 'snippets/table-sort-header.html' with field="start_date" sort=sort text=text %}
+ |
+
+ {% trans "End date" as text %}
+ {% include 'snippets/table-sort-header.html' with field="end_date" sort=sort text=text %}
+ |
+
+ {% trans "Status" as text %}
+ {% include 'snippets/table-sort-header.html' with field="active" sort=sort text=text %}
+ |
+
+ {% for announcement in announcements %}
+
+ {{ announcement.name }} |
+ {{ announcement.start_date|naturaltime|default:'' }} |
+ {{ announcement.end_date|naturaltime|default:'' }} |
+ {% if announcement.active %}{% trans "active" %}{% else %}{% trans "inactive" %}{% endif %} |
+
+ {% endfor %}
+
+
+{% include 'snippets/pagination.html' with page=servers path=request.path %}
+{% endblock %}
diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py
index 3fdced387..92f2c4985 100644
--- a/bookwyrm/urls.py
+++ b/bookwyrm/urls.py
@@ -56,6 +56,16 @@ urlpatterns = [
),
# admin
re_path(r"^settings/site-settings/?$", views.Site.as_view(), name="settings-site"),
+ re_path(
+ r"^settings/announcements/?$",
+ views.Announcements.as_view(),
+ name="settings-announcements",
+ ),
+ re_path(
+ r"^settings/announcements/(?P\d+)/??$",
+ views.Announcements.as_view(),
+ name="settings-announcements",
+ ),
re_path(
r"^settings/email-preview/?$",
views.site.email_preview,
diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py
index bcd914e10..99fedf366 100644
--- a/bookwyrm/views/__init__.py
+++ b/bookwyrm/views/__init__.py
@@ -1,4 +1,5 @@
""" make sure all our nice views are available """
+from .announcements import Announcements
from .authentication import Login, Register, Logout
from .author import Author, EditAuthor
from .block import Block, unblock
diff --git a/bookwyrm/views/announcements.py b/bookwyrm/views/announcements.py
new file mode 100644
index 000000000..318cf8569
--- /dev/null
+++ b/bookwyrm/views/announcements.py
@@ -0,0 +1,36 @@
+""" make announcements """
+from django.contrib.auth.decorators import login_required, permission_required
+from django.template.response import TemplateResponse
+from django.utils.decorators import method_decorator
+from django.views import View
+
+from bookwyrm import forms, models
+
+
+# pylint: disable= no-self-use
+@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"""
+ data = {
+ "announcements": models.Announcement.objects.all(),
+ "form": forms.AnnouncementForm(),
+ }
+ 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()
+ data = {
+ "announcements": models.Announcement.objects.all(),
+ "form": forms.AnnouncementForm(),
+ }
+ return TemplateResponse(request, "settings/announcements.html", data)