Skip to content

Commit

Permalink
(forum) wrap iframe in div tag, for display purpose (TESTS TBD)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Apr 16, 2024
1 parent 0d17949 commit b3abfc9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lacommunaute/forum/forms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import re

from django import forms

from lacommunaute.forum.models import Forum


def wrap_iframe_in_div_tag(text):
# iframe tags must be wrapped in a div tag to be displayed correctly
# add it if not present

if "<iframe" in text and "</iframe>" in text:
regex = r"<div>\s*<iframe.*</iframe>\s*</div>"
if not re.search(regex, text):
text = text.replace("<iframe", "<div><iframe")
text = text.replace("</iframe>", "</iframe></div>")

return text


class ForumForm(forms.ModelForm):
name = forms.CharField(required=True, label="Titre")
short_description = forms.CharField(
Expand All @@ -15,6 +30,13 @@ class ForumForm(forms.ModelForm):
widget=forms.Textarea(attrs={"rows": 20}), required=False, label="Contenu (markdown autorisé)"
)

def save(self, commit=True):
forum = super().save(commit=False)
forum.description = wrap_iframe_in_div_tag(self.cleaned_data.get("description"))
if commit:
forum.save()
return forum

class Meta:
model = Forum
fields = ["name", "short_description", "description"]

0 comments on commit b3abfc9

Please sign in to comment.