Skip to content

Commit

Permalink
Misc fixes to make blog tags more effective
Browse files Browse the repository at this point in the history
- Add alt text to blog hero
- Autogenerate blog slugs
- Add featured posts to context
- Render post tag menu
- Change latest posts header
- Update blog tags with instructions
  • Loading branch information
VirginiaDooley authored and Virginia Dooley committed Oct 20, 2023
1 parent aa92655 commit ec8a620
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ New posts are created at `/admin/hermes/post`.

Blog posts now include tags which correspond to projects, such as
`representatives`.

We've hard-coded tags to reduce the risk of typos and to ensure that tags are used consistently. To add or edit a tag, change the `tag_values` variable in `democracy_club/apps/hermes/admin.py`.

## Staging environment
Add your branch to the staging env on .circleci/config to deploy to https://stage.democracyclub.org.uk and test/view edits

Expand Down
1 change: 1 addition & 0 deletions democracy_club/apps/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ class HomeView(TemplateView):
def get_context_data(self, **kwargs):
context = super(HomeView, self).get_context_data(**kwargs)
context["latest_posts"] = Post.objects.recent(3)
context["featured_posts"] = Post.objects.for_tag("featured").recent(3)

return context
24 changes: 13 additions & 11 deletions democracy_club/apps/hermes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ class Meta:
fields = "__all__"

tag_values = [
("who_can_i_vote_for", "WCIVF"),
("case_study", "case_study"),
("research", "research"),
("elections", "elections"),
("councils", "councils"),
("candidates", "candidates"),
("representatives", "representatives"),
("data", "data"),
("where_do_i_vote", "WDIV"),
("election_leaflets", "electionleaflets"),
("blog", "blog"),
("WhoCanIVoteFor", "WhoCanIVoteFor"),
("CaseStudy", "Case Study"),
("Research", "Research"),
("Elections", "Elections"),
("Councils", "Councils"),
("Candidates", "Candidates"),
("Representatives", "Representatives"),
("Data", "Data"),
("WhereDoIVote", "WhereDoIVote"),
("Election_Leaflets", "Election Leaflets"),
("Blog", "Blog"),
("Impact", "Impact"),
("Featured", "Featured"),
]

tags = forms.MultipleChoiceField(
Expand Down
23 changes: 23 additions & 0 deletions democracy_club/apps/hermes/migrations/0012_post_hero_alt_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.6 on 2023-10-18 12:15

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("hermes", "0011_alter_post_slug"),
]

operations = [
migrations.AddField(
model_name="post",
name="hero_alt_text",
field=models.CharField(
blank=True,
max_length=100,
null=True,
verbose_name="hero alt text",
),
),
]
10 changes: 10 additions & 0 deletions democracy_club/apps/hermes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ class Post(TimestampedModel):
hero = models.ImageField(
_("hero"), upload_to=post_hero_upload_to, blank=True, null=True
)
hero_alt_text = models.CharField(
_("hero alt text"), max_length=100, blank=True, null=True
)
subject = models.CharField(_("subject"), max_length=100)
slug = models.SlugField(_("slug"), max_length=100, unique=True)
summary = models.TextField(_("summary"), blank=True, null=True)
Expand Down Expand Up @@ -238,6 +241,13 @@ def _rendered_attribute(self, attr_name):
return settings.MARKUP_RENDERER(attr_value)
return attr_value

def _generate_slug(self):
return slugify(self.subject)

def save(self, *args, **kwargs):
self.slug = self._generate_slug()
super(Post, self).save(*args, **kwargs)

@property
def reading_time(self):
time = (self.summary.count(" ") + self.body.count(" ")) / 300
Expand Down
5 changes: 5 additions & 0 deletions democracy_club/apps/hermes/tests/test_post_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ def test_author_formatting(self):
response = self.client.get(self.post5.get_absolute_url())
self.assertInHTML(expected, response.rendered_content)

def test_generate_slug(self):
"""A Post should know how to generate its slug"""
expected = "a-leela-of-her-own"
self.assertEqual(expected, self.post1._generate_slug())


class PostQuerySetTestCase(HermesTestCase):
def test_reverse_creation_order(self):
Expand Down
11 changes: 11 additions & 0 deletions democracy_club/apps/hermes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ def get_queryset(self):
qs = qs.for_tag(tag)
return qs

def get_context_data(self, **kwargs):
context = super(PostListView, self).get_context_data(**kwargs)
posts = Post.objects.all().published()
tags = []
for post in posts:
for tag in post.tags:
if tag not in tags:
tags.append(tag)
context["all_tags"] = tags
return context


class TagPostListView(PostListView):
"""Displays posts from a specific Tag"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ <h1>Candidates Wiki</h1>
{% endfilter %}

{% load post_tags %}
{% posts_for_tag "WCIVF" %}
{% posts_for_tag "wcivf" %}
</div>

{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
{% endfilter %}

{% load post_tags %}
{% posts_for_tag "WDIV" %}
{% posts_for_tag "wdiv" %}

{% endblock main_content %}
2 changes: 1 addition & 1 deletion democracy_club/templates/hermes/post_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ <h1>{{ post.subject }}</h1>
</header>
{% if post.hero %}
{% thumbnail post.hero "690" as im %}
<img class="blog_hero" src="{{ im.url }}">
<img class="blog_hero" src="{{ im.url }}" alt="{{ post.hero_alt_text }}">
{% endthumbnail %}
{% endif %}
{{ post.rendered|safe|typogrify }}
Expand Down
7 changes: 7 additions & 0 deletions democracy_club/templates/hermes/post_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

<div class="ds-stack">
<h1>Democracy Club Blog</h1>
<p>Our work, election news, and other items of interest.</p>
<ul class="unindented-list">
<span aria-hidden="true">🏷️ Topics:</span>
{% for tag in all_tags %}
<li class="blog-tag"><a href="{% url 'hermes_post_list_by_tag' tag %}">{{ tag }}</a>{% if not forloop.last %}, {% endif %}</li>
{% endfor %}
</ul>
{% for post in posts %}
<article class="ds-stack-smaller">
<h2 class="ds-h3"><a href="{{ post.get_absolute_url }}">{{ post.subject }}</a></h2>
Expand Down
2 changes: 1 addition & 1 deletion democracy_club/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h2>Community volunteers</h2>
<hr>

{% if latest_posts %}
<h2>On the blog</h2>
<h2>Latest blog posts</h2>
<ul class="post-list ds-switcher" style="--gridCellMin: 20ch; --gridGap: 2rem;">
{% for post in latest_posts %}
<li class="ds-card">
Expand Down

0 comments on commit ec8a620

Please sign in to comment.