From 68a926b60519e09918b18348998fa20f96e786ae Mon Sep 17 00:00:00 2001 From: Raz Date: Thu, 3 Apr 2025 15:04:47 +0200 Subject: [PATCH] fix user licence-id --- tournaments/admin.py | 2 + tournaments/forms.py | 1 - .../migrations/0115_auto_20250403_1503.py | 45 +++++++++++++++++++ tournaments/utils/licence_validator.py | 9 +++- 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 tournaments/migrations/0115_auto_20250403_1503.py diff --git a/tournaments/admin.py b/tournaments/admin.py index 6163a1a..1248c10 100644 --- a/tournaments/admin.py +++ b/tournaments/admin.py @@ -13,6 +13,8 @@ class CustomUserAdmin(UserAdmin): form = CustomUserChangeForm add_form = CustomUserCreationForm model = CustomUser + search_fields = ('username', 'email', 'phone', 'first_name', 'last_name', 'licence_id') + list_display = ['email', 'first_name', 'last_name', 'username', 'licence_id', 'date_joined', 'latest_event_club_name', 'is_active', 'event_count', 'origin'] list_filter = ['is_active', 'origin'] ordering = ['-date_joined'] diff --git a/tournaments/forms.py b/tournaments/forms.py index ea3fc96..cb868df 100644 --- a/tournaments/forms.py +++ b/tournaments/forms.py @@ -102,7 +102,6 @@ class SimpleCustomUserCreationForm(UserCreationForm): class CustomUserChangeForm(UserChangeForm): def clean_licence_id(self): - print("CustomUserChangeForm") licence_id = self.cleaned_data.get('licence_id') if licence_id: licence_id = licence_id.replace(' ', '').strip().upper() diff --git a/tournaments/migrations/0115_auto_20250403_1503.py b/tournaments/migrations/0115_auto_20250403_1503.py new file mode 100644 index 0000000..9ad2a0c --- /dev/null +++ b/tournaments/migrations/0115_auto_20250403_1503.py @@ -0,0 +1,45 @@ +# Generated by Django 5.1 on 2025-04-03 12:29 + +from django.db import migrations +from ..utils.licence_validator import LicenseValidator + +def clean_license_ids(apps, schema_editor): + # Get the historical model + CustomUser = apps.get_model('tournaments', 'CustomUser') + + # Query all users + users = CustomUser.objects.all() + + for user in users: + if user.licence_id: + # Clean up logic - examples: + # 1. Strip whitespace + cleaned_id = user.licence_id.strip() + cleaned_id = cleaned_id.replace(' ', '').strip().upper() + last_char = cleaned_id[-1] if cleaned_id else '' + ends_with_non_alpha = not last_char.isalpha() + if ends_with_non_alpha: + key = LicenseValidator.get_computed_license_key(cleaned_id) + if key: + cleaned_id += key.upper() + + + # Save the cleaned value + if cleaned_id != user.licence_id: + user.licence_id = cleaned_id + user.save() + +def reverse_migration(apps, schema_editor): + # Reversing this migration isn't generally needed for data cleanup + pass + + +class Migration(migrations.Migration): + + dependencies = [ + ('tournaments', '0114_purchase_creation_date_purchase_last_update_and_more'), + ] + + operations = [ + migrations.RunPython(clean_license_ids, reverse_migration), + ] diff --git a/tournaments/utils/licence_validator.py b/tournaments/utils/licence_validator.py index 105bb22..e3b51ef 100644 --- a/tournaments/utils/licence_validator.py +++ b/tournaments/utils/licence_validator.py @@ -20,6 +20,13 @@ class LicenseValidator: @property def computed_license_key(self) -> str: stripped = self.stripped_license + key = LicenseValidator.get_computed_license_key(stripped) + if key: + return key + return 'aucune clé de licence' + + @staticmethod + def get_computed_license_key(stripped): if stripped and stripped.isdigit(): int_value = int(stripped) value = (int_value - 1) % 23 @@ -34,7 +41,7 @@ class LicenseValidator: char_code += 1 return chr(char_code) - return 'aucune clé de licence' + return None def validate_license(self) -> bool: if not self.license_id or len(self.license_id) < 7: