Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing migration and JSONLD support to models #1122

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 %}
21 changes: 21 additions & 0 deletions events/migrations/0024_event_events_start_d_6c01fc_idx_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.1 on 2024-09-20 15:03

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("events", "0023_alter_event_body"),
("wagtailcore", "0094_alter_page_locale"),
]

operations = [
migrations.AddIndex(
model_name="event",
index=models.Index(fields=["start_date"], name="events_start_d_6c01fc_idx"),
),
migrations.AddIndex(
model_name="event",
index=models.Index(fields=["end_date"], name="events_end_dat_f7b00b_idx"),
),
]
Loading