Browse Source

Teach users how to generate their own JSON-LD representation rather than doing it in the view code

pull/1/head
Danielle McLean 5 years ago
parent
commit
371401d441
Signed by: 00dani
GPG Key ID: 5A5D2D1AFF12EEC5
  1. 2
      home/templates/home/index.html
  2. 13
      home/views.py
  3. 18
      users/models.py

2
home/templates/home/index.html

@ -35,7 +35,7 @@
</li>{% endfor %}
</ul>
</div>
<script class="p-json-ld" type="application/ld+json">{{ person | jsonify }}</script>
<script class="p-json-ld" type="application/ld+json">{{ user.json_ld | jsonify }}</script>
</article>
</aside>
<ol class="list-unstyled entries">

13
home/views.py

@ -11,22 +11,9 @@ def index(request):
query = User.objects.prefetch_related('entries', 'profiles', 'keys')
user = get_object_or_404(query, pk=1)
uri = utils.uri(request)
person = {
'@context': 'http://schema.org',
'@type': 'Person',
'@id': uri,
'url': uri,
'name': '{0} {1}'.format(user.first_name, user.last_name),
'email': user.email,
'image': user.avatar.url,
'givenName': user.first_name,
'familyName': user.last_name,
'sameAs': [profile.url for profile in user.profiles.all()]
}
return {
'user': user,
'person': person,
'entries': user.entries.all(),
'meta': user.as_meta(request),
}

18
users/models.py

@ -1,7 +1,9 @@
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.sites.models import Site as DjangoSite
from django.utils.functional import cached_property
from meta.models import ModelMeta
from urllib.parse import urljoin
def avatar_path(instance, name):
@ -55,6 +57,22 @@ class User(ModelMeta, AbstractUser):
except IndexError:
return None
@property
def json_ld(self):
base = 'https://' + DjangoSite.objects.get_current().domain
return {
'@context': 'http://schema.org',
'@type': 'Person',
'@id': urljoin(base, self.url),
'url': urljoin(base, self.url),
'name': '{0} {1}'.format(self.first_name, self.last_name),
'email': self.email,
'image': urljoin(base, self.avatar.url),
'givenName': self.first_name,
'familyName': self.last_name,
'sameAs': [profile.url for profile in self.profiles.all()]
}
_metadata = {
'image': 'avatar_url',
'description': 'note',

Loading…
Cancel
Save