Skip to content

Commit

Permalink
Add iframe code validation
Browse files Browse the repository at this point in the history
  • Loading branch information
FinemechanicPub committed Oct 22, 2024
1 parent ee18c85 commit c5476f1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 3 additions & 2 deletions apps/content_pages/migrations/0009_embedcode.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by Django 3.2.25 on 2024-10-22 19:13
# Generated by Django 3.2.25 on 2024-10-22 20:02

import apps.content_pages.validators
from django.db import migrations, models


Expand All @@ -17,7 +18,7 @@ class Migration(migrations.Migration):
('created', models.DateTimeField(auto_now_add=True)),
('modified', models.DateTimeField(auto_now=True)),
('title', models.CharField(max_length=250, verbose_name='Заголовок')),
('code', models.TextField(max_length=500, verbose_name='Тег iframe для встраиваемого содержимого')),
('code', models.TextField(max_length=500, validators=[apps.content_pages.validators.iframe_validator], verbose_name='Тег iframe для встраиваемого содержимого')),
],
options={
'verbose_name': 'Встраиваемое содержимое',
Expand Down
5 changes: 4 additions & 1 deletion apps/content_pages/models/content_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.db import models
from django.utils.html import strip_tags

from apps.content_pages.validators import iframe_validator
from apps.core.models import BaseModel


Expand Down Expand Up @@ -58,7 +59,9 @@ class Meta:
class EmbedCode(AbstractItemWithTitle):
"""Embeddable iframe link."""

code = models.TextField(max_length=500, verbose_name="Тег iframe для встраиваемого содержимого")
code = models.TextField(
max_length=500, verbose_name="Тег iframe для встраиваемого содержимого", validators=(iframe_validator,)
)

class Meta:
verbose_name = "Встраиваемое содержимое"
Expand Down
7 changes: 7 additions & 0 deletions apps/content_pages/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.core.exceptions import ValidationError


def iframe_validator(code: str):
code = code.strip()
if not (code.startswith("<iframe ") and code.endswith("</iframe>")):
raise ValidationError("Встраиваемое содержимое должно быть заключено в теги <iframe></iframe>")

0 comments on commit c5476f1

Please sign in to comment.