From a3aab3cf21e2ee5a6a9798e8d82d98a697a5aa1d Mon Sep 17 00:00:00 2001 From: Arthur Branchu-Harel Date: Fri, 10 Nov 2023 00:41:07 +0100 Subject: [PATCH] fix unique registration validation --- insalan/tournament/models.py | 57 +++++++++++++++++-------------- insalan/tournament/serializers.py | 27 +++++++-------- insalan/tournament/views.py | 4 +-- 3 files changed, 45 insertions(+), 43 deletions(-) diff --git a/insalan/tournament/models.py b/insalan/tournament/models.py index 47477dfa..732ee97f 100644 --- a/insalan/tournament/models.py +++ b/insalan/tournament/models.py @@ -469,12 +469,11 @@ def unique_registration(users: Union[User, List[User]]): for user in users: unique_registration(user) else: - e_regs = [obj.team.tournament.event for obj in Player.objects.filter(user=users)] + \ - [obj.team.tournament.event for obj in Manager.objects.filter(user=users)] - if (len(e_regs) != len(set(e_regs))): - raise serializers.ValidationError( - _("Utilisateur⋅rice déjà inscrit⋅e dans un tournoi de cet évènement") - ) + e_regs = Event.objects.filter(tournament__team__player__user=users).union(Event.objects.filter(tournament__team__manager__user=users)) + if len(e_regs) > 0: + return False + else: + return True class Player(models.Model): @@ -493,7 +492,7 @@ class Meta: User, on_delete=models.CASCADE, verbose_name=_("Utilisateur⋅ice"), - validators=[player_manager_user_unique_validator,unique_registration], + #validators=[player_manager_user_unique_validator,unique_registration], ) team = models.ForeignKey( "tournament.Team", @@ -545,26 +544,32 @@ def clean(self): Assert that the user associated with the provided player does not already exist in any team of any tournament of the event """ - event = self.get_team().get_tournament().get_event() - - if ( - len( - [ - player.user - for players in [ - team.get_players() - for teams in [ - trnm.get_teams() for trnm in event.get_tournaments() - ] - for team in teams - ] - for player in players - if player.user == self.user - ] + if not unique_registration(self.user): + raise ValidationError( + _("Utilisateur⋅rice déjà inscrit⋅e dans un tournoi de cet évènement") ) - > 1 - ): - raise ValidationError(_("Joueur⋅euse déjà inscrit⋅e pour cet évènement")) + + # event = self.get_team().get_tournament().get_event() + # raise ValidationError("test2") + + # if ( + # len( + # [ + # player.user + # for players in [ + # team.get_players() + # for teams in [ + # trnm.get_teams() for trnm in event.get_tournaments() + # ] + # for team in teams + # ] + # for player in players + # if player.user == self.user + # ] + # ) + # > 0 + # ): + # raise ValidationError(_("Joueur⋅euse déjà inscrit⋅e pour cet évènement")) class Manager(models.Model): diff --git a/insalan/tournament/serializers.py b/insalan/tournament/serializers.py index eeb534c1..47bad69c 100644 --- a/insalan/tournament/serializers.py +++ b/insalan/tournament/serializers.py @@ -76,7 +76,7 @@ def to_representation(self, instance): class TeamSerializer(serializers.ModelSerializer): """Serializer class for Teams""" - players = serializers.ListField(required=False, source="get_players_id", validators=[unique_registration]) + players = serializers.ListField(required=False, source="get_players_id") managers = serializers.ListField(required=False, source="get_managers_id", validators=[unique_registration]) password = serializers.CharField(write_only=True) players_pseudos = serializers.ListField(required=False, write_only=True) @@ -100,25 +100,15 @@ def create(self, validated_data): raise serializers.ValidationError(_("Il manque des pseudos de joueurs")) validated_data["password"] = make_password(validated_data["password"]) - try: - team_obj = Team.objects.create(**validated_data) - except: - raise serializers.ValidationError(_("Erreur à la création de l'équipe")) + team_obj = Team.objects.create(**validated_data) for player, pseudo in zip(players,players_pseudos): user_obj = User.objects.get(id=player) - try: - Player.objects.create(user=user_obj, team=team_obj, pseudo=pseudo) - except: - raise serializers.ValidationError(_("Erreur à l'inscription du joueur")) - + Player.objects.create(user=user_obj, team=team_obj, pseudo=pseudo) - for manager, pseudo in zip(managers, managers_pseudos): + for manager in managers: user_obj = User.objects.get(id=manager) - try: - Manager.objects.create(user=user_obj, team=team_obj, pseudo=pseudo) - except: - raise serializers.ValidationError(_("Erreur à l'inscription du manager")) + Manager.objects.create(user=user_obj, team=team_obj) return team_obj @@ -169,6 +159,13 @@ class Meta: model = Player fields = "__all__" + def validate_user(self, user): + if not unique_registration(user): + raise serializers.ValidationError( + _("Utilisateur⋅rice déjà inscrit⋅e dans un tournoi de cet évènement") + ) + return user + class PlayerIdSerializer(serializers.Serializer): """Serializer to verify a list of player IDs""" diff --git a/insalan/tournament/views.py b/insalan/tournament/views.py index 5841bceb..18f17b08 100644 --- a/insalan/tournament/views.py +++ b/insalan/tournament/views.py @@ -274,7 +274,7 @@ def post(self, request, *args, **kwargs): } ) - if check_password(data["password"], Team.objects.get(pk=data["team"]).get_password()): + if not check_password(data["password"], Team.objects.get(pk=data["team"]).get_password()): return Response( { "password": _("Mot de passe invalide.")}, status=status.HTTP_400_BAD_REQUEST @@ -354,7 +354,7 @@ def post(self, request, *args, **kwargs): } ) - if check_password(data["password"], Team.objects.get(pk=data["team"]).get_password()): + if not check_password(data["password"], Team.objects.get(pk=data["team"]).get_password()): return Response( { "password": _("Mot de passe invalide.")}, status=status.HTTP_400_BAD_REQUEST