Skip to content

Commit

Permalink
Filter by tag and category
Browse files Browse the repository at this point in the history
-Render tags on post list and detail
-Fix filtering
-Add tags to Post list admin view
-Change tag widget to multiselect
  • Loading branch information
VirginiaDooley committed Oct 9, 2023
1 parent f94fb62 commit 5d8e588
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 2 deletions.
32 changes: 31 additions & 1 deletion democracy_club/apps/hermes/admin.py
Original file line number Diff line number Diff line change
@@ -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"]


Expand Down
15 changes: 15 additions & 0 deletions democracy_club/apps/hermes/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion democracy_club/apps/hermes/urls.py
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down Expand Up @@ -34,4 +35,9 @@
name="hermes_post_list",
),
re_path(r"^feed/$", view=LatestPostFeed(), name="hermes_post_feed"),
path(
"tag/<slug:tag>/",
view=TagPostListView.as_view(),
name="hermes_post_list_by_tag",
),
]
8 changes: 8 additions & 0 deletions democracy_club/apps/hermes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down
5 changes: 5 additions & 0 deletions democracy_club/assets/scss/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 8 additions & 0 deletions democracy_club/templates/hermes/post_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ <h1>{{ post.subject }}</h1>
{% endfor %}
</address>
on <time pubdate datetime="{{post.modified_on|date:"c"}}" title="{{post.created_on|date:"jS E Y"}}">{{post.created_on|date:"jS E Y"}}</time>
{% if post.tags %}
<span aria-hidden="true">🏷️</span>
{% for tag in post.tags %}
<ul>
<li class="blog-tag"><a href="{% url 'hermes_post_list_by_tag' tag %}">{{ tag }}</a>{% if not forloop.last %}, {% endif %}</li>
</ul>
{% endfor %}
{% endif %}
</div>

</header>
Expand Down
6 changes: 6 additions & 0 deletions democracy_club/templates/hermes/post_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ <h2 class="ds-h3"><a href="{{ post.get_absolute_url }}">{{ post.subject }}</a></
</address>
on <time pubdate datetime="{{post.modified_on|date:"c"}}" title="{{post.created_on|date:"jS E Y"}}">{{post.created_on|date:"jS E Y"}}</time>
</div>
<div>
<span aria-hidden="true">🏷️</span>
{% for tag in post.tags %}
<a href="{% url 'hermes_post_list_by_tag' tag %}">{{ tag }}</a>{% if not forloop.last %}, {% endif %}
{% endfor %}
</div>
{{ post.rendered_summary|safe|typogrify }}
<a class="ds-cta ds-cta-blue" href="{{ post.get_absolute_url }}">Read more</a>
</article>
Expand Down
5 changes: 5 additions & 0 deletions democracy_club/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 5d8e588

Please sign in to comment.