from common.mixins import GenericView from rest_framework import status from rest_framework_jwt.views import ObtainJSONWebToken from common import response from common.redis_cache import redis_handler as rh from .consts import LOGIN_TIMES_LIMIT_EXPIRES, LOGIN_TIMES_LIMIT # Create your views here. class LoginView(ObtainJSONWebToken, GenericView): def post(self, request, *args, **kwargs): # 登录次数限制 user_name = request.data.get('username', '') # times = rh.get_login_times(user_name) # if isinstance(times, int) and times >= LOGIN_TIMES_LIMIT: # raise self.invalid_params(msg="重试次数限制") res = super(LoginView, self).post(request, *args, **kwargs) self.running_log.info('[users.login] username: {0}'.format(user_name)) if res.status_code == status.HTTP_400_BAD_REQUEST: # rh.set_login_times(user_name, LOGIN_TIMES_LIMIT_EXPIRES) raise self.invalid_params(msg="用户名或密码错误") serializer = self.get_serializer(data=request.data) serializer.is_valid() user = serializer.object.get('user') data = { 'user_id': user.id, 'user_name': user.username, 'token': res.data.get('token'), } return response.ok(data=data)