fix user licence-id

sync_v2
Raz 7 months ago
parent e397a37b05
commit 68a926b605
  1. 2
      tournaments/admin.py
  2. 1
      tournaments/forms.py
  3. 45
      tournaments/migrations/0115_auto_20250403_1503.py
  4. 9
      tournaments/utils/licence_validator.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']

@ -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()

@ -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),
]

@ -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:

Loading…
Cancel
Save