wagtailseo.utils

class wagtailseo.utils.StructDataEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Serializes data into LD+JSON format required for Structured Data.

default(obj)

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
wagtailseo.utils.get_absolute_media_url(site: wagtail.core.models.Site) → str

Returns an absolute base URL for media files.

This will normally be the site’s root URL, except for when MEDIA_URL already looks like a full URL (e.g. if media is stored and served from S3, a CDN, etc.). The return value can always be safely prefixed to an image URL.

Parameters

site (Site) – The site object from which the media is served.

Return type

str

Returns

The absolute base URL for media files of this site.

wagtailseo.utils.get_struct_data_images(site: wagtail.core.models.Site, image: wagtail.images.models.AbstractImage) → List[str]

Google requires multiple different aspect ratios for certain structured data image fields. This will render the image in 1:1, 4:3, and 16:9 aspect ratios with very high resolution and return a list of URLs.

Parameters
  • site (Site) – The Wagtail Site this image belongs to.

  • image (Image) – An image descending from Wagtail AbstractImage model.

Return type

List[str]

Returns

A list of absolute image URLs.

wagtailseo.utils.serialize_date(date: Union[datetime.datetime, datetime.time]) → str

Serializes a datetime or time into ISO 8601 format required for Open Graph and Structured Data.

Parameters

time] date (Union[datetime,) – The date object to serialize.

Return type

str

Returns

String-ified date.