forked from 520118202/ExamOnline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
103 lines (87 loc) · 3.08 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User
from django.db.models import Q
from rest_framework import viewsets, mixins, status
from rest_framework.response import Response
from rest_framework.views import APIView
from user.models import Student, Clazz
from user.serializers import StudentSerializer, UserDetailSerializer, ClazzSerializer
# Create your views here.
class CustomBackend(ModelBackend):
"""
自定义用户验证
"""
def authenticate(self, username=None, password=None, **kwargs):
try:
user = User.objects.get(Q(username=username) | Q(mobile=username))
if user.check_password(password):
return user
except Exception as e:
return None
def jwt_response_payload_handler(token, user=None, request=None):
"""
设置jwt登录之后返回token和user信息
"""
student = Student.objects.get(user=user)
return {
'token': token,
'user': UserDetailSerializer(user, context={'request': request}).data,
'student': StudentSerializer(student, context={'request': request}).data
}
class RegisterViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet):
"""
用户注册
"""
queryset = User.objects.all()
serializer_class = UserDetailSerializer
def create(self, request, *args, **kwargs):
user = User.objects.filter(username=request.data['username'])
if user:
return Response({'msg': '用户名已存在'}, status=status.HTTP_201_CREATED)
user_detail = UserDetailSerializer(data=request.data)
if user_detail.is_valid():
user_detail.save()
user = User.objects.get(username=request.data['username'])
# 密码转成密文存储
user.password = make_password(user.password)
user.save()
student = Student(user=user, name=request.data['name'])
if student:
student.save()
return Response(user_detail.errors)
class UpdatePwdApi(APIView):
"""
修改用户密码
"""
def patch(self, request):
# 获取参数
old_pwd = request.data['oldpwd']
new_pwd = request.data['newpwd']
user_id = request.data['userid']
# 获得请求用户
user = User.objects.get(id=user_id)
# 检查原始密码是否正确
if user.check_password(old_pwd):
user.set_password(new_pwd)
user.save()
else:
return Response(data={'msg': 'fail'}, status=status.HTTP_200_OK)
# 返回数据
return Response(data={'msg': 'success'}, status=status.HTTP_200_OK)
class StudentViewSet(viewsets.ModelViewSet):
"""
学生信息
"""
# 查询集
queryset = Student.objects.all().order_by('id')
# 序列化
serializer_class = StudentSerializer
class ClazzListViewSet(viewsets.ModelViewSet):
"""
班级信息
"""
# 查询集
queryset = Clazz.objects.all().order_by('id')
# 序列化
serializer_class = ClazzSerializer