From 8667ea97aca10d630d789b6c0d5cc27679d942ed Mon Sep 17 00:00:00 2001 From: Raz Date: Wed, 16 Apr 2025 17:49:55 +0200 Subject: [PATCH] fix mail doubling --- tournaments/models/team_registration.py | 61 ------------------- .../services/tournament_registration.py | 45 +++++++------- 2 files changed, 24 insertions(+), 82 deletions(-) diff --git a/tournaments/models/team_registration.py b/tournaments/models/team_registration.py index 51964a2..2b039f8 100644 --- a/tournaments/models/team_registration.py +++ b/tournaments/models/team_registration.py @@ -134,67 +134,6 @@ class TeamRegistration(SideStoreModel): else: return "--" - def set_weight(self): - self.weight = self.players_sorted_by_rank.aggregate(total_weight=models.Sum('computed_rank'))['total_weight'] or 0 - - def has_changed(self, previous_instance): - """ - Compare the current instance with a previous instance to detect changes. - - Args: - previous_instance (TeamRegistration): The previous version of the model instance - - Returns: - bool: True if any significant fields have changed, False otherwise - """ - # Define the fields to compare - fields_to_compare = [ - 'weight', - 'registration_date', - 'bracket_position', - 'group_stage_position', - 'walk_out', - 'wild_card_bracket', - 'wild_card_group_stage', - 'locked_weight', - ] - - # Check if previous_instance exists - if previous_instance is None: - return True - - # Compare each field - for field in fields_to_compare: - current_value = getattr(self, field) - previous_value = getattr(previous_instance, field) - - # Special handling for datetime fields to compare precisely - if field in ['registration_date']: - if (current_value is None and previous_value is not None) or \ - (current_value is not None and previous_value is None): - return True - if current_value and previous_value: - # Compare with microsecond precision - if current_value.replace(microsecond=0) != previous_value.replace(microsecond=0): - return True - - # For other fields, use standard comparison - elif current_value != previous_value: - return True - - current_players = list(self.players_sorted_by_rank) - previous_players = list(previous_instance.players_sorted_by_rank) - - if len(current_players) != len(previous_players): - return True - - # Compare player details if needed - for current_player, previous_player in zip(current_players, previous_players): - if current_player.id != previous_player.id: - return True - - return False - def is_valid_for_summon(self): return self.players_sorted_by_rank.count() > 0 or self.name is not None diff --git a/tournaments/services/tournament_registration.py b/tournaments/services/tournament_registration.py index c919924..7b143fa 100644 --- a/tournaments/services/tournament_registration.py +++ b/tournaments/services/tournament_registration.py @@ -316,13 +316,6 @@ class RegistrationCartManager: if len(players) < tournament.minimum_player_per_team: return False, f"Vous avez besoin d'au moins {tournament.minimum_player_per_team} joueurs pour vous inscrire." - # Create team registration - team_registration = TeamRegistration.objects.create( - tournament=tournament, - registration_date=timezone.now(), - walk_out=False - ) - # Identify captain from user's license stripped_license = None if self.request.user.is_authenticated and self.request.user.licence_id: @@ -330,7 +323,27 @@ class RegistrationCartManager: stripped_license = validator.stripped_license # Create player registrations - for player_data in players: + 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 + team_registration = TeamRegistration.objects.create( + tournament=tournament, + registration_date=timezone.now(), + walk_out=False, + weight=weight, + ) + + for player_data in players: # Compute rank and sex using the original logic # Determine if this player is the captain is_captain = False player_licence_id = player_data.get('licence_id') @@ -338,12 +351,6 @@ class RegistrationCartManager: if stripped_license.lower() in player_licence_id.lower(): is_captain = True - # Compute rank and sex using the original logic - sex, rank, computed_rank = self._compute_rank_and_sex( - tournament, - player_data - ) - # Determine data source data_source = None if player_data.get('found_in_french_federation', False) == True: @@ -372,19 +379,15 @@ class RegistrationCartManager: 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, + sex=player_data.get('sex'), + rank=player_data.get('rank'), + computed_rank=player_data.get('computed_rank'), licence_id=player_data.get('licence_id'), email=matching_user.email if matching_user else player_data.get('email'), phone_number=matching_user.phone if matching_user else player_data.get('mobile_number'), registration_status=RegistrationStatus.CONFIRMED if self.session.get('waiting_list_position', 0) < 0 else RegistrationStatus.WAITING ) - # Calculate and set team weight - team_registration.set_weight() - team_registration.save() - # Update user phone if provided if self.request.user.is_authenticated and mobile_number: self.request.user.phone = mobile_number