diff --git a/contact/models.py b/contact/models.py index 676663d9..3b2a85b5 100644 --- a/contact/models.py +++ b/contact/models.py @@ -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, @@ -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="", @@ -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 @@ -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" @@ -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( @@ -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, @@ -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 diff --git a/contact/templates/contact/contact.html b/contact/templates/contact/contact.html index 3284695a..4374be01 100644 --- a/contact/templates/contact/contact.html +++ b/contact/templates/contact/contact.html @@ -193,69 +193,6 @@

Memorials

{% block extra_js %} -{% endblock extra_js %} - - \ No newline at end of file +{% endblock %} diff --git a/events/migrations/0024_event_events_start_d_6c01fc_idx_and_more.py b/events/migrations/0024_event_events_start_d_6c01fc_idx_and_more.py new file mode 100644 index 00000000..1661cb17 --- /dev/null +++ b/events/migrations/0024_event_events_start_d_6c01fc_idx_and_more.py @@ -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"), + ), + ]