Skip to content

Commit

Permalink
add exception error handling in team creation
Browse files Browse the repository at this point in the history
  • Loading branch information
istar410 committed Nov 9, 2023
1 parent 0f4ad0b commit a634bd0
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions insalan/tournament/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .models import Event, Tournament, Game, Team, Player, Manager, unique_registration
from insalan.user.models import User
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.hashers import make_password

class EventSerializer(serializers.ModelSerializer):
# pylint: disable=R0903
Expand Down Expand Up @@ -79,7 +80,6 @@ class TeamSerializer(serializers.ModelSerializer):
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)
managers_pseudos = serializers.ListField(required=False, write_only=True)

class Meta:
"""Meta options of the team serializer"""
Expand All @@ -95,20 +95,30 @@ def create(self, validated_data):
players = validated_data.pop("get_players_id", [])
managers = validated_data.pop("get_managers_id", [])
players_pseudos = validated_data.pop("players_pseudos", [])
managers_pseudos = validated_data.pop("managers_pseudos", [])

if len(players_pseudos) != len(players) or len(managers_pseudos) != len(managers):
raise serializers.ValidationError(_("Il manque des pseudos de joueurs et/ou managers"))
if len(players_pseudos) != len(players):
raise serializers.ValidationError(_("Il manque des pseudos de joueurs"))

team_obj = Team.objects.create(**validated_data)
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"))

for player, pseudo in zip(players,players_pseudos):
user_obj = User.objects.get(id=player)
Player.objects.create(user=user_obj, team=team_obj, pseudo=pseudo)
try:
Player.objects.create(user=user_obj, team=team_obj, pseudo=pseudo)
except:
raise serializers.ValidationError(_("Erreur à l'inscription du joueur"))


for manager, pseudo in zip(managers, managers_pseudos):
user_obj = User.objects.get(id=manager)
Manager.objects.create(user=user_obj, team=team_obj, pseudo=pseudo)
try:
Manager.objects.create(user=user_obj, team=team_obj, pseudo=pseudo)
except:
raise serializers.ValidationError(_("Erreur à l'inscription du manager"))

return team_obj

Expand All @@ -117,7 +127,12 @@ def update(self, instance, validated_data):

# Catch the players and managers keywords
if "get_players_id" in validated_data:
players_pseudos = validated_data.pop("players_pseudos", [])
players = set(validated_data.pop("get_players_id", []))

if len(players_pseudos) != len(players):
raise serializers.ValidationError(_("Il manque des pseudos de joueurs"))

existing = set(instance.get_players_id())
removed = existing - players
for uid in removed:
Expand All @@ -136,6 +151,9 @@ def update(self, instance, validated_data):
for uid in new:
Manager.objects.create(user_id=uid, team=instance)

if "password" in validated_data:
validated_data["password"] = make_password(validated_data["password"])

# Update all other fields
super().update(instance, validated_data)

Expand Down

0 comments on commit a634bd0

Please sign in to comment.