diff --git a/cms/embeds_finders.py b/cms/embeds_finders.py index 67558d3..181da2c 100644 --- a/cms/embeds_finders.py +++ b/cms/embeds_finders.py @@ -1,5 +1,6 @@ import re +from urllib.parse import urlparse from django.conf import settings from wagtail.embeds.finders.base import EmbedFinder @@ -24,3 +25,37 @@ def find_embed(self, url, max_width=None): "height": settings.WAGTAILEMBEDS_GRIST_HEIGHT, "html": html, } + +class MetabaseFinder(EmbedFinder): + def __init__(self, **options): + pass + + def accept(self, url): + pattern = r"^https?://(?:www\.)?(stats|datalake)\.inclusion\.beta\.gouv\.fr/.+$" + return re.match(pattern, url) + + def metabase_domain(self, url): + parsed_url = urlparse(url) + return parsed_url.netloc + + def resizer_url(self, url): + return f"//{self.metabase_domain(url)}{settings.WAGTAILEMBEDS_METABASE_IFRAME_RESIZER_URL}" + + def find_embed(self, url, max_width=None): + html = f''' + + + + ''' + return { + "title": "Metabase Embed", + "author_name": "Plateforme de l'inclision", + "provider_name": "Metabase", + "type": "rich", + "thumbnail_url": f"https://{self.metabase_domain(url)}/app/assets/img/metabot-happy.svg", + "width": 600, + "height": settings.WAGTAILEMBEDS_METABASE_HEIGHT, + "html": html, + } diff --git a/config/settings.py b/config/settings.py index 6ca7684..cf2918f 100644 --- a/config/settings.py +++ b/config/settings.py @@ -230,6 +230,9 @@ { "class": "cms.embeds_finders.GristFinder", }, + { + "class": "cms.embeds_finders.MetabaseFinder", + }, # Handles all other oEmbed providers the default way { "class": "wagtail.embeds.finders.oembed", @@ -239,6 +242,12 @@ # Height for custom Grist Embed WAGTAILEMBEDS_GRIST_HEIGHT = 400 +# Height for custom Metabase Embed +WAGTAILEMBEDS_METABASE_HEIGHT = 650 + +# Auto-sizer script for custom Metabase Embed +WAGTAILEMBEDS_METABASE_IFRAME_RESIZER_URL = "/app/iframeResizer.js" + # Increase throttling to avoid Bad request errors when saving large pages # https://docs.djangoproject.com/en/4.2/ref/settings/#data-upload-max-number-fields DATA_UPLOAD_MAX_NUMBER_FIELDS = os.getenv("DATA_UPLOAD_MAX_NUMBER_FIELDS", 10000)