fix licence-id stuff

sync_v2
Raz 7 months ago
parent d400a07f88
commit 3bd4007acc
  1. 32
      tournaments/forms.py
  2. 35
      tournaments/models/enums.py
  3. 57
      tournaments/models/tournament.py
  4. 51
      tournaments/services/tournament_registration.py
  5. 110
      tournaments/templates/tournaments/tournament_info.html
  6. 4
      tournaments/utils/licence_validator.py

@ -15,7 +15,12 @@ class CustomUserCreationForm(UserCreationForm):
def clean_licence_id(self):
licence_id = self.cleaned_data.get('licence_id')
if licence_id:
return licence_id.replace(' ', '').strip().upper()
licence_id = licence_id.replace(' ', '').strip().upper()
validator = LicenseValidator(licence_id)
if validator.validate_license():
licence_id = validator.computed_licence_id
else:
raise forms.ValidationError('Le numéro de licence est invalide, la lettre ne correspond pas.')
return licence_id
class Meta:
@ -41,7 +46,12 @@ class SimpleCustomUserCreationForm(UserCreationForm):
def clean_licence_id(self):
licence_id = self.cleaned_data.get('licence_id')
if licence_id:
return licence_id.replace(' ', '').strip().upper()
licence_id = licence_id.replace(' ', '').strip().upper()
validator = LicenseValidator(licence_id)
if validator.validate_license():
licence_id = validator.computed_licence_id
else:
raise forms.ValidationError('Le numéro de licence est invalide, la lettre ne correspond pas.')
return licence_id
def clean_phone(self):
@ -91,6 +101,17 @@ 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()
validator = LicenseValidator(licence_id)
if validator.validate_license():
licence_id = validator.computed_licence_id
else:
raise forms.ValidationError('Le numéro de licence est invalide, la lettre ne correspond pas.')
return licence_id
class Meta:
model = CustomUser
@ -209,7 +230,12 @@ class ProfileUpdateForm(forms.ModelForm):
def clean_licence_id(self):
licence_id = self.cleaned_data.get('licence_id')
if licence_id:
return licence_id.replace(' ', '').upper()
licence_id = licence_id.replace(' ', '').strip().upper()
validator = LicenseValidator(licence_id)
if validator.validate_license():
licence_id = validator.computed_licence_id
else:
raise forms.ValidationError('Le numéro de licence est invalide, la lettre ne correspond pas.')
return licence_id
def clean_phone(self):

@ -148,6 +148,41 @@ class OnlineRegistrationStatus(models.IntegerChoices):
ENDED_WITH_RESULTS = 8, 'Ended with Results'
CANCELED = 9, 'Canceled'
def display_register_option(self) -> bool:
status_map = {
OnlineRegistrationStatus.OPEN: True,
OnlineRegistrationStatus.NOT_ENABLED: False,
OnlineRegistrationStatus.NOT_STARTED: False,
OnlineRegistrationStatus.ENDED: False,
OnlineRegistrationStatus.WAITING_LIST_POSSIBLE: True,
OnlineRegistrationStatus.WAITING_LIST_FULL: False,
OnlineRegistrationStatus.IN_PROGRESS: False,
OnlineRegistrationStatus.ENDED_WITH_RESULTS: False,
OnlineRegistrationStatus.CANCELED: False
}
return status_map.get(self, False)
def register_button_text(self) -> str:
if self == OnlineRegistrationStatus.OPEN:
return "S'inscrire"
elif self == OnlineRegistrationStatus.NOT_ENABLED:
return "Inscription désactivée"
elif self == OnlineRegistrationStatus.NOT_STARTED:
return "Ouverture des inscriptions à venir"
elif self == OnlineRegistrationStatus.ENDED:
return "Inscription terminée"
elif self == OnlineRegistrationStatus.WAITING_LIST_POSSIBLE:
return "S'inscrire en liste d'attente"
elif self == OnlineRegistrationStatus.WAITING_LIST_FULL:
return "Liste d'attente complète"
elif self == OnlineRegistrationStatus.IN_PROGRESS:
return "Tournoi en cours"
elif self == OnlineRegistrationStatus.ENDED_WITH_RESULTS:
return "Tournoi terminé"
elif self == OnlineRegistrationStatus.CANCELED:
return "Tournoi annulé"
return ""
def status_localized(self) -> str:
status_map = {
OnlineRegistrationStatus.OPEN: "Inscription ouverte",

@ -1066,42 +1066,6 @@ class Tournament(BaseModel):
return options
def online_register_is_enabled(self):
if self.supposedly_in_progress():
return False
if self.closed_registration_date is not None:
return False
if self.end_date is not None:
return False
now = timezone.now()
# Check if online registration is enabled
if not self.enable_online_registration:
return False
# Check opening registration date
if self.opening_registration_date is not None:
timezoned_datetime = timezone.localtime(self.opening_registration_date)
if now < timezoned_datetime:
return False
# Check registration date limit
if self.registration_date_limit is not None:
timezoned_datetime = timezone.localtime(self.registration_date_limit)
if now > timezoned_datetime:
return False
# Check target team count and waiting list limit
if self.team_count is not None:
current_team_count = self.team_registrations.exclude(walk_out=True).count()
if current_team_count >= self.team_count:
if self.waiting_list_limit is not None:
waiting_list_count = current_team_count - self.team_count
if waiting_list_count >= self.waiting_list_limit:
return False
return True
def get_selection_status_localized(self):
if self.team_sorting == TeamSortingType.RANK:
return "La sélection se fait par le poids de l'équipe"
@ -1552,18 +1516,17 @@ class Tournament(BaseModel):
return None
validator = LicenseValidator(licence_id)
stripped_license = validator.stripped_license
if validator.validate_license():
stripped_license = validator.stripped_license
# Check if there is a PlayerRegistration for this user in this tournament
user_player = PlayerRegistration.objects.filter(
licence_id__icontains=stripped_license,
team_registration__tournament=self,
).first()
if user_player:
return user_player.get_registration_status()
# Check if there is a PlayerRegistration for this user in this tournament
user_player = PlayerRegistration.objects.filter(
licence_id__icontains=stripped_license,
team_registration__tournament=self,
).first()
if user_player:
return user_player.get_registration_status()
else:
return None
return None
class MatchGroup:

@ -7,6 +7,9 @@ from ..utils.licence_validator import LicenseValidator
from ..utils.player_search import get_player_name_from_csv
from tournaments.models import PlayerRegistration
from ..utils.extensions import is_not_sqlite_backend
from django.contrib.auth import get_user_model
from django.contrib.messages import get_messages
from django.db import IntegrityError
class TournamentRegistrationService:
def __init__(self, request, tournament):
@ -70,6 +73,10 @@ class TournamentRegistrationService:
if self._is_already_registered(licence_id):
return
if self.request.user.is_authenticated and self.request.user.licence_id is None:
if self._update_user_license(player_data.get('licence_id')) == False:
return
if self.request.user.licence_id is None and len(self.context['current_players']) == 0:
# if no licence id for authentificated user and trying to add him as first player of the team, we check his federal data
self._handle_invalid_names(licence_id, player_data)
@ -80,9 +87,6 @@ class TournamentRegistrationService:
else:
self._handle_invalid_names(licence_id, player_data)
if self.request.user.is_authenticated and self.request.user.licence_id is None:
self._update_user_license(player_data.get('licence_id'))
def handle_team_registration(self):
if not self.context['team_form'].is_valid():
return
@ -119,7 +123,7 @@ class TournamentRegistrationService:
self.context['registration_successful'] = True
def handle_get_request(self):
from django.contrib.messages import get_messages
print("handle_get_request")
storage = get_messages(self.request)
# Iterate through the storage to clear it
for _ in storage:
@ -180,8 +184,6 @@ class TournamentRegistrationService:
self.request.session.modified = True
def _get_authenticated_user_data(self):
from ..utils.player_search import get_player_name_from_csv
from ..utils.licence_validator import LicenseValidator
user = self.request.user
validator = LicenseValidator(user.licence_id)
@ -209,6 +211,7 @@ class TournamentRegistrationService:
return player_data
def _validate_license(self, licence_id):
print("Validating license...")
validator = LicenseValidator(licence_id)
if validator.validate_license() is False and self.tournament.license_is_required:
@ -221,6 +224,7 @@ class TournamentRegistrationService:
# computed_license_key = validator.computed_license_key
# messages.error(self.request, f"Le numéro de licence est invalide, la lettre ne correspond pas. {computed_license_key}")
messages.error(self.request, "Le numéro de licence est invalide, la lettre ne correspond pas.")
print("License validation failed")
return False
return True
@ -276,16 +280,26 @@ class TournamentRegistrationService:
player_data['is_woman'] = self.request.session.get('is_woman', False)
def _update_user_license(self, licence_id):
if self.request.user.is_authenticated and licence_id:
self.context['add_player_form'].user_without_licence = False
validator = LicenseValidator(licence_id)
self.request.user.licence_id = validator.computed_licence_id
self.request.user.save()
self.request.user.refresh_from_db()
self.request.session.modified = True
# Reset the form state
self.context['add_player_form'] = AddPlayerForm()
self.context['add_player_form'].first_tournament = False
if not self.request.user.is_authenticated or not licence_id:
return False
self.context['add_player_form'].user_without_licence = False
validator = LicenseValidator(licence_id)
if validator.validate_license():
computed_licence_id = validator.computed_licence_id
try:
self.request.user.licence_id = computed_licence_id
self.request.user.save()
self.request.user.refresh_from_db()
self.request.session.modified = True
return True
except IntegrityError:
# Handle the duplicate license error
error_msg = f"Ce numéro de licence ({computed_licence_id}) est déjà utilisé par un autre joueur."
messages.error(self.request, error_msg)
return False
def _update_player_data_from_csv(self, player_data, csv_data):
print("_update_player_data_from_csv", player_data, csv_data)
@ -305,16 +319,15 @@ class TournamentRegistrationService:
'phone': None,
})
from django.contrib.auth import get_user_model
User = get_user_model()
# Get the license ID from player_data
licence_id = player_data.get('licence_id')
validator = LicenseValidator(licence_id)
if validator and validator.stripped_license:
if validator.validate_license():
try:
# Try to find a user with matching license
user_with_same_license = User.objects.get(licence_id__icontains=validator.stripped_license)
user_with_same_license = User.objects.get(licence_id__iexact=validator.computed_licence_id)
# If found, update the email and phone
if user_with_same_license:

@ -111,75 +111,75 @@
</p>
{% endif %}
{% if tournament.enable_online_registration %}
<p>
<div class="semibold">Inscription en ligne</div>
{% if tournament.options_online_registration %}
<ul>
{% for option in tournament.options_online_registration %}
<li>{{ option }}</li>
{% endfor %}
</ul>
{% endif %}
</p>
{% with status=tournament.get_online_registration_status %}
{% if tournament.enable_online_registration %}
<p>
<div class="semibold">Inscription en ligne</div>
<p>
<div class="semibold">
{% if tournament.registration_count_display %}
<div>{{ tournament.registration_count_display }}</div>
{% else %}
<div>Aucune équipe inscrite</div>
{% endif %}
</div>
</p>
{% if tournament.options_online_registration %}
<ul>
{% for option in tournament.options_online_registration %}
<li>{{ option }}</li>
{% endfor %}
</ul>
{% endif %}
</p>
<p>
<div class="semibold">
{{ tournament.get_online_registration_status.status_localized }}
</div>
</p>
<p>
<div class="semibold">
{% if tournament.registration_count_display %}
<div>{{ tournament.registration_count_display }}</div>
{% else %}
<div>Aucune équipe inscrite</div>
{% endif %}
</div>
</p>
{% endif %}
<p>
<div class="semibold">
{{ status.status_localized }}
</div>
</p>
{% endif %}
{% if tournament.online_register_is_enabled and team is None %}
{% if tournament.account_is_required is False or user.is_authenticated and user.is_active %}
{% if player_register_check is None %}
{% if status.display_register_option and team is None %}
{% if tournament.account_is_required is False or user.is_authenticated and user.is_active %}
{% if player_register_check is None %}
<p>
<div>
<a href="{% url 'register_tournament' tournament.id %}" class="rounded-button">{{ status.register_button_text }}</a>
</div>
</p>
{% else %}
<p>
<div class="semibold">
Vous ne pouvez pas vous inscrire à ce tournoi.
</div>
<div class="alert">
{% for reason in player_register_check %}
<div>{{ reason }}</div>
{% endfor %}
</div>
</p>
{% endif %}
{% else %}
<p>
<div>
<a href="{% url 'register_tournament' tournament.id %}" class="rounded-button">S'inscrire</a>
<a href="{% url 'login' %}?next={{ request.path }}" class="styled-link">Connectez-vous !</a>
</div>
</p>
{% else %}
<p>
<div class="semibold">
Vous ne pouvez pas vous inscrire à ce tournoi.
<div>
Vous avez besoin d'un compte Padel Club pour pouvoir vous inscrire en ligne.
</div>
<div class="alert">
{% for reason in player_register_check %}
<div>{{ reason }}</div>
{% endfor %}
<div>
<a href="{% url 'signup' %}?next={{ request.path }}" class="styled-link">Créer le tout de suite !</a>
</div>
</p>
{% endif %}
{% else %}
<p>
<div>
<a href="{% url 'login' %}?next={{ request.path }}" class="styled-link">Connectez-vous !</a>
</div>
</p>
<p>
<div>
Vous avez besoin d'un compte Padel Club pour pouvoir vous inscrire en ligne.
</div>
<div>
<a href="{% url 'signup' %}?next={{ request.path }}" class="styled-link">Créer le tout de suite !</a>
</div>
</p>
{% endif %}
{% endif %}
{% endwith %}
</div>
</div>
</div>

@ -15,7 +15,7 @@ class LicenseValidator:
@property
def computed_licence_id(self) -> str:
return f"{self.stripped_license}{self.computed_license_key}"
return f"{self.stripped_license}{self.computed_license_key.upper()}"
@property
def computed_license_key(self) -> str:
@ -45,4 +45,4 @@ class LicenseValidator:
given_letter = self.license_id[-1]
# Verify that the last character matches the computed license key
return self.computed_license_key == given_letter
return self.computed_licence_id == numeric_part + given_letter

Loading…
Cancel
Save