From 6678b84de234294284922cf1400d793fb6d6bb62 Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Wed, 21 May 2025 10:10:26 +0300 Subject: [PATCH] Features: 1) Add `forwards` and `backwards` migration logic for `language` field normalization using `Lower`; 2) Enhance allowed choices formatting in `language` field for improved readability; Fixes: None; Extra: 1) Minor code cleanup and reorganization in migration file; --- .../migrations/0003_alter_user_language.py | 48 +++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/vibes_auth/migrations/0003_alter_user_language.py b/vibes_auth/migrations/0003_alter_user_language.py index 311eacc5..39be35b6 100644 --- a/vibes_auth/migrations/0003_alter_user_language.py +++ b/vibes_auth/migrations/0003_alter_user_language.py @@ -1,6 +1,19 @@ -# Generated by Django 5.2 on 2025-05-20 19:06 - from django.db import migrations, models +from django.db.models.functions import Lower + + +def forwards(apps, schema_editor): + User = apps.get_model('vibes_auth', 'User') + User.objects.all().update(language=Lower('language')) + + +def backwards(apps, schema_editor): + User = apps.get_model('vibes_auth', 'User') + for u in User.objects.all(): + parts = u.language.split('-', 1) + if len(parts) == 2: + u.language = f"{parts[0].lower()}-{parts[1].upper()}" + u.save(update_fields=['language']) class Migration(migrations.Migration): @@ -9,15 +22,34 @@ class Migration(migrations.Migration): ] operations = [ + migrations.RunPython(forwards, backwards), + migrations.AlterField( model_name='user', name='language', field=models.CharField( - choices=[('en-gb', 'English (British)'), ('ar-ar', 'العربية'), ('cs-cz', 'Česky'), ('da-dk', 'Dansk'), - ('de-de', 'Deutsch'), ('en-us', 'English (American)'), ('es-es', 'Español'), - ('fr-fr', 'Français'), ('hi-in', 'हिंदी'), ('it-it', 'Italiano'), ('ja-jp', '日本語'), - ('kk-kz', 'Қазақ'), ('nl-nl', 'Nederlands'), ('pl-pl', 'Polska'), ('pt-br', 'Português'), - ('ro-ro', 'Română'), ('ru-ru', 'Русский'), ('zh-hans', '简体中文')], default='en-gb', - max_length=7), + choices=[ + ('en-gb', 'English (British)'), + ('ar-ar', 'العربية'), + ('cs-cz', 'Česky'), + ('da-dk', 'Dansk'), + ('de-de', 'Deutsch'), + ('en-us', 'English (American)'), + ('es-es', 'Español'), + ('fr-fr', 'Français'), + ('hi-in', 'हिंदी'), + ('it-it', 'Italiano'), + ('ja-jp', '日本語'), + ('kk-kz', 'Қазақ'), + ('nl-nl', 'Nederlands'), + ('pl-pl', 'Polska'), + ('pt-br', 'Português'), + ('ro-ro', 'Română'), + ('ru-ru', 'Русский'), + ('zh-hans', '简体中文'), + ], + default='en-gb', + max_length=7, + ), ), ]