From 5a102f2b6cbe4ac20fdb53cb40291d90ecdf5aca Mon Sep 17 00:00:00 2001 From: "Dragon (David McKee)" Date: Thu, 2 Sep 2021 14:53:34 +0100 Subject: [PATCH 1/2] Add OpenStreetMap embed finder --- app/config/settings/base.py | 6 ++++++ app/helpers/finders.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 app/helpers/finders.py diff --git a/app/config/settings/base.py b/app/config/settings/base.py index 99474868..aa933053 100644 --- a/app/config/settings/base.py +++ b/app/config/settings/base.py @@ -277,6 +277,12 @@ WAGTAILIMAGES_IMAGE_MODEL = "images.NHSXImage" WAGTAILDOCS_DOCUMENT_MODEL = "documents.NHSXDocument" WAGTAIL_SITE_NAME = "NHSX" +WAGTAILEMBEDS_FINDERS = [ + { + 'class': 'helpers.finders.OSMFinder', + # Any other options will be passed as kwargs to the __init__ method + } +] DEFAULT_AUTHOR_AVATAR = "avatar.png" diff --git a/app/helpers/finders.py b/app/helpers/finders.py new file mode 100644 index 00000000..6d8ed5e1 --- /dev/null +++ b/app/helpers/finders.py @@ -0,0 +1,37 @@ +from wagtail.embeds.finders.base import EmbedFinder + + +class OSMFinder(EmbedFinder): + """OpenStreetMap.org embed""" + def __init__(self, **options): + pass + + def accept(self, url): + """ + Returns True if this finder knows how to fetch an embed for the URL. + This should not have any side effects (no requests to external servers) + """ + + if not url.startswith('https://www.openstreetmap.org/export/embed.html'): + return False + return True + + + def find_embed(self, url, max_width=None): + """ + Takes a URL and max width and returns a dictionary of information about the + content to be used for embedding it on the site. + + This is the part that may make requests to external APIs. + """ + + return { + # 'title': "", + 'author_name': "OpenStreetMap contributors", + 'provider_name': "OpenStreetMap.org", + 'type': "rich", + # 'thumbnail_url': "URL to thumbnail image", + 'width': 425, + 'height': 350, + 'html': f"""

View Larger Map
""", + } From 9638970a5b284862113079a707caef64a554cd86 Mon Sep 17 00:00:00 2001 From: "Dragon (David McKee)" Date: Fri, 3 Sep 2021 11:15:48 +0100 Subject: [PATCH 2/2] Black spacing --- app/config/settings/base.py | 2 +- app/helpers/finders.py | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/config/settings/base.py b/app/config/settings/base.py index aa933053..06971258 100644 --- a/app/config/settings/base.py +++ b/app/config/settings/base.py @@ -279,7 +279,7 @@ WAGTAIL_SITE_NAME = "NHSX" WAGTAILEMBEDS_FINDERS = [ { - 'class': 'helpers.finders.OSMFinder', + "class": "helpers.finders.OSMFinder", # Any other options will be passed as kwargs to the __init__ method } ] diff --git a/app/helpers/finders.py b/app/helpers/finders.py index 6d8ed5e1..d9781571 100644 --- a/app/helpers/finders.py +++ b/app/helpers/finders.py @@ -3,6 +3,7 @@ class OSMFinder(EmbedFinder): """OpenStreetMap.org embed""" + def __init__(self, **options): pass @@ -12,10 +13,9 @@ def accept(self, url): This should not have any side effects (no requests to external servers) """ - if not url.startswith('https://www.openstreetmap.org/export/embed.html'): + if not url.startswith("https://www.openstreetmap.org/export/embed.html"): return False return True - def find_embed(self, url, max_width=None): """ @@ -27,11 +27,11 @@ def find_embed(self, url, max_width=None): return { # 'title': "", - 'author_name': "OpenStreetMap contributors", - 'provider_name': "OpenStreetMap.org", - 'type': "rich", + "author_name": "OpenStreetMap contributors", + "provider_name": "OpenStreetMap.org", + "type": "rich", # 'thumbnail_url': "URL to thumbnail image", - 'width': 425, - 'height': 350, - 'html': f"""""", + "width": 425, + "height": 350, + "html": f"""""", }