diff --git a/bookwyrm/migrations/0075_announcement.py b/bookwyrm/migrations/0075_announcement.py index 7808eea1a..b667c2627 100644 --- a/bookwyrm/migrations/0075_announcement.py +++ b/bookwyrm/migrations/0075_announcement.py @@ -1,4 +1,4 @@ -# Generated by Django 3.2 on 2021-05-19 22:16 +# Generated by Django 3.2 on 2021-05-20 19:34 import bookwyrm.models.fields from django.conf import settings @@ -36,7 +36,7 @@ class Migration(migrations.Migration): ), ), ("preview", models.CharField(max_length=255)), - ("content", models.TextField()), + ("content", models.TextField(blank=True, null=True)), ("event_date", models.DateTimeField(blank=True, null=True)), ("start_date", models.DateTimeField(blank=True, null=True)), ("end_date", models.DateTimeField(blank=True, null=True)), diff --git a/bookwyrm/models/announcement.py b/bookwyrm/models/announcement.py index 987b06c2d..498d50418 100644 --- a/bookwyrm/models/announcement.py +++ b/bookwyrm/models/announcement.py @@ -11,7 +11,7 @@ class Announcement(BookWyrmModel): user = models.ForeignKey("User", on_delete=models.PROTECT) preview = models.CharField(max_length=255) - content = models.TextField() + content = models.TextField(null=True, blank=True) event_date = models.DateTimeField(blank=True, null=True) start_date = models.DateTimeField(blank=True, null=True) end_date = models.DateTimeField(blank=True, null=True) diff --git a/bookwyrm/templates/snippets/announcement.html b/bookwyrm/templates/snippets/announcement.html index ee00cfd72..a6a7c65b3 100644 --- a/bookwyrm/templates/snippets/announcement.html +++ b/bookwyrm/templates/snippets/announcement.html @@ -11,18 +11,22 @@ {% endif %} {{ announcement.preview }} + {% if announcement.content %}
{% blocktrans with user_path=announcement.user.local_path username=announcement.user.display_name %}Posted by {{ username }}{% endblocktrans %}
{% if not admin_mode %} diff --git a/bookwyrm/tests/views/test_announcements.py b/bookwyrm/tests/views/test_announcements.py index d9999d5ac..a9a9423da 100644 --- a/bookwyrm/tests/views/test_announcements.py +++ b/bookwyrm/tests/views/test_announcements.py @@ -51,9 +51,11 @@ class AnnouncementViews(TestCase): def test_announcement_page(self): """there are so many views, this just makes sure it LOADS""" - announcement = models.Announcement.objects.create(preview="hi", user=self.local_user) + announcement = models.Announcement.objects.create( + preview="hi", user=self.local_user + ) - view = views.Announcements.as_view() + view = views.Announcement.as_view() request = self.factory.get("") request.user = self.local_user request.user.is_superuser = True @@ -63,3 +65,72 @@ class AnnouncementViews(TestCase): self.assertIsInstance(result, TemplateResponse) result.render() self.assertEqual(result.status_code, 200) + + def test_create_announcement(self): + """create a new announcement""" + view = views.Announcements.as_view() + form = forms.AnnouncementForm() + form.data["preview"] = "hi hi" + form.data["start_date"] = "2021-05-20" + form.data["user"] = self.local_user.id + + request = self.factory.post("", form.data) + request.user = self.local_user + request.user.is_superuser = True + + view(request) + + announcement = models.Announcement.objects.get() + self.assertEqual(announcement.preview, "hi hi") + self.assertEqual(announcement.start_date.year, 2021) + self.assertEqual(announcement.start_date.month, 5) + self.assertEqual(announcement.start_date.day, 20) + + def test_edit_announcement(self): + """edit an announcement""" + announcement = models.Announcement.objects.create( + preview="hi", user=self.local_user + ) + view = views.Announcement.as_view() + form = forms.AnnouncementForm(instance=announcement) + form.data["preview"] = "hi hi" + form.data["start_date"] = "2021-05-20" + form.data["user"] = self.local_user.id + + request = self.factory.post("", form.data) + request.user = self.local_user + request.user.is_superuser = True + + view(request, announcement.id) + + announcement.refresh_from_db() + self.assertEqual(announcement.preview, "hi hi") + self.assertEqual(announcement.start_date.year, 2021) + self.assertEqual(announcement.start_date.month, 5) + self.assertEqual(announcement.start_date.day, 20) + + def test_delete_announcement(self): + """delete an announcement""" + announcement = models.Announcement.objects.create( + preview="hi", user=self.local_user + ) + view = views.delete_announcement.as_view() + + request = self.factory.post("") + request.user = self.local_user + request.user.is_superuser = True + + view(request, announcement.id) + + self.assertFalse(models.Announcement.objects.exists()) + + def test_view_announcement(self): + """display announcement on other pages""" + view = views.User.as_view() + request = self.factory.get("") + + result = view(request) + + self.assertIsInstance(result, TemplateResponse) + result.render() + self.assertEqual(result.status_code, 200) diff --git a/bookwyrm/views/announcements.py b/bookwyrm/views/announcements.py index 3f38f7af7..6cd6ecd8f 100644 --- a/bookwyrm/views/announcements.py +++ b/bookwyrm/views/announcements.py @@ -51,7 +51,7 @@ class Announcements(View): form = forms.AnnouncementForm() data = { "announcements": Paginator( - models.Announcement.objects, PAGE_LENGTH + models.Announcement.objects.all(), PAGE_LENGTH ).get_page(request.GET.get("page")), "form": form, }