Skip to content

Commit

Permalink
add ckeditor iframe migrations for plans and offlineevents
Browse files Browse the repository at this point in the history
  • Loading branch information
goapunk committed Mar 13, 2024
1 parent 915a0a8 commit 286f66f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions changelog/_1111.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Fixed

- add migrations for offlineevents and plans to make existing iframes appear in
the new ckeditor5
35 changes: 35 additions & 0 deletions meinberlin/apps/offlineevents/migrations/0011_ckeditor_iframes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 3.2.20 on 2023-11-16 11:35

from bs4 import BeautifulSoup
from django.db import migrations


def replace_iframe_with_figur(apps, schema_editor):
template = (
'<figure class="media"><div data-oembed-url="{url}"><div><iframe src="'
'{url}"></iframe></div></div></figure>'
)
OfflineEvent = apps.get_model("meinberlin_offlineevents", "OfflineEvent")
for offline_event in OfflineEvent.objects.all():
soup = BeautifulSoup(offline_event.description, "html.parser")
iframes = soup.findAll("iframe")
for iframe in iframes:
figure = BeautifulSoup(
template.format(url=iframe.attrs["src"]), "html.parser"
)
iframe.replaceWith(figure)
if iframes:
offline_event.description = soup.prettify(formatter="html")
offline_event.save()


class Migration(migrations.Migration):
dependencies = [
("meinberlin_offlineevents", "0010_alter_offlineevent_description"),
]

operations = [
migrations.RunPython(
replace_iframe_with_figur,
),
]
35 changes: 35 additions & 0 deletions meinberlin/apps/plans/migrations/0064_ckeditor_iframes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 3.2.20 on 2023-11-16 11:35

from bs4 import BeautifulSoup
from django.db import migrations


def replace_iframe_with_figur(apps, schema_editor):
template = (
'<figure class="media"><div data-oembed-url="{url}"><div><iframe src="'
'{url}"></iframe></div></div></figure>'
)
Plan = apps.get_model("meinberlin_plans", "Plan")
for plan in Plan.objects.all():
soup = BeautifulSoup(plan.description, "html.parser")
iframes = soup.findAll("iframe")
for iframe in iframes:
figure = BeautifulSoup(
template.format(url=iframe.attrs["src"]), "html.parser"
)
iframe.replaceWith(figure)
if iframes:
plan.description = soup.prettify(formatter="html")
plan.save()


class Migration(migrations.Migration):
dependencies = [
("meinberlin_plans", "0063_alter_plan_description"),
]

operations = [
migrations.RunPython(
replace_iframe_with_figur,
),
]

0 comments on commit 286f66f

Please sign in to comment.