You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
padelclub_backend/tournaments/repositories.py

74 lines
3.0 KiB

from django.utils import timezone
from .models import TeamRegistration, PlayerRegistration
from .models.player_enums import PlayerSexType, PlayerDataSource
from .models.enums import FederalCategory
from tournaments.utils.licence_validator import LicenseValidator
class TournamentRegistrationRepository:
@staticmethod
def create_team_registration(tournament, registration_date):
team_registration = TeamRegistration.objects.create(
tournament=tournament,
registration_date=registration_date
)
return team_registration
@staticmethod
def create_player_registrations(request, team_registration, players_data, team_form_data):
stripped_license = None
if request.user.is_authenticated and request.user.licence_id:
stripped_license = LicenseValidator(request.user.licence_id).stripped_license
for player_data in players_data:
is_captain = False
player_licence_id = player_data['licence_id']
if player_licence_id and stripped_license:
if player_licence_id.startswith(stripped_license):
is_captain = True
sex, rank, computed_rank = TournamentRegistrationRepository._compute_rank_and_sex(
team_registration.tournament,
player_data
)
player_registration = PlayerRegistration.objects.create(
team_registration=team_registration,
captain=is_captain,
source=PlayerDataSource.ONLINE_REGISTRATION,
first_name=player_data.get('first_name'),
last_name=player_data.get('last_name'),
points=player_data.get('points'),
assimilation=player_data.get('assimilation'),
tournament_played=player_data.get('tournament_count'),
ligue_name=player_data.get('ligue_name'),
club_name=player_data.get('club_name'),
birthdate=player_data.get('birth_year'),
sex=sex,
rank=rank,
computed_rank=computed_rank,
licence_id=player_data['licence_id'],
)
if is_captain is True:
player_registration.email=team_form_data['email']
player_registration.phone_number=team_form_data['mobile_number']
player_registration.save()
team_registration.set_weight()
team_registration.save()
@staticmethod
def _compute_rank_and_sex(tournament, player_data):
is_woman = player_data.get('is_woman', False)
rank = player_data.get('rank', 0)
computed_rank = rank
sex = PlayerSexType.MALE
if is_woman:
sex = PlayerSexType.FEMALE
if tournament.federal_category == FederalCategory.MEN:
computed_rank = str(int(computed_rank) +
FederalCategory.female_in_male_assimilation_addition(int(rank)))
return sex, rank, computed_rank