-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete extra empty paragraphs from all Rich Text fields
- Loading branch information
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
tilavarauspalvelu/migrations/0056_clean_newlines_from_rich_text_fields.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> </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), | ||
] |