# Generated by Django 4.2 on 2024-09-27 09:21

from django.db import migrations
from django.template.defaultfilters import linebreaksbr
from django.core.exceptions import ValidationError

from django_bleach.templatetags.bleach_tags import bleach_value

from utils import forms


def update_text_editor_comments(apps, schema_editor):
    Article = apps.get_model("submission", "Article")
    articles = Article.objects.all()

    for article in articles:
        if article.comments_editor:
            try:
                forms.plain_text_validator(article.comments_editor)
                article.comments_editor = linebreaksbr(article.comments_editor)
            except ValidationError:
                # these fields may have harmful content, clean it with bleach
                article.comments_editor = bleach_value(
                    article.comments_editor,
                )
            article.save()


class Migration(migrations.Migration):
    dependencies = [
        ("submission", "0080_frozen_author_bleach_20240507_1350"),
    ]

    operations = [
        migrations.RunPython(
            update_text_editor_comments, reverse_code=migrations.RunPython.noop
        ),
    ]
