CheckUserData.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import re
  4. import traceback
  5. from random import Random # 用于生成随机码
  6. from wsgiref.util import FileWrapper
  7. from django.contrib.auth.hashers import make_password
  8. from django.http import HttpResponse
  9. from django.utils.decorators import method_decorator
  10. from django.views.decorators.csrf import csrf_exempt
  11. from django.views.generic import TemplateView
  12. from Ansjer.config import BASE_DIR
  13. from Model.models import Device_User
  14. from Object.ResponseObject import ResponseObject
  15. # 生成随机字符串
  16. def RandomStr(randomlength=8, number=False):
  17. str = ''
  18. if number == False:
  19. characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \
  20. 'tUuVvWwXxYyZz0123456789'
  21. else:
  22. characterSet = '0123456789'
  23. length = len(characterSet) - 1
  24. random = Random()
  25. for index in range(randomlength):
  26. str += characterSet[random.randint(0, length)]
  27. return str
  28. def date_handler(obj):
  29. return obj.isoformat()
  30. class ModifyPwdView(TemplateView):
  31. @method_decorator(csrf_exempt)
  32. def dispatch(self, *args, **kwargs):
  33. return super(ModifyPwdView, self).dispatch(*args, **kwargs)
  34. def post(self, request, *args, **kwargs):
  35. request.encoding = 'utf-8'
  36. request_dict = request.POST
  37. return self.ValidationError(request_dict)
  38. def get(self, request, *args, **kwargs):
  39. request.encoding = 'utf-8'
  40. request_dict = request.GET
  41. return self.ValidationError(request_dict)
  42. def ValidationError(self, request_dict):
  43. userEmail = request_dict.get('userEmail', None)
  44. oldPwd = request_dict.get('oldPwd', None)
  45. newPwd = request_dict.get('newPwd', None)
  46. response = ResponseObject()
  47. dataValid = DataValid()
  48. if dataValid.password_validate(newPwd):
  49. if oldPwd != newPwd:
  50. return response.json(118)
  51. try:
  52. User = Device_User.objects.get(userEmail=userEmail)
  53. User.userPwd = make_password(newPwd)
  54. User.save()
  55. except Exception as e:
  56. errorInfo = traceback.format_exc()
  57. print('更新密码到数据库: %s' % errorInfo)
  58. return response.json(501, {'msg': repr(e)})
  59. else:
  60. return response.json(0)
  61. else:
  62. return response.json(109)
  63. class DataValid:
  64. def __init__(self):
  65. # 用户名正则
  66. # self.re_name = re.compile(r'^[A-Za-z0-9\u4e00-\u9fa5\.\_]{1,16}$')
  67. self.re_name = re.compile(r'^[A-Za-z0-9\u4e00-\u9fa5\.\_\-\@]{4,40}$')
  68. # 密码强度正则
  69. self.re_password = re.compile(r'^[\w\.\_\@\-]{1,16}$')
  70. # 手机号码正则
  71. self.re_mobile = re.compile(r'^\d{1,16}$')
  72. # 邮箱地址正则
  73. self.re_email = re.compile(r'^[A-Za-z0-9\u4e00-\u9fa5\.\_\-]+@[A-Za-z0-9_-]+(\.[A-Za-z0-9_-]+)+$')
  74. def name_validate(self, value):
  75. if self.re_name.match(value):
  76. return True
  77. else:
  78. return False
  79. def password_validate(self, value):
  80. if self.re_password.match(value):
  81. return True
  82. else:
  83. return False
  84. def email_validate(self, value):
  85. if self.re_email.match(value):
  86. return True
  87. else:
  88. return False
  89. def mobile_validate(self, value):
  90. if self.re_mobile.match(value):
  91. return True
  92. else:
  93. return False
  94. @csrf_exempt
  95. def download_file(request, Upgradename, *callback_args, **callback_kwargs):
  96. """
  97. 下载单个文件
  98. :param request:
  99. :param Upgradename:
  100. :param callback_args:
  101. :param callback_kwargs:
  102. :return:
  103. """
  104. print(Upgradename)
  105. print(callback_args, callback_kwargs)
  106. for value in callback_args:
  107. print("other args:", value)
  108. # 打印dict类型的不定长参数 args
  109. for key in callback_kwargs:
  110. print("dictargs:" + key + ":" + bytes(callback_kwargs[key]))
  111. print(request.body)
  112. file_name = os.path.join(BASE_DIR, "static/Upgrade/DVR/").replace('\\', '/') + Upgradename
  113. response = ResponseObject()
  114. if os.path.isfile(file_name):
  115. try:
  116. print(file_name)
  117. JSON = response.formal(0)
  118. wrapper = FileWrapper(open(file_name, 'rb'))
  119. response = HttpResponse(wrapper, content_type="application/octet-stream")
  120. response['Content-Length'] = os.path.getsize(file_name)
  121. response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(file_name)
  122. response['Content-Error'] = JSON
  123. return response
  124. except Exception as e:
  125. errorJSON = response.formal(10, 'Wrong reason:' + repr(e))
  126. response = HttpResponse(errorJSON, content_type='text/plain', charset='utf-8')
  127. response['Content-Error'] = errorJSON
  128. return response
  129. else:
  130. errorJSON = response.formal(907)
  131. response = HttpResponse(errorJSON, content_type='text/plain', charset='utf-8')
  132. response['Content-Error'] = errorJSON
  133. return response