Add JSON-LD support to entries too, mostly so Google can understand the site a little better

This commit is contained in:
Danielle McLean 2017-10-31 14:51:50 +11:00
parent 371401d441
commit 7090db3c37
Signed by untrusted user: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
3 changed files with 34 additions and 2 deletions

View file

@ -1,9 +1,11 @@
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.db import models
from django.urls import reverse
from itertools import groupby
from slugify import slugify
from textwrap import shorten
from urllib.parse import urljoin
from meta.models import ModelMeta
from users.models import Profile
@ -97,6 +99,31 @@ class Entry(ModelMeta, models.Model):
def slug(self):
return slugify(self.name)
@property
def json_ld(self):
base = 'https://' + Site.objects.get_current().domain
url = urljoin(base, self.url)
posting = {
'@context': 'http://schema.org',
'@type': 'BlogPosting',
'@id': url,
'url': url,
'mainEntityOfPage': url,
'author': {
'@type': 'Person',
'url': urljoin(base, self.author.url),
'name': self.author.name,
},
'headline': self.title,
'description': self.excerpt,
'datePublished': self.published.isoformat(),
'dateModified': self.updated.isoformat(),
}
if self.photo:
posting['image'] = (urljoin(base, self.photo.url), )
return posting
class Meta:
verbose_name_plural = 'entries'
ordering = ['-published']

View file

@ -1,4 +1,4 @@
{% load friendly_url humanize markdown shortlink %}<article class="card h-entry">
{% load friendly_url humanize jsonify markdown shortlink %}<article class="card h-entry">
{% if entry.photo %}<img class="card-img-top u-photo" src="{{ entry.photo.url }}" />{% endif %}
<div class="card-body">
{% if entry.name %}<h4 class="card-title p-name">{{ entry.name }}</h4>{% endif %}
@ -32,4 +32,5 @@
</a>
{% endfor %}
</div>
<script class="p-json-ld" type="application/ld+json">{{ entry.json_ld | jsonify }}</script>
</article>

View file

@ -36,6 +36,10 @@ class User(ModelMeta, AbstractUser):
# This is gonna need to change if I ever decide to add multiple-user support ;)
url = '/'
@property
def name(self):
return '{0} {1}'.format(self.first_name, self.last_name)
def get_absolute_url(self):
return self.url
@ -65,7 +69,7 @@ class User(ModelMeta, AbstractUser):
'@type': 'Person',
'@id': urljoin(base, self.url),
'url': urljoin(base, self.url),
'name': '{0} {1}'.format(self.first_name, self.last_name),
'name': self.name,
'email': self.email,
'image': urljoin(base, self.avatar.url),
'givenName': self.first_name,