diff --git a/tournaments/forms.py b/tournaments/forms.py index 8e344ad..aef159a 100644 --- a/tournaments/forms.py +++ b/tournaments/forms.py @@ -86,9 +86,9 @@ class TournamentRegistrationForm(forms.Form): #last_name = forms.CharField(label='Nom', max_length=50) email = forms.EmailField(label='E-mail', widget=forms.EmailInput(attrs={'readonly': 'readonly'})) mobile_number = forms.CharField( - label='Téléphone (facultatif)', + label='Téléphone', max_length=15, - required=False + required=True ) def clean_mobile_number(self): @@ -106,6 +106,7 @@ class AddPlayerForm(forms.Form): first_name = forms.CharField(label='Prénom', min_length=2, max_length=50, required=False) last_name = forms.CharField(label='Nom', min_length=2, max_length=50, required=False) first_tournament = False + user_without_licence = False def names_is_valid(self): first_name = self.cleaned_data.get('first_name') diff --git a/tournaments/services/tournament_registration.py b/tournaments/services/tournament_registration.py index 2d4c7d6..5294434 100644 --- a/tournaments/services/tournament_registration.py +++ b/tournaments/services/tournament_registration.py @@ -64,6 +64,12 @@ class TournamentRegistrationService: if not self.context['team_form'].is_valid(): return + if self.request.user.is_authenticated: + cleaned_data = self.context['team_form'].cleaned_data + mobile_number = cleaned_data.get('mobile_number') + self.request.user.phone = mobile_number + self.request.user.save() + waiting_list_position = self.tournament.get_waiting_list_position() team_registration = self.repository.create_team_registration( @@ -98,6 +104,9 @@ class TournamentRegistrationService: self.request.session['team_registration'] = [] self.request.session['team_registration'].append(player_data) + self.context['current_players'] = self.request.session.get('team_registration', []) + self.context['add_player_form'].first_tournament = False + self.context['add_player_form'].user_without_licence = False self.request.session.modified = True def clear_session_data(self): @@ -109,7 +118,7 @@ class TournamentRegistrationService: if self.request.user.is_authenticated: initial_data = { 'email': self.request.user.email, - 'phone': self.request.user.phone, + 'mobile_number': self.request.user.phone, } return TournamentRegistrationForm(initial=initial_data) @@ -136,7 +145,7 @@ class TournamentRegistrationService: 'last_name': self.request.user.last_name.upper(), } self.context['add_player_form'] = AddPlayerForm(initial=player_data) - self.request.session['user_without_licence'] = True + self.context['add_player_form'].user_without_licence = True self.request.session.modified = True def _get_authenticated_user_data(self): @@ -208,8 +217,11 @@ class TournamentRegistrationService: if self.request.user.is_authenticated and self.request.user.licence_id is None: self._update_user_license(player_data.get('licence_id')) + self.context['add_player_form'].user_without_licence = False + self.request.session.modified = True self.context['add_player_form'] = AddPlayerForm() + self.context['add_player_form'].first_tournament = False def _handle_invalid_names(self, licence_id, player_data): if not self.context['add_player_form'].first_tournament: @@ -237,9 +249,15 @@ class TournamentRegistrationService: player_data['is_woman'] = self.request.session.get('is_woman', False) def _update_user_license(self, licence_id): - self.request.session['user_without_licence'] = False - self.request.user.licence_id = LicenseValidator(licence_id).computed_licence_id - self.request.user.save() + 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() + # Reset the form state + self.context['add_player_form'] = AddPlayerForm() + self.context['add_player_form'].first_tournament = False def _update_player_data_from_csv(self, player_data, csv_data): player_data.update({ diff --git a/tournaments/templates/register_tournament.html b/tournaments/templates/register_tournament.html index de84e2c..5d5a262 100644 --- a/tournaments/templates/register_tournament.html +++ b/tournaments/templates/register_tournament.html @@ -61,7 +61,7 @@ Inscrivez votre partenaire {% endif %} - {% if user_without_licence and tournament.license_is_required %} + {% if current_players|length == 0 and add_player_form.user_without_licence and tournament.license_is_required %}
Une licence est obligatoire pour vous inscrire :
@@ -71,8 +71,8 @@ {{ add_player_form.licence_id.label_tag }} {{ add_player_form.licence_id }} {% endif %} - {% if add_player_form.first_tournament or user_without_licence or tournament.license_is_required is False %} - {% if not user_without_licence and tournament.license_is_required is True %} + {% if add_player_form.first_tournament or add_player_form.user_without_licence or tournament.license_is_required is False %} + {% if not add_player_form.user_without_licence and tournament.license_is_required is True %}
Précisez les informations du joueur :
@@ -95,7 +95,7 @@