forked from 00dani/lemoncurry
Teach users how to generate their own JSON-LD representation rather than doing it in the view code
This commit is contained in:
parent
a86188fcb6
commit
371401d441
3 changed files with 19 additions and 14 deletions
|
@ -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">
|
||||
|
|
|
@ -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),
|
||||
}
|
||||
|
|
|
@ -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…
Reference in a new issue