From ded995590b0ee083c976e14f39584f2c21b38ab2 Mon Sep 17 00:00:00 2001 From: Virginia Dooley Date: Tue, 3 Oct 2023 10:19:29 +0100 Subject: [PATCH] Filter by tag and category -Render tags on post list and detail -Fix filtering -Add tags to Post list admin view -Change tag widget to multiselect --- democracy_club/apps/hermes/admin.py | 32 ++++++++++++++++++- .../apps/hermes/tests/test_views.py | 15 +++++++++ democracy_club/apps/hermes/urls.py | 8 ++++- democracy_club/apps/hermes/views.py | 8 +++++ democracy_club/assets/scss/styles.scss | 5 +++ .../templates/hermes/post_detail.html | 8 +++++ .../templates/hermes/post_list.html | 6 ++++ democracy_club/urls.py | 5 +++ 8 files changed, 85 insertions(+), 2 deletions(-) diff --git a/democracy_club/apps/hermes/admin.py b/democracy_club/apps/hermes/admin.py index 97e06c2f..305d1ac9 100644 --- a/democracy_club/apps/hermes/admin.py +++ b/democracy_club/apps/hermes/admin.py @@ -1,15 +1,45 @@ +from django import forms from django.contrib import admin +from django.contrib.admin.widgets import FilteredSelectMultiple from hermes.models import Category, Post +class PostAdminForm(forms.ModelForm): + class Meta: + model = Post + fields = "__all__" + + tag_values = [ + ("wcivf", "WCIVF"), + ("case_study", "case_study"), + ("research", "research"), + ("elections", "elections"), + ("councils", "councils"), + ("candidates", "candidates"), + ("representatives", "representatives"), + ("data", "data"), + ("WDIV", "WDIV"), + ("electionleaflets", "electionleaflets"), + ("blog", "blog"), + ] + + tags = forms.MultipleChoiceField( + choices=tag_values, + widget=FilteredSelectMultiple("tags", False), + required=False, + ) + + class PostAdmin(admin.ModelAdmin): + form = PostAdminForm + prepopulated_fields = { "slug": ("subject",), } filter_horizontal = ["author"] search_fields = ["subject"] search_help_text = "Search by subject (Post title)" - list_display = ["subject", "created_on", "category"] + list_display = ["subject", "created_on", "category", "tags"] list_filter = ["is_published", "author"] diff --git a/democracy_club/apps/hermes/tests/test_views.py b/democracy_club/apps/hermes/tests/test_views.py index ceb4330c..890b3fcf 100644 --- a/democracy_club/apps/hermes/tests/test_views.py +++ b/democracy_club/apps/hermes/tests/test_views.py @@ -32,6 +32,21 @@ def test_qs_contains_tags(self): self.assertNotContains(response, self.post3.subject) +class TagPostListViewTestCase(HermesTestCase): + def url(self, tag): + return super(TagPostListViewTestCase, self).url( + "hermes_post_list_by_tag", tag=tag + ) + + def test_get_queryset(self): + """The TagPostListView Context should contain a QuerySet of all Posts + with the given tag + """ + response = self.get(self.url("foo")) + expected = list(models.Post.objects.for_tag("foo")) + self.assertEqual(expected, list(response.context["posts"])) + + class ArchivePostListViewTestCase(HermesTestCase): def url(self, year=None, month=None, day=None): if year and month and day: diff --git a/democracy_club/apps/hermes/urls.py b/democracy_club/apps/hermes/urls.py index 069ff462..13d0cea3 100644 --- a/democracy_club/apps/hermes/urls.py +++ b/democracy_club/apps/hermes/urls.py @@ -1,10 +1,11 @@ -from django.urls import re_path +from django.urls import path, re_path from .feeds import LatestPostFeed from .views import ( ArchivePostListView, PostDetail, PostListView, + TagPostListView, ) urlpatterns = [ @@ -34,4 +35,9 @@ name="hermes_post_list", ), re_path(r"^feed/$", view=LatestPostFeed(), name="hermes_post_feed"), + path( + "tag//", + view=TagPostListView.as_view(), + name="hermes_post_list_by_tag", + ), ] diff --git a/democracy_club/apps/hermes/views.py b/democracy_club/apps/hermes/views.py index 2212dea1..d99a00e3 100644 --- a/democracy_club/apps/hermes/views.py +++ b/democracy_club/apps/hermes/views.py @@ -35,6 +35,14 @@ def get_queryset(self): return qs +class TagPostListView(PostListView): + """Displays posts from a specific Tag""" + + def get_queryset(self): + tag = self.kwargs.get("tag", "") + return self.model.objects.for_tag(tag) + + class CategoryPostListView(PostListView): """Displays posts from a specific Category""" diff --git a/democracy_club/assets/scss/styles.scss b/democracy_club/assets/scss/styles.scss index 95d011fa..efae4a12 100644 --- a/democracy_club/assets/scss/styles.scss +++ b/democracy_club/assets/scss/styles.scss @@ -226,3 +226,8 @@ ul > li > ul { column-gap: $s2; list-style: none; } + +.blog-tag { + // # show them inline rather than as a list + display: inline; +} diff --git a/democracy_club/templates/hermes/post_detail.html b/democracy_club/templates/hermes/post_detail.html index f2944a39..51bb5f3f 100644 --- a/democracy_club/templates/hermes/post_detail.html +++ b/democracy_club/templates/hermes/post_detail.html @@ -32,6 +32,14 @@

{{ post.subject }}

{% endfor %} on + {% if post.tags %} + + {% for tag in post.tags %} + + {% endfor %} + {% endif %} diff --git a/democracy_club/templates/hermes/post_list.html b/democracy_club/templates/hermes/post_list.html index 2efde618..cdb26565 100644 --- a/democracy_club/templates/hermes/post_list.html +++ b/democracy_club/templates/hermes/post_list.html @@ -24,6 +24,12 @@

{{ post.subject }} on +
+ + {% for tag in post.tags %} + {{ tag }}{% if not forloop.last %}, {% endif %} + {% endfor %} +
{{ post.rendered_summary|safe|typogrify }} Read more diff --git a/democracy_club/urls.py b/democracy_club/urls.py index 500890de..efaf1682 100644 --- a/democracy_club/urls.py +++ b/democracy_club/urls.py @@ -66,6 +66,11 @@ def trigger_error(request): TemplateView.as_view(template_name="research/case_studies.html"), name="case_studies", ), + path( + "research/media/", + TemplateView.as_view(template_name="research/media.html"), + name="media", + ), path( "research/impact/", TemplateView.as_view(template_name="research/impact.html"),