diff --git a/insalan/tournament/views/player.py b/insalan/tournament/views/player.py index 771016a9..40261e59 100644 --- a/insalan/tournament/views/player.py +++ b/insalan/tournament/views/player.py @@ -12,6 +12,7 @@ from rest_framework.views import APIView from rest_framework.authentication import SessionAuthentication +from insalan.tournament.models.validators import valid_name from insalan.user.models import User import insalan.tournament.serializers as serializers @@ -45,10 +46,22 @@ def patch(self, request, *args, **kwargs): }, status=status.HTTP_403_FORBIDDEN) if "name_in_game" in data: - player.name_in_game = data["name_in_game"] - - player.save() + if(valid_name(player.team.tournament.game, data["name_in_game"])): + player.name_in_game = data["name_in_game"] + else: + return Response( + {"name_in_game": _("Pseudo invalide")}, + status=status.HTTP_400_BAD_REQUEST + ) + try: + player.save() + except Exception as exc: + return Response( + {"player": str(exc)}, + status=status.HTTP_400_BAD_REQUEST + ) + serializer = serializers.PlayerSerializer(player, context={"request": request}) return Response(serializer.data, status=status.HTTP_200_OK) diff --git a/insalan/tournament/views/substitute.py b/insalan/tournament/views/substitute.py index 608a2b2b..329beb95 100644 --- a/insalan/tournament/views/substitute.py +++ b/insalan/tournament/views/substitute.py @@ -12,6 +12,7 @@ from rest_framework.views import APIView from rest_framework.authentication import SessionAuthentication +from insalan.tournament.models.validators import valid_name from insalan.user.models import User import insalan.tournament.serializers as serializers @@ -45,9 +46,21 @@ def patch(self, request, *args, **kwargs): }, status=status.HTTP_403_FORBIDDEN) if "name_in_game" in data: - substitute.name_in_game = data["name_in_game"] + if(valid_name(substitute.team.tournament.game, data["name_in_game"])): + substitute.name_in_game = data["name_in_game"] + else: + return Response( + {"name_in_game": _("Pseudo invalide")}, + status=status.HTTP_400_BAD_REQUEST + ) - substitute.save() + try: + substitute.save() + except Exception as exc: + return Response( + {"player": str(exc)}, + status=status.HTTP_400_BAD_REQUEST + ) serializer = serializers.SubstituteSerializer(substitute, context={"request": request})