From 371401d44178a0da1d71d6bfb3f99f675627028d Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Tue, 31 Oct 2017 14:33:16 +1100 Subject: [PATCH] Teach users how to generate their own JSON-LD representation rather than doing it in the view code --- home/templates/home/index.html | 2 +- home/views.py | 13 ------------- users/models.py | 18 ++++++++++++++++++ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/home/templates/home/index.html b/home/templates/home/index.html index 3e5d7f3..898b53d 100644 --- a/home/templates/home/index.html +++ b/home/templates/home/index.html @@ -35,7 +35,7 @@ {% endfor %} - +
    diff --git a/home/views.py b/home/views.py index aa8599c..cfc0842 100644 --- a/home/views.py +++ b/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), } diff --git a/users/models.py b/users/models.py index bdf378f..40f45b6 100644 --- a/users/models.py +++ b/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',