Calculate navbar text in a nicer way
This commit is contained in:
parent
b30d5ffa05
commit
7b3e583007
5 changed files with 35 additions and 3 deletions
|
@ -10,3 +10,4 @@ flake8-comprehensions==3.2.2
|
||||||
flake8-mutable==1.2.0
|
flake8-mutable==1.2.0
|
||||||
flake8-tuple==0.4.1
|
flake8-tuple==0.4.1
|
||||||
coverage==5.1
|
coverage==5.1
|
||||||
|
beautifulsoup4==4.9.0
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<li><a href="{% url 'homepage' %}" class="index-link"><code>/home/theorangeone</code></a></li>
|
<li><a href="{% url 'homepage' %}" class="index-link"><code>/home/theorangeone</code></a></li>
|
||||||
|
|
||||||
{% include "navigation/item.html" with reverse="about" text="about" %}
|
{% include "navigation/item.html" with reverse="about" %}
|
||||||
{% include "navigation/item.html" with reverse="blog:list" text="posts" %}
|
{% include "navigation/item.html" with reverse="blog:list" %}
|
||||||
|
|
|
@ -1 +1,5 @@
|
||||||
<li><a href="{% url reverse %}" class="nav-link"><code>~/{{ text | lower }}</code></a></li>
|
{% load navigation %}
|
||||||
|
|
||||||
|
{% url reverse as url %}
|
||||||
|
|
||||||
|
<li><a href="{{ url }}" class="nav-link"><code>{{ url | to_navbar_text }}</code></a></li>
|
||||||
|
|
8
website/common/templatetags/navigation.py
Normal file
8
website/common/templatetags/navigation.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
from django import template
|
||||||
|
|
||||||
|
register = template.Library()
|
||||||
|
|
||||||
|
|
||||||
|
@register.filter(is_safe=True)
|
||||||
|
def to_navbar_text(path):
|
||||||
|
return "~/" + path.strip("/")
|
|
@ -1,3 +1,4 @@
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
from django.test import SimpleTestCase
|
from django.test import SimpleTestCase
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
|
@ -15,6 +16,24 @@ class HomepageViewTestCase(BaseTestCase):
|
||||||
response = self.client.get(reverse("homepage"))
|
response = self.client.get(reverse("homepage"))
|
||||||
self.assertContains(response, '<body class="homepageview">')
|
self.assertContains(response, '<body class="homepageview">')
|
||||||
|
|
||||||
|
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):
|
class AboutViewTestCase(BaseTestCase):
|
||||||
def test_accessible(self):
|
def test_accessible(self):
|
||||||
|
|
Reference in a new issue