Browse Source

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

pull/1/head
Danielle McLean 5 years ago
parent
commit
7090db3c37
Signed by: 00dani
GPG Key ID: 5A5D2D1AFF12EEC5
  1. 27
      entries/models.py
  2. 3
      entries/templates/entries/h-entry.html
  3. 6
      users/models.py

27
entries/models.py

@ -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']

3
entries/templates/entries/h-entry.html

@ -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>

6
users/models.py

@ -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,

Loading…
Cancel
Save