|
|
|
|
@ -7,6 +7,7 @@ from ..utils.player_search import get_player_name_from_csv |
|
|
|
|
from ..models.enums import FederalCategory, RegistrationStatus |
|
|
|
|
from ..models.player_enums import PlayerSexType, PlayerDataSource |
|
|
|
|
from django.contrib.auth import get_user_model |
|
|
|
|
from django.conf import settings |
|
|
|
|
|
|
|
|
|
class RegistrationCartManager: |
|
|
|
|
""" |
|
|
|
|
@ -20,6 +21,7 @@ class RegistrationCartManager: |
|
|
|
|
def __init__(self, request): |
|
|
|
|
self.request = request |
|
|
|
|
self.session = request.session |
|
|
|
|
self.first_tournament = False |
|
|
|
|
|
|
|
|
|
def get_or_create_cart_id(self): |
|
|
|
|
"""Get or create a registration cart ID in the session""" |
|
|
|
|
@ -165,10 +167,20 @@ class RegistrationCartManager: |
|
|
|
|
if not result[0]: |
|
|
|
|
return result # Return the error |
|
|
|
|
|
|
|
|
|
# If we got here, license validation passed |
|
|
|
|
tournament_federal_category = tournament.federal_category |
|
|
|
|
if tournament_federal_category == FederalCategory.MIXED and len(players) == 1: |
|
|
|
|
other_player_is_woman = players[0].get('is_woman', False) |
|
|
|
|
if other_player_is_woman is False: |
|
|
|
|
tournament_federal_category = FederalCategory.WOMEN |
|
|
|
|
|
|
|
|
|
if licence_id: |
|
|
|
|
# Get federation data |
|
|
|
|
fed_data, found = get_player_name_from_csv(tournament.federal_category, licence_id) |
|
|
|
|
fed_data, found = get_player_name_from_csv(tournament_federal_category, licence_id) |
|
|
|
|
if found is False and fed_data: |
|
|
|
|
player_data.update({ |
|
|
|
|
'rank': fed_data['rank'], |
|
|
|
|
'is_woman': fed_data['is_woman'], |
|
|
|
|
}) |
|
|
|
|
if found and fed_data: |
|
|
|
|
# Use federation data (including check for eligibility) |
|
|
|
|
player_register_check = tournament.player_register_check(licence_id) |
|
|
|
|
@ -189,34 +201,37 @@ class RegistrationCartManager: |
|
|
|
|
'birth_year': fed_data.get('birth_year'), |
|
|
|
|
'found_in_french_federation': True, |
|
|
|
|
}) |
|
|
|
|
elif tournament.license_is_required: |
|
|
|
|
# License required but not found in federation data |
|
|
|
|
return False, "La licence fournit n'a pas été trouvée dans la base FFT. Contactez le juge arbitre si cette licence est valide." |
|
|
|
|
elif not first_name or not last_name: |
|
|
|
|
# License not required or not found, but name is needed |
|
|
|
|
return False, "Le prénom et le nom sont obligatoires pour les joueurs sans licence." |
|
|
|
|
self.first_tournament = True |
|
|
|
|
return False, "Le prénom et le nom sont obligatoires pour les joueurs dont la licence n'a pas été trouvée." |
|
|
|
|
elif not tournament.license_is_required: |
|
|
|
|
# License not required, check if name is provided |
|
|
|
|
if not first_name or not last_name: |
|
|
|
|
return False, "Le prénom et le nom sont obligatoires pour les joueurs sans licence." |
|
|
|
|
|
|
|
|
|
# Set default rank for players without a license |
|
|
|
|
if player_data.get('rank') is None: |
|
|
|
|
default_data, _ = get_player_name_from_csv(tournament.federal_category, None) |
|
|
|
|
if default_data: |
|
|
|
|
player_data['rank'] = default_data.get('rank') |
|
|
|
|
player_data['is_woman'] = default_data.get('is_woman', False) |
|
|
|
|
self.first_tournament = True |
|
|
|
|
return False, "Le prénom et le nom sont obligatoires." |
|
|
|
|
else: |
|
|
|
|
# License is required but not provided |
|
|
|
|
return False, "Le numéro de licence est obligatoire." |
|
|
|
|
|
|
|
|
|
# Create player registrations |
|
|
|
|
sex, rank, computed_rank = self._compute_rank_and_sex( |
|
|
|
|
tournament, |
|
|
|
|
player_data |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
player_data['computed_rank'] = computed_rank |
|
|
|
|
|
|
|
|
|
# Add player to cart |
|
|
|
|
players.append(player_data) |
|
|
|
|
self.session['registration_cart_players'] = players |
|
|
|
|
self.reset_cart_expiry() |
|
|
|
|
self.session.modified = True |
|
|
|
|
|
|
|
|
|
return True, "Joueur ajouté avec succès." |
|
|
|
|
if sex == PlayerSexType.FEMALE: |
|
|
|
|
return True, "Joueuse ajoutée avec succès." |
|
|
|
|
else: |
|
|
|
|
return True, "Joueur ajouté avec succès." |
|
|
|
|
|
|
|
|
|
def _process_player_license(self, tournament, licence_id, first_name, last_name, players, is_first_player): |
|
|
|
|
""" |
|
|
|
|
@ -234,7 +249,10 @@ class RegistrationCartManager: |
|
|
|
|
# Validate the license format |
|
|
|
|
validator = LicenseValidator(licence_id) |
|
|
|
|
if not validator.validate_license(): |
|
|
|
|
return False, "Le numéro de licence est invalide, la lettre ne correspond pas." |
|
|
|
|
if settings.DEBUG: |
|
|
|
|
return False, f"Le numéro de licence est invalide, la lettre ne correspond pas. {validator.get_computed_license_key(validator.stripped_license)}" |
|
|
|
|
else: |
|
|
|
|
return False, "Le numéro de licence est invalide, la lettre ne correspond pas." |
|
|
|
|
|
|
|
|
|
# Check if player is already registered in tournament |
|
|
|
|
stripped_license = validator.stripped_license |
|
|
|
|
@ -322,17 +340,6 @@ class RegistrationCartManager: |
|
|
|
|
validator = LicenseValidator(self.request.user.licence_id) |
|
|
|
|
stripped_license = validator.stripped_license |
|
|
|
|
|
|
|
|
|
# Create player registrations |
|
|
|
|
for player_data in players: # Compute rank and sex using the original logic |
|
|
|
|
sex, rank, computed_rank = self._compute_rank_and_sex( |
|
|
|
|
tournament, |
|
|
|
|
player_data |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
player_data['sex'] = sex |
|
|
|
|
player_data['rank'] = rank |
|
|
|
|
player_data['computed_rank'] = computed_rank |
|
|
|
|
|
|
|
|
|
weight = sum(int(player_data.get('computed_rank', 0) or 0) for player_data in players) |
|
|
|
|
|
|
|
|
|
# Create team registration |
|
|
|
|
@ -455,18 +462,26 @@ class RegistrationCartManager: |
|
|
|
|
rank = player_data.get('rank', None) |
|
|
|
|
|
|
|
|
|
if rank is None: |
|
|
|
|
rank_int = None |
|
|
|
|
computed_rank = 100000 |
|
|
|
|
else: |
|
|
|
|
computed_rank = rank |
|
|
|
|
# Ensure rank is an integer for calculations |
|
|
|
|
try: |
|
|
|
|
rank_int = int(rank) |
|
|
|
|
computed_rank = rank_int |
|
|
|
|
except (ValueError, TypeError): |
|
|
|
|
# If rank can't be converted to int, set a default |
|
|
|
|
rank_int = None |
|
|
|
|
computed_rank = 100000 |
|
|
|
|
|
|
|
|
|
# Use the integer enum values |
|
|
|
|
sex = PlayerSexType.FEMALE if is_woman else PlayerSexType.MALE |
|
|
|
|
|
|
|
|
|
# Apply assimilation for women playing in men's tournaments |
|
|
|
|
if is_woman and tournament.federal_category == FederalCategory.MEN: |
|
|
|
|
assimilation_addition = FederalCategory.female_in_male_assimilation_addition(rank) |
|
|
|
|
if is_woman and tournament.federal_category == FederalCategory.MEN and rank_int is not None: |
|
|
|
|
assimilation_addition = FederalCategory.female_in_male_assimilation_addition(rank_int) |
|
|
|
|
computed_rank = computed_rank + assimilation_addition |
|
|
|
|
|
|
|
|
|
print(f"_compute_rank_and_sex: {player_data.get('last_name')}, {sex}, {rank}, {computed_rank}") |
|
|
|
|
|
|
|
|
|
return sex, rank, computed_rank |
|
|
|
|
return sex, rank, str(computed_rank) |
|
|
|
|
|