fix: 访问 tokens 接口更新用户最后登录时间

pull/5912/head
xinwen 2021-04-07 10:29:41 +08:00 committed by 老广
parent 3ec78ff9be
commit f34fb5d9d5
1 changed files with 7 additions and 0 deletions

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
#
from django.utils import timezone
from rest_framework import serializers
from common.utils import get_object_or_none
@ -44,6 +45,10 @@ class BearerTokenSerializer(serializers.Serializer):
def get_keyword(obj):
return 'Bearer'
def update_last_login(self, user):
user.last_login = timezone.now()
user.save(update_fields=['last_login'])
def create(self, validated_data):
request = self.context.get('request')
if request.user and not request.user.is_anonymous:
@ -56,6 +61,8 @@ class BearerTokenSerializer(serializers.Serializer):
"user id {} not exist".format(user_id)
)
token, date_expired = user.create_bearer_token(request)
self.update_last_login(user)
instance = {
"token": token,
"date_expired": date_expired,