13
13
from rest_framework .permissions import IsAuthenticated
14
14
from rest_framework .response import Response
15
15
16
- from insalan .tournament .models import Event , Player , PaymentStatus , Manager
16
+ from insalan .tournament .models import Event , Player , PaymentStatus , Manager , Substitute
17
17
from insalan .user .models import User
18
18
19
19
from .models import LangateReply , TournamentRegistration
20
20
from .serializers import ReplySerializer
21
21
22
+ from insalan .user .serializers import UserLoginSerializer
22
23
23
24
class LangateUserView (CreateAPIView ):
24
25
"""
25
26
API endpoint used by the langate to authenticate and verify a user's data
26
27
"""
27
28
authentication_classes = [SessionAuthentication ]
28
- permission_classes = [IsAuthenticated ]
29
29
serializer_class = ReplySerializer
30
30
31
31
def post (self , request , * args , ** kwargs ):
@@ -45,10 +45,20 @@ def post(self, request, *args, **kwargs):
45
45
Our response is lengthier, and should contain all of the information
46
46
necessary for the langate to identify the user.
47
47
"""
48
- # Name of the user
48
+ # authenticate the user
49
+ data = request .data
50
+ serializer = UserLoginSerializer (data = data , context = {"request" : request })
51
+ if serializer .is_valid ():
52
+ user = serializer .check_validity (data )
53
+ if user is None :
54
+ return Response (
55
+ {"user" : [_ ("Nom d'utilisateur·rice ou mot de passe incorrect" )]},
56
+ status = status .HTTP_404_NOT_FOUND ,
57
+ )
58
+
49
59
# If we reached here, they are authenticated correctly, so now we
50
60
# fetch their data
51
- gate_user = request . user
61
+ gate_user = data . get ( "username" )
52
62
53
63
# Attempt to determine what the event id is
54
64
# How many ongoing events are there?
@@ -59,33 +69,8 @@ def post(self, request, *args, **kwargs):
59
69
status = status .HTTP_500_INTERNAL_SERVER_ERROR ,
60
70
)
61
71
62
- event_id = request .data .get ("event_id" )
63
- if event_id is not None :
64
- event_id = int (event_id )
65
-
66
- # If there is only one event...
67
- if len (ongoing_events ) == 1 :
68
- # ...and there is none provided: use it
69
- if event_id is not None and ongoing_events [0 ] != event_id :
70
- return Response (
71
- {"err" : _ ("Évènement demandé incompatible" )},
72
- status = status .HTTP_400_BAD_REQUEST ,
73
- )
74
- event_id = ongoing_events [0 ]
75
- else :
76
- if event_id is None :
77
- return Response (
78
- {"err" : _ ("Identifiant manquant" )}, status = status .HTTP_400_BAD_REQUEST
79
- )
80
-
81
- if not event_id in ongoing_events :
82
- return Response (
83
- {"err" : _ ("Évènement non en cours" )},
84
- status = status .HTTP_400_BAD_REQUEST ,
85
- )
86
-
87
72
# Get the event
88
- ev_obj = Event .objects .get (id = event_id )
73
+ ev_obj = Event .objects .get (id = ongoing_events [ 0 ] )
89
74
if not ev_obj .ongoing :
90
75
return Response (
91
76
{"err" : _ ("Évènement non en cours" )},
@@ -96,7 +81,8 @@ def post(self, request, *args, **kwargs):
96
81
user_obj = User .objects .get (username = gate_user )
97
82
regs_pl = Player .objects .filter (user = user_obj , team__tournament__event = ev_obj )
98
83
regs_man = Manager .objects .filter (user = user_obj , team__tournament__event = ev_obj )
99
- regs = list (regs_pl ) + list (regs_man )
84
+ regs_sub = Substitute .objects .filter (user = user_obj , team__tournament__event = ev_obj )
85
+ regs = list (regs_pl ) + list (regs_man ) + list (regs_sub )
100
86
101
87
found_count = len (regs )
102
88
0 commit comments