Skip to content

Commit

Permalink
Delete extra empty paragraphs from all Rich Text fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ranta committed Dec 17, 2024
1 parent e76880b commit fc76fa7
Showing 1 changed file with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Generated by Django 5.1.3 on 2024-12-17 14:38
from __future__ import annotations

from django.db import migrations


def clean_newlines(text: str | None) -> str:
if not text:
return text

return text.replace("<p><br></p>", "").replace("\r\n<p>&nbsp;</p>\r\n", "")


def clean_reservation_unit_fields(apps, schema_editor):
ReservationUnit = apps.get_model("tilavarauspalvelu", "ReservationUnit")

fields = [
"description_fi",
"description_sv",
"description_en",
"terms_of_use_fi",
"terms_of_use_sv",
"terms_of_use_en",
"reservation_pending_instructions_fi",
"reservation_pending_instructions_sv",
"reservation_pending_instructions_en",
"reservation_confirmed_instructions_fi",
"reservation_confirmed_instructions_sv",
"reservation_confirmed_instructions_en",
"reservation_cancelled_instructions_fi",
"reservation_cancelled_instructions_sv",
"reservation_cancelled_instructions_en",
]

rows = list(ReservationUnit.objects.all())
for row in rows:
for field in fields:
setattr(row, field, clean_newlines(getattr(row, field)))

ReservationUnit.objects.bulk_update(rows, fields=fields)


def clean_terms_of_use_fields(apps, schema_editor):
TermsOfUse = apps.get_model("tilavarauspalvelu", "TermsOfUse")

fields = [
"text_fi",
"text_sv",
"text_en",
]

rows = list(TermsOfUse.objects.all())
for row in rows:
for field in fields:
setattr(row, field, clean_newlines(getattr(row, field)))

TermsOfUse.objects.bulk_update(rows, fields=fields)


def clean_application_round_fields(apps, schema_editor):
ApplicationRound = apps.get_model("tilavarauspalvelu", "ApplicationRound")

fields = [
"criteria_fi",
"criteria_sv",
"criteria_en",
"notes_when_applying_fi",
"notes_when_applying_sv",
"notes_when_applying_en",
]

rows = list(ApplicationRound.objects.all())
for row in rows:
for field in fields:
setattr(row, field, clean_newlines(getattr(row, field)))

ApplicationRound.objects.bulk_update(rows, fields=fields)


def clean_banner_notification_fields(apps, schema_editor):
BannerNotification = apps.get_model("tilavarauspalvelu", "BannerNotification")

fields = [
"message_fi",
"message_sv",
"message_en",
]

rows = list(BannerNotification.objects.all())
for row in rows:
for field in fields:
setattr(row, field, clean_newlines(getattr(row, field)))

BannerNotification.objects.bulk_update(rows, fields=fields)


class Migration(migrations.Migration):
dependencies = [
("tilavarauspalvelu", "0055_migrate_instructions_to_html"),
]

operations = [
migrations.RunPython(clean_reservation_unit_fields, migrations.RunPython.noop),
migrations.RunPython(clean_terms_of_use_fields, migrations.RunPython.noop),
migrations.RunPython(clean_application_round_fields, migrations.RunPython.noop),
migrations.RunPython(clean_banner_notification_fields, migrations.RunPython.noop),
]

0 comments on commit fc76fa7

Please sign in to comment.