From 7b3e5830074722a27b2aee81cf30490c75a540da Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 7 May 2020 15:22:07 +0100 Subject: [PATCH] Calculate navbar text in a nicer way --- dev-requirements.txt | 1 + templates/navigation/index.html | 4 ++-- templates/navigation/item.html | 6 +++++- website/common/templatetags/navigation.py | 8 ++++++++ website/common/tests.py | 19 +++++++++++++++++++ 5 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 website/common/templatetags/navigation.py diff --git a/dev-requirements.txt b/dev-requirements.txt index 3884021..9386086 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -10,3 +10,4 @@ flake8-comprehensions==3.2.2 flake8-mutable==1.2.0 flake8-tuple==0.4.1 coverage==5.1 +beautifulsoup4==4.9.0 diff --git a/templates/navigation/index.html b/templates/navigation/index.html index 99e8c54..5679b6b 100644 --- a/templates/navigation/index.html +++ b/templates/navigation/index.html @@ -1,4 +1,4 @@
  • /home/theorangeone
  • -{% include "navigation/item.html" with reverse="about" text="about" %} -{% include "navigation/item.html" with reverse="blog:list" text="posts" %} +{% include "navigation/item.html" with reverse="about" %} +{% include "navigation/item.html" with reverse="blog:list" %} diff --git a/templates/navigation/item.html b/templates/navigation/item.html index 7b9b456..3e164b7 100644 --- a/templates/navigation/item.html +++ b/templates/navigation/item.html @@ -1 +1,5 @@ -
  • ~/{{ text | lower }}
  • +{% load navigation %} + +{% url reverse as url %} + +
  • {{ url | to_navbar_text }}
  • diff --git a/website/common/templatetags/navigation.py b/website/common/templatetags/navigation.py new file mode 100644 index 0000000..6898687 --- /dev/null +++ b/website/common/templatetags/navigation.py @@ -0,0 +1,8 @@ +from django import template + +register = template.Library() + + +@register.filter(is_safe=True) +def to_navbar_text(path): + return "~/" + path.strip("/") diff --git a/website/common/tests.py b/website/common/tests.py index de6b1b7..f1bff80 100644 --- a/website/common/tests.py +++ b/website/common/tests.py @@ -1,3 +1,4 @@ +from bs4 import BeautifulSoup from django.test import SimpleTestCase from django.urls import reverse @@ -15,6 +16,24 @@ class HomepageViewTestCase(BaseTestCase): response = self.client.get(reverse("homepage")) self.assertContains(response, '') + def test_navbar_links_accessible(self): + response = self.client.get(reverse("homepage")) + soup = BeautifulSoup(response.content, features="html.parser") + nav_links = list(soup.find("nav").find_all("a", class_="nav-link")) + self.assertEqual(len(nav_links), 2) + for nav_link in nav_links: + nav_link_response = self.client.head(nav_link.attrs["href"]) + self.assertEqual(nav_link_response.status_code, 200) + + def test_navbar_link_text(self): + response = self.client.get(reverse("homepage")) + soup = BeautifulSoup(response.content, features="html.parser") + nav_links = list(soup.find("nav").find_all("a", class_="nav-link")) + self.assertEqual( + {nav_link.find("code").text for nav_link in nav_links}, + {"~/about", "~/blog"}, + ) + class AboutViewTestCase(BaseTestCase): def test_accessible(self):