From fc76fa7badd916a6c8d96233e797e4f9eb825c3a Mon Sep 17 00:00:00 2001 From: Eemeli Ranta Date: Tue, 17 Dec 2024 18:46:11 +0200 Subject: [PATCH] Delete extra empty paragraphs from all Rich Text fields --- ...56_clean_newlines_from_rich_text_fields.py | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 tilavarauspalvelu/migrations/0056_clean_newlines_from_rich_text_fields.py diff --git a/tilavarauspalvelu/migrations/0056_clean_newlines_from_rich_text_fields.py b/tilavarauspalvelu/migrations/0056_clean_newlines_from_rich_text_fields.py new file mode 100644 index 000000000..27d518ce9 --- /dev/null +++ b/tilavarauspalvelu/migrations/0056_clean_newlines_from_rich_text_fields.py @@ -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("


", "").replace("\r\n

 

\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), + ]