Skip to content

Commit 2cbeb40

Browse files
authored
Merge pull request #23 from Lab-Lab-Lab/main
track users' last_login
2 parents c4dbe96 + 245301f commit 2cbeb40

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ typings/
151151

152152

153153
### VisualStudioCode template
154-
.vscode/
155154
.vscode/*
156155
!.vscode/settings.json
157156
!.vscode/tasks.json

.vscode/launch.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python Debugger: Django",
9+
"type": "debugpy",
10+
"request": "launch",
11+
"args": [
12+
"runserver"
13+
],
14+
"django": true,
15+
"autoStartBrowser": false,
16+
"program": "${workspaceFolder}/manage.py"
17+
}
18+
]
19+
}

.vscode/settings.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"[python]": {
3+
"editor.defaultFormatter": "ms-python.black-formatter",
4+
"editor.formatOnSave": true
5+
}
6+
}

teleband/users/api/views.py

+15
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
@@ -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)