Skip to content

Commit

Permalink
Merge pull request #466 from nhsx/feature/embed_maps
Browse files Browse the repository at this point in the history
Add OpenStreetMap embed finder
  • Loading branch information
dragon-dxw committed Sep 3, 2021
2 parents 3ee375f + b74cc23 commit 8f967d2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
37 changes: 37 additions & 0 deletions app/helpers/finders.py
Original file line number Diff line number Diff line change
@@ -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"""<div class='osm-embed'><iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="{url}" style="border: 1px solid black"></iframe><br/><small><a href="https://www.openstreetmap.org/#map=14/54.7747/-1.5886">View Larger Map</a></small></div>""",
}

0 comments on commit 8f967d2

Please sign in to comment.