Skip to content

Commit

Permalink
fix unique registration validation
Browse files Browse the repository at this point in the history
  • Loading branch information
istar410 committed Nov 9, 2023
1 parent a634bd0 commit a3aab3c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 43 deletions.
57 changes: 31 additions & 26 deletions insalan/tournament/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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",
Expand Down Expand Up @@ -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):
Expand Down
27 changes: 12 additions & 15 deletions insalan/tournament/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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"""
Expand Down
4 changes: 2 additions & 2 deletions insalan/tournament/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a3aab3c

Please sign in to comment.