Skip to content

Commit 577b880

Browse files
committed
track users' last_login
1 parent 7c43a81 commit 577b880

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

teleband/users/api/views.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.contrib.auth.models import Group
77
from django.core.exceptions import ValidationError
88
from django.core.validators import validate_email
9+
from django.utils.timezone import now
910

1011
from rest_framework import permissions
1112
from rest_framework import status
@@ -92,13 +93,13 @@ def bulk_create_teachers(self, request):
9293
validate_email(invitee)
9394
CleanEmailMixin().validate_invitation(invitee)
9495
invite = Invitation.create(invitee, group=teacher_group)
95-
except (ValidationError):
96+
except ValidationError:
9697
response["invalid"].append({invitee: "invalid email"})
97-
except (AlreadyAccepted):
98+
except AlreadyAccepted:
9899
response["invalid"].append({invitee: "already accepted"})
99-
except (AlreadyInvited):
100+
except AlreadyInvited:
100101
response["invalid"].append({invitee: "pending invite"})
101-
except (UserRegisteredEmail):
102+
except UserRegisteredEmail:
102103
response["invalid"].append({invitee: "user registered email"})
103104
else:
104105
invite.send_invitation(request)
@@ -124,5 +125,19 @@ def delete(self, request, *args, **kwargs):
124125
except Token.DoesNotExist:
125126
return Response(status=status.HTTP_404_NOT_FOUND)
126127

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+
127142

128143
obtain_delete_auth_token = ObtainDeleteAuthToken.as_view()

0 commit comments

Comments
 (0)