Skip to content

Commit

Permalink
Add JSONLD to models
Browse files Browse the repository at this point in the history
  • Loading branch information
brylie committed Sep 20, 2024
1 parent 2cfc6ba commit 986da3d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 68 deletions.
98 changes: 95 additions & 3 deletions contact/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from typing import Any

from django.contrib.postgres.fields import ArrayField
from django.db import models
from django.db.models import TextChoices
from django.utils.encoding import force_str
from django.utils.html import strip_tags
from modelcluster.fields import ParentalKey
from wagtail.admin.panels import (
FieldPanel,
Expand All @@ -16,7 +19,59 @@
from addresses.models import Address


class Person(Page):
class JSONLDMixin:
def get_json_ld(self):
data = {
"@context": "https://schema.org",
"name": force_str(self.title),
}

if hasattr(self, "description"):
data["description"] = strip_tags(force_str(self.description))

if hasattr(self, "website") and self.website:
data["url"] = self.website
if hasattr(self, "email") and self.email:
data["email"] = self.email
if hasattr(self, "phone") and self.phone:
data["telephone"] = self.phone

if hasattr(self, "addresses") and self.addresses.count():
data["address"] = []
for address in self.addresses.all():
address_data = {
"@type": "PostalAddress",
}
if address.street_address:
address_data["streetAddress"] = force_str(address.street_address)
if address.extended_address:
address_data["streetAddress"] += ", " + force_str(
address.extended_address,
)
if address.po_box_number:
address_data["streetAddress"] += ", P.O. Box " + force_str(
address.po_box_number,
)
if address.locality:
address_data["addressLocality"] = force_str(address.locality)
if address.region:
address_data["addressRegion"] = force_str(address.region)
if address.postal_code:
address_data["postalCode"] = force_str(address.postal_code)
if address.country:
address_data["addressCountry"] = force_str(address.country)
if address.latitude and address.longitude:
address_data["geo"] = {
"@type": "GeoCoordinates",
"latitude": address.latitude,
"longitude": address.longitude,
}
data["address"].append(address_data)

return data


class Person(JSONLDMixin, Page):
given_name = models.CharField(
max_length=255,
default="",
Expand Down Expand Up @@ -111,6 +166,17 @@ def save(
parent_page_types = ["contact.PersonIndexPage"]
subpage_types: list[str] = []

def get_json_ld(self):
data = super().get_json_ld()
data.update(
{
"@type": "Person",
"givenName": force_str(self.given_name),
"familyName": force_str(self.family_name),
},
)
return data


class PersonIndexPage(Page):
max_count = 1
Expand Down Expand Up @@ -141,7 +207,7 @@ class MeetingPresidingClerk(Orderable):
]


class Meeting(Page):
class Meeting(JSONLDMixin, Page):
class MeetingTypeChoices(TextChoices):
MONTHLY_MEETING = "monthly_meeting", "Monthly Meeting"
QUARTERLY_MEETING = "quarterly_meeting", "Quarterly Meeting"
Expand Down Expand Up @@ -281,6 +347,27 @@ def get_context(self, request, *args, **kwargs):

return context

def get_json_ld(self):
data = super().get_json_ld()
data.update(
{
"@type": "Organization",
"organizationType": force_str(self.get_meeting_type_display()),
},
)

if self.worship_times.count():
data["event"] = []
for worship_time in self.worship_times.all():
event_data = {
"@type": "Event",
"name": force_str(worship_time.get_worship_type_display()),
"description": force_str(worship_time.worship_time),
}
data["event"].append(event_data)

return data


class MeetingAddress(Orderable, Address):
page = ParentalKey(
Expand Down Expand Up @@ -321,7 +408,7 @@ class MeetingIndexPage(Page):
template = "contact/meeting_index_page.html"


class Organization(Page):
class Organization(JSONLDMixin, Page):
description = models.CharField(
max_length=255,
blank=True,
Expand Down Expand Up @@ -401,6 +488,11 @@ class Meta:
models.Index(fields=["drupal_author_id"]),
]

def get_json_ld(self):
data = super().get_json_ld()
data["@type"] = "Organization"
return data


class OrganizationIndexPage(Page):
max_count = 1
Expand Down
67 changes: 2 additions & 65 deletions contact/templates/contact/contact.html
Original file line number Diff line number Diff line change
Expand Up @@ -193,69 +193,6 @@ <h2>Memorials</h2>

{% block extra_js %}
<script type="application/ld+json">
{
"@context": "https://schema.org",
{% if page.specific_class_name == 'Person' %}
"@type": "Person",
"givenName": "{{ page.specific.given_name }}",
"familyName": "{{ page.specific.family_name }}",
{% elif page.specific_class_name == 'Meeting' %}
"@type": "Organization",
"additionalType": "{{ page.specific.get_meeting_type_display }}",
{% else %}
"@type": "Organization",
{% endif %}
"name": "{{ page.title }}",
{% if page.specific.description %}
"description": "{{ page.specific.description|striptags }}",
{% endif %}
{% if page.specific.website %}
"url": "{{ page.specific.website }}",
{% endif %}
{% if page.specific.email %}
"email": "{{ page.specific.email }}",
{% endif %}
{% if page.specific.phone %}
"telephone": "{{ page.specific.phone }}",
{% endif %}
{% if page.addresses.count %}
"address": [
{% for address in page.addresses.all %}
{
"@type": "PostalAddress",
"streetAddress": "{{ address.street_address }}{% if address.extended_address %}, {{ address.extended_address }}{% endif %}{% if address.po_box_number %}, PO Box {{ address.po_box_number }}{% endif %}",
"addressLocality": "{{ address.locality }}",
"addressRegion": "{{ address.region }}",
"postalCode": "{{ address.postal_code }}",
"addressCountry": "{{ address.country }}"
}{% if not forloop.last %},{% endif %}
{% endfor %}
],
{% endif %}
{% if page.specific_class_name == 'Meeting' and page.worship_times.count %}
"event": [
{% for worship_time in page.worship_times.all %}
{
"@type": "Event",
"name": "{{ worship_time.get_worship_type_display }}",
"description": "{{ worship_time.worship_time }}"
}{% if not forloop.last %},{% endif %}
{% endfor %}
],
{% endif %}
{% if page.specific_class_name == 'Meeting' and page.presiding_clerks.count %}
"member": [
{% for presiding_clerk in page.presiding_clerks.all %}
{
"@type": "Person",
"name": "{{ presiding_clerk.person.title }}",
"jobTitle": "Presiding Clerk"
}{% if not forloop.last %},{% endif %}
{% endfor %}
]
{% endif %}
}
{{ page.specific.get_json_ld|safe }}
</script>
{% endblock extra_js %}


{% endblock %}

0 comments on commit 986da3d

Please sign in to comment.