Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter by blog tag #738

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created this for clarity as there are multiple for_tag methods throughout.

"""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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ul needs to be outside the loop (drive by review!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #739

<li class="blog-tag"><a href="{% url 'hermes_post_list_by_tag' tag %}">{{ tag }}</a>{% if not forloop.last %}, {% endif %}</li>
</ul>
{% endfor %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you render tags as a ul & li list? And add some CSS to show them inline rather than as a list? This will help screen readers read them out as tags.

Copy link
Contributor Author

@VirginiaDooley VirginiaDooley Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Ran Lighthouse and the page scored 100% for navigation.

{% 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