Skip to content

Commit

Permalink
Add meta noindex to pages marked as delisted in YNR
Browse files Browse the repository at this point in the history
  • Loading branch information
symroe committed Sep 12, 2023
1 parent 8393a08 commit 6d43bed
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 1 deletion.
6 changes: 5 additions & 1 deletion wcivf/apps/elections/templates/elections/post_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
{% block og_description_content %}{% include "elections/includes/_post_meta_description.html" %}{% endblock og_description_content %}
{% block twitter_title_content %}{% include "elections/includes/_post_meta_title.html" %}{% endblock twitter_title_content %}>
{% block twitter_description_content %}{% include "elections/includes/_post_meta_description.html" %}{% endblock twitter_description_content %}/>

{% block page_meta %}
{% if postelection.people.contains_delisted_person %}
<meta name="robots" content="noindex">
{% endif %}
{% endblock %}

{% block content %}
{% include "elections/includes/_post_breadcrumbs.html" %}
Expand Down
4 changes: 4 additions & 0 deletions wcivf/apps/people/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def current(self):
"""
return self.filter(election__current=True)

def contains_delisted_person(self):
return self.filter(person__delisted=True).exists()


class PersonPostManager(models.Manager):
def get_queryset(self):
Expand Down Expand Up @@ -107,6 +110,7 @@ def update_or_create_from_ynr(self, person):
"birth_date": person["birth_date"] or None,
"death_date": person["death_date"] or None,
"last_updated": last_updated,
"delisted": person.get("delisted", False),
}

for value_type in VALUE_TYPES_TO_IMPORT:
Expand Down
17 changes: 17 additions & 0 deletions wcivf/apps/people/migrations/0045_person_delisted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.1.6 on 2023-09-11 16:26

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("people", "0044_person_mastodon_username"),
]

operations = [
migrations.AddField(
model_name="person",
name="delisted",
field=models.BooleanField(default=False),
),
]
3 changes: 3 additions & 0 deletions wcivf/apps/people/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ class Person(models.Model):
last_or_current_job = models.CharField(null=True, max_length=800)
previously_in_parliament = models.CharField(null=True, max_length=800)

# Meta
delisted = models.BooleanField(default=False)

objects = PersonManager()

class Meta:
Expand Down
5 changes: 5 additions & 0 deletions wcivf/apps/people/templates/people/person_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
{% block twitter_image %}{% if object.photo_url %}{{ object.photo_url }}{% else %}{{ CANONICAL_URL }}{% static 'images/blank-avatar.png' %}{% endif %}{% endblock twitter_image %}
{% block twitter_description_content %}{% include "people/includes/_person_meta_description.html" %}{% endblock twitter_description_content %}
{% block twitter_image_alt %}Photo of {{ object.name }}{% endblock twitter_image_alt %}
{% block page_meta %}
{% if object.delisted %}
<meta name="robots" content="noindex">
{% endif %}
{% endblock %}

{% block content %}
{% if referer_postcode %}
Expand Down
8 changes: 8 additions & 0 deletions wcivf/apps/people/tests/test_person_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ def test_counts_by_post(self):
assert Person.objects.all().count() == 5
assert PersonPost.objects.all().counts_by_post().count() == 1

def test_delisted_value(self):
assert PersonPost.objects.all().contains_delisted_person() is False

person = Person.objects.first()
person.delisted = True
person.save()
assert PersonPost.objects.all().contains_delisted_person() is True


@pytest.mark.freeze_time("2021-01-13")
class PersonPostManagerTests(TestCase):
Expand Down
12 changes: 12 additions & 0 deletions wcivf/apps/people/tests/test_person_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,18 @@ def test_person_detail_404_with_string_pk(self):
req = self.client.get("/person/partywebsite.org/")
self.assertEqual(req.status_code, 404)

def test_noindex_tag_added(self):
noindex_string = """<meta name="robots" content="noindex">"""
PersonPostWithPartyFactory(
person=self.person, election=ElectionFactory()
)
response = self.client.get(self.person_url, follow=True)
self.assertNotContains(response, noindex_string)
self.person.delisted = True
self.person.save()
response = self.client.get(self.person_url, follow=True)
self.assertInHTML(noindex_string, response.content.decode("utf8"))


class TestPersonViewUnitTests:
@pytest.fixture
Expand Down

0 comments on commit 6d43bed

Please sign in to comment.