Skip to content

Commit

Permalink
Merge pull request #34 from louije/add-metabase-embed-finder
Browse files Browse the repository at this point in the history
Gestion des embeds Metabase
  • Loading branch information
louije authored Apr 30, 2024
2 parents 24c3f4f + 2cb9173 commit b7fd630
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cms/embeds_finders.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re

from urllib.parse import urlparse
from django.conf import settings
from wagtail.embeds.finders.base import EmbedFinder

Expand All @@ -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'''
<iframe src="{url}" height="{settings.WAGTAILEMBEDS_METABASE_HEIGHT}" width="100%"></iframe>
<script src="{self.resizer_url(url)}"></script>
<script>
iFrameResize({{}}, "iframe");
</script>
'''
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,
}
9 changes: 9 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand Down

0 comments on commit b7fd630

Please sign in to comment.