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

This commit is contained in:
Danielle McLean 2017-10-31 14:33:16 +11:00
parent a86188fcb6
commit 371401d441
Signed by untrusted user: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
3 changed files with 19 additions and 14 deletions

View file

@ -35,7 +35,7 @@
</li>{% endfor %} </li>{% endfor %}
</ul> </ul>
</div> </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> </article>
</aside> </aside>
<ol class="list-unstyled entries"> <ol class="list-unstyled entries">

View file

@ -11,22 +11,9 @@ def index(request):
query = User.objects.prefetch_related('entries', 'profiles', 'keys') query = User.objects.prefetch_related('entries', 'profiles', 'keys')
user = get_object_or_404(query, pk=1) user = get_object_or_404(query, pk=1)
uri = utils.uri(request) 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 { return {
'user': user, 'user': user,
'person': person,
'entries': user.entries.all(), 'entries': user.entries.all(),
'meta': user.as_meta(request), 'meta': user.as_meta(request),
} }

View file

@ -1,7 +1,9 @@
from django.db import models from django.db import models
from django.contrib.auth.models import AbstractUser from django.contrib.auth.models import AbstractUser
from django.contrib.sites.models import Site as DjangoSite
from django.utils.functional import cached_property from django.utils.functional import cached_property
from meta.models import ModelMeta from meta.models import ModelMeta
from urllib.parse import urljoin
def avatar_path(instance, name): def avatar_path(instance, name):
@ -55,6 +57,22 @@ class User(ModelMeta, AbstractUser):
except IndexError: except IndexError:
return None 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 = { _metadata = {
'image': 'avatar_url', 'image': 'avatar_url',
'description': 'note', 'description': 'note',