当前位置:网站首页>用户-注册-登录
用户-注册-登录
2022-07-27 05:02:00 【Csdn__F】
django项目中写入方法
这是先登录,如果用户没有存在,就给他注册一个用户,注册完之后,在让用户登录,登录时判断是否有管理权限
import redis
from rest_framework.views import APIView
from rest_framework.respoonse import Response
from django.contrib.auth.hashers import check_password
from datetime import datetime
from rest_framework_jwt.utils import jwt_payload_handler, jwt_encode_handler
# 登录视图
class LoginUserAPIView(APIView):
def post(self, request):
username = request.data.get("username")
password = request.data.get("password")
mobile = request.data.get("mobile")
sms_code = request.data.get("sms_code")
r = redis.Redis(host='127.0.0.1', port=6379)
# 判断用户名是否存在
user = User.objects.filter(username=username).first()
if user:
# 判断密码
if check_password(password, user.password):
# 密码正确,判断是否以管理员身份登录
if user.is_staff and user.is_active:
token = self.gen_token(user)
# 改变登录时间
user.last_login = datetime.now()
user.save()
return Response({
'code': 200,
'msg': '登录成功',
'user': {
'id': user.id,
'name': user.username,
'token': token
}
})
else:
return Response({
'code': 206, 'msg': '没有管理员权限'})
else:
# 密码不正确,重置密码
validation = self.validate_sms_code(sms_code, mobile, r)
if validation:
# 完成短信认证, 开始重置密码
user.set_password(password)
user.save()
return Response({
'code': 207, 'msg': '密码以重置'})
else:
return Response({
'code': 204, 'msg': '重置失败'})
else:
# 注册用户
if sms_code:
# 使用验证码,并注册
validation = self.validate_sms_code(sms_code, mobile, r)
# 注册用户信息
if validation:
User.objects.create_user(username=username, password=password, mobile=mobile)
return Response({
'code': 201, 'msg': '注册成功,申请管理员权限'})
else:
return Response({
'code': 400, 'msg': '注册失败'})
else:
return Response({
'code': 400, 'msg': '请输入验证码'})
@staticmethod
def gen_token(user):
# 生成payload 载荷信息
payload = jwt_payload_handler(user)
# 生成token
# token = jwt_decode_handler(payload)
token = jwt_encode_handler(payload)
return token
@staticmethod
def validate_sms_code(sms_code, mobile, r):
stared_code = r.get(mobile)
if stared_code and stared_code.decode() == sms_code:
return True
else:
return False
边栏推荐
猜你喜欢

Message reliability processing

Day4 --- Flask 蓝图与Rest-ful

JVM Part 1: memory and garbage collection part 6 -- runtime data area local method & local method stack

pytorch 数据类型 和 numpy 数据 相互转化

mq设置过期时间、优先级、死信队列、延迟队列

用户的管理-限制

Li Hongyi machine learning team learning punch in activity day06 --- convolutional neural network

数据库连接池&&Druid使用

JVM Part 1: memory and garbage collection -- runtime data area 4 - program counter

JVM part I: memory and garbage collection part II -- class loading subsystem
随机推荐
Bean的生命周期&&依赖注入*依赖自动装配
Seckill system design
用pygame自己动手做一款游戏01
B1028 census
B1021 single digit statistics
弹球小游戏
Cenos7更新MariaDB
Enumeration class implements singleton mode
用户的管理-限制
Redis cluster
Notes Series docker installation PostgreSQL 14
Niuke sword refers to the path in the offer--jz12 matrix
2021 OWASP top 5: security configuration error
redis事务
B1023 组个最小数
2022 Zhengzhou light industry Freshmen's competition topic - I won't say if I'm killed
Find the number of combinations (the strongest optimization)
SQL数据库→约束→设计→多表查询→事务
SQL database → constraint → design → multi table query → transaction
p7 day1 初识Flask框架