views.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # # Create your views here.
  2. # import json
  3. # import base64
  4. #
  5. # import re
  6. # from django import http
  7. # from django.shortcuts import render,redirect
  8. #
  9. # # Create your views here.
  10. # from django.views import View
  11. # from django.conf import settings
  12. # from django.db import connection, transaction
  13. # import datetime
  14. # from django.contrib.auth import authenticate, login,logout
  15. #
  16. # from utils.sqlinjection import sqlinjections
  17. # from utils.cust_data import customer_data
  18. # from utils.et_CACC_NO import et_CACC_NO
  19. # from utils.examine_ood import examine_ood
  20. # from utils.if_account import if_account
  21. #
  22. # from utils.usr_data import usr_data
  23. # from utils.monthly_odd import monthly_odd
  24. # from utils.executeQuery import executeQuery
  25. #
  26. # # 用户注册
  27. # from users.models import Customer
  28. #
  29. # # 用户注册
  30. # class RegisterView(View):
  31. # def get(self,request):
  32. # return render(request,'register.html')
  33. # def post(self,request):
  34. # #1,获取参数
  35. # user_name = request.POST.get("user_name")
  36. # pwd = request.POST.get("pwd")
  37. # #2,校验参数
  38. # #2,1 为空校验
  39. # if not all([user_name,pwd]):
  40. # return http.HttpResponseForbidden("参数不全")
  41. #
  42. # #3,创建用户对象,保存到数据库中
  43. # user = Customer.objects.create(username=user_name,password=pwd)
  44. #
  45. # #4,返回响应
  46. # response = redirect("http://www.taobao.com")
  47. # return response
  48. # # 用户登录处理 初始化密码后台设置为:MTEx 表示密码是:111
  49. # class Login(View):
  50. # def get(self, request):
  51. # return render(request, 'login.html')
  52. # def post(self, request):
  53. # sqlinjection = sqlinjections().ifsql(request.POST)
  54. # if sqlinjection:
  55. # return http.HttpResponseForbidden("参数非法")
  56. # user_1 = request.POST.get("user_name").split('->')[0]
  57. # # print(request.POST)
  58. # pswd = request.POST.get("pwd").lower()
  59. # # 校验参数
  60. # if not all([user_1,pswd]):
  61. # return http.HttpResponseForbidden("请输入账号密码")
  62. # # 判断用户密码正确性
  63. # try:
  64. # user = Customer.objects.get(user_no=user_1)
  65. # except Exception:
  66. # return http.HttpResponseForbidden("存在重复的账号,请联系管理员")
  67. # # 对数据库密码进行解码
  68. # try:
  69. # sql_password = bytes(base64.b64decode(user.password.encode(encoding='utf-8'))).decode()
  70. # except Exception:
  71. # return http.HttpResponseForbidden("密码被后台操作过请联系管理员")
  72. # print(sql_password)
  73. # # 校验密码是否正确
  74. # if user.user_no !=user_1 or sql_password!=pswd:
  75. # return http.HttpResponseForbidden("账号或者密码错误")
  76. # # 3,状态保持
  77. # # login(request, user)
  78. # # 3,1设置状态保持的时间
  79. # # if request.session.get('username'):
  80. # request.session['username'] = user_1
  81. # request.session.set_expiry(3600 * 24 * 2) # 两天有效
  82. # response = redirect('/index')
  83. # response.set_cookie("username", user.username, 3600 * 24 * 2)
  84. # return response
  85. # def put(self,request):
  86. # req_data = json.loads(request.body.decode())["params"]
  87. # user_2 = req_data.get("user_2").split('->')[0]
  88. # print(user_2)
  89. # pswd_old = req_data.get("pswd_old").lower()
  90. # pswd_new = req_data.get("pswd_new").lower()
  91. # print(req_data)
  92. # try:
  93. # user = Customer.objects.get(user_no=user_2)
  94. # except Exception:
  95. # return http.HttpResponseForbidden("存在重复的账号,请联系管理员")
  96. # # 对数据库密码进行解码
  97. # try:
  98. # sql_password = bytes(base64.b64decode(user.password.encode(encoding='utf-8'))).decode()
  99. # except Exception:
  100. # return http.HttpResponseForbidden("密码被后台操作过请联系管理员")
  101. # # 判断输入的密码是否正确
  102. # if user.user_no !=user_2 or sql_password!=pswd_old:
  103. # return http.HttpResponseForbidden("旧密码与账号不匹配")
  104. # pswd_new = bytes(base64.b64encode(pswd_new.encode())).decode()
  105. # user.password = pswd_new
  106. # user.save()
  107. # context = {
  108. # "message":"修改成功",
  109. # "code":200
  110. # }
  111. # return http.JsonResponse(context)
  112. # def delete(self, request):
  113. # del request.session['username']
  114. # response = redirect('/login/')
  115. # response.delete_cookie("username")
  116. # return response
  117. import json
  118. import re
  119. from django import http
  120. from django.views import View
  121. from rest_framework import status
  122. from rest_framework.response import Response
  123. from rest_framework.views import APIView
  124. from rest_framework_jwt.views import obtain_jwt_token, RefreshJSONWebToken
  125. from rest_framework_jwt.settings import api_settings
  126. from rest_framework_jwt.views import ObtainJSONWebToken
  127. from .models import User
  128. jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
  129. jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
  130. jwt_decode_handler = api_settings.JWT_DECODE_HANDLER
  131. from rest_framework_jwt.views import ObtainJSONWebToken
  132. from rest_framework.authentication import SessionAuthentication,BasicAuthentication
  133. from rest_framework.permissions import IsAuthenticated,AllowAny
  134. from rest_framework_jwt.authentication import JSONWebTokenAuthentication
  135. from utils.executeQuery import executeQuery,IseUpDelQuery
  136. from utils.ClssSql import ClassSqls
  137. #用户登录
  138. class LoginView(ObtainJSONWebToken):
  139. def post(self, request, *args, **kwargs):
  140. usr = request.data.get('usr')
  141. pwd = request.data.get('pwd')
  142. user_query = User.objects.filter(username=usr)
  143. user_obj = user_query.first()
  144. if user_obj and user_obj.check_password(pwd):
  145. payload = jwt_payload_handler(user_obj)
  146. token = jwt_encode_handler(payload)
  147. data = {"data":[{"token":token,"username":user_obj.username,"is_superuser":user_obj.is_superuser}]}
  148. return Response(data,status=status.HTTP_200_OK)
  149. # return APIResponse(results={'username': user_obj.username}, token=token)
  150. return Response({"msg":"账号或密码错误"},status=status.HTTP_304_NOT_MODIFIED)
  151. # 查看用户用户信息操作
  152. class LoginInformation(APIView):
  153. # 1,设置局部认证
  154. authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication,)
  155. # authentication_classes = [SessionAuthentication, BasicAuthentication]
  156. # 2,设置局部权限
  157. permission_classes = (IsAuthenticated,)
  158. # permission_classes = (AllowAny,) #任何用户都能访问
  159. def get(self, request):
  160. sql = ClassSqls.CommonUsers
  161. result = executeQuery(sql)
  162. data = {
  163. "result": result,
  164. "errmsg": "获取用户信息成功",
  165. }
  166. return Response(data,status=status.HTTP_200_OK)
  167. def post(self,request):
  168. sql = ClassSqls.InsetUser
  169. IseUpDelQuery(sql)
  170. data = {
  171. "msg": "批量导入成功",
  172. }
  173. return http.JsonResponse(data, status=status.HTTP_200_OK)
  174. def put(self,request):
  175. loginusername = request.user.username
  176. is_superuser = request.user.is_superuser
  177. req_data = request.data
  178. username =req_data["username"]
  179. password =req_data["password"]
  180. print(req_data)
  181. if is_superuser:
  182. user = User.objects.get(username=username)
  183. user.set_password(password)
  184. user.save()
  185. else:
  186. user = User.objects.get(username=loginusername)
  187. user.set_password(password)
  188. user.save()
  189. # a = User.objects.filter(username=username)
  190. # request.user.set_password(pwd)
  191. # try:
  192. # User.objects.filter(username=username).update(pssword=pwd)
  193. # except Exception:
  194. # return http.HttpResponseForbidden("密码修改失败")
  195. data = {
  196. "msg": "密码修改成功",
  197. }
  198. return http.JsonResponse(data,status=status.HTTP_200_OK)