6
6
from django .contrib .auth .models import Group
7
7
from django .core .exceptions import ValidationError
8
8
from django .core .validators import validate_email
9
+ from django .utils .timezone import now
9
10
10
11
from rest_framework import permissions
11
12
from rest_framework import status
@@ -92,13 +93,13 @@ def bulk_create_teachers(self, request):
92
93
validate_email (invitee )
93
94
CleanEmailMixin ().validate_invitation (invitee )
94
95
invite = Invitation .create (invitee , group = teacher_group )
95
- except ( ValidationError ) :
96
+ except ValidationError :
96
97
response ["invalid" ].append ({invitee : "invalid email" })
97
- except ( AlreadyAccepted ) :
98
+ except AlreadyAccepted :
98
99
response ["invalid" ].append ({invitee : "already accepted" })
99
- except ( AlreadyInvited ) :
100
+ except AlreadyInvited :
100
101
response ["invalid" ].append ({invitee : "pending invite" })
101
- except ( UserRegisteredEmail ) :
102
+ except UserRegisteredEmail :
102
103
response ["invalid" ].append ({invitee : "user registered email" })
103
104
else :
104
105
invite .send_invitation (request )
@@ -124,5 +125,19 @@ def delete(self, request, *args, **kwargs):
124
125
except Token .DoesNotExist :
125
126
return Response (status = status .HTTP_404_NOT_FOUND )
126
127
128
+ # with thanks to https://chatgpt.com/share/66ee4879-8d84-800f-b18c-7d63efbb2c43
129
+ def post (self , request , * args , ** kwargs ):
130
+ # Call the original implementation to get the authenticated user and token
131
+ response = super ().post (request , * args , ** kwargs )
132
+ token = Token .objects .get (key = response .data ["token" ])
133
+ user = token .user
134
+
135
+ # Update last_login and save the user instance
136
+ user .last_login = now ()
137
+ user .save ()
138
+
139
+ # Return the response with the token and any additional data
140
+ return response
141
+
127
142
128
143
obtain_delete_auth_token = ObtainDeleteAuthToken .as_view ()
0 commit comments