Bring back support for https://schema.org/BreadcrumbList - it's gross but what can you do

This commit is contained in:
Danielle McLean 2017-10-27 09:35:56 +11:00
parent 67308911fb
commit 29da1e52d7
Signed by untrusted user: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
4 changed files with 54 additions and 7 deletions

View file

@ -1,7 +1,7 @@
{% load compress favtags lemoncurry_tags meta static %}<!doctype html>
<html lang="en" class="{% block html_class %}{% endblock %}">
<head{% meta_namespaces %}>{% site_name as site_name %}{% request_uri request as uri %}
<base href="{% request_origin request %}" />
<head{% meta_namespaces %}>{% site_name as site_name %}{% request_uri request as uri %}{% request_origin request as origin %}
<base href="{{ origin }}" />
<title class="p-name">{% if title %}{{ title }} ~ {% endif %}{{ site_name }}</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
@ -37,7 +37,7 @@
</div>
</nav>
{% if request.resolver_match.view_name %}
{% nav_crumbs request.resolver_match.view_name title %}
{% nav_crumbs request.resolver_match.view_name %}
{% endif %}
</header>

View file

@ -1,4 +1,4 @@
{% if crumbs %}
{% load jsonify %}{% if crumbs %}
<nav class="breadcrumbs" aria-label="breadcrumb" role="navigation">
<ol class="breadcrumb">
{% for crumb in crumbs %}
@ -6,5 +6,6 @@
{% endfor %}
<li class="breadcrumb-item active" aria-current="page">{% firstof current.label title %}</li>
</ol>
<script type="application/ld+json">{{ breadcrumb_list | jsonify }}</script>
</nav>
{% endif %}

View file

@ -0,0 +1,15 @@
from django import template
from django.core.serializers import serialize
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models.query import QuerySet
from django.utils.safestring import mark_safe
import json
register = template.Library()
@register.filter
def jsonify(value):
if isinstance(value, QuerySet):
return mark_safe(serialize('json', value))
return mark_safe(json.dumps(value, cls=DjangoJSONEncoder))

View file

@ -71,8 +71,39 @@ def nav_right(request):
return {'items': items, 'request': request}
@register.inclusion_tag('lemoncurry/tags/breadcrumbs.html')
def nav_crumbs(route, title):
@register.inclusion_tag('lemoncurry/tags/breadcrumbs.html', takes_context=True)
def nav_crumbs(context, route):
crumbs = breadcrumbs.find(route)
current = crumbs.pop()
return {'crumbs': crumbs, 'current': current, 'title': title}
item_list_element = [{
'@type': 'ListItem',
'position': i + 1,
'item': {
'@id': context['origin'] + reverse(crumb['route']),
'@type': 'WebPage',
'name': crumb['label']
}
} for i, crumb in enumerate(crumbs)]
item_list_element.append({
'@type': 'ListItem',
'position': len(item_list_element) + 1,
'item': {
'id': context['uri'],
'@type': 'WebPage',
'name': current['label'] or context.get('title'),
}
})
breadcrumb_list = {
'@context': 'http://schema.org',
'@type': 'BreadcrumbList',
'itemListElement': item_list_element
}
return {
'breadcrumb_list': breadcrumb_list,
'crumbs': crumbs,
'current': current,
'title': context['title'],
}