CheckUserData.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 DataValid:
  31. def __init__(self):
  32. # 用户名正则
  33. # self.re_name = re.compile(r'^[A-Za-z0-9\u4e00-\u9fa5\.\_]{1,16}$')
  34. self.re_name = re.compile(r'^[A-Za-z0-9\u4e00-\u9fa5\.\_\-\@]{4,40}$')
  35. # 密码强度正则
  36. self.re_password = re.compile(r'^[\w\.\_\@\-]{1,16}$')
  37. # 手机号码正则
  38. self.re_mobile = re.compile(r'^\d{1,16}$')
  39. # 邮箱地址正则
  40. self.re_email = re.compile(r'^[A-Za-z0-9\u4e00-\u9fa5\.\_\-]+@[A-Za-z0-9_-]+(\.[A-Za-z0-9_-]+)+$')
  41. def name_validate(self, value):
  42. if self.re_name.match(value):
  43. return True
  44. else:
  45. return False
  46. def password_validate(self, value):
  47. if self.re_password.match(value):
  48. return True
  49. else:
  50. return False
  51. def email_validate(self, value):
  52. if self.re_email.match(value):
  53. return True
  54. else:
  55. return False
  56. def mobile_validate(self, value):
  57. if self.re_mobile.match(value):
  58. return True
  59. else:
  60. return False
  61. @csrf_exempt
  62. def download_file(request, Upgradename, *callback_args, **callback_kwargs):
  63. """
  64. 下载单个文件
  65. :param request:
  66. :param Upgradename:
  67. :param callback_args:
  68. :param callback_kwargs:
  69. :return:
  70. """
  71. print(Upgradename)
  72. print(callback_args, callback_kwargs)
  73. for value in callback_args:
  74. print("other args:", value)
  75. # 打印dict类型的不定长参数 args
  76. for key in callback_kwargs:
  77. print("dictargs:" + key + ":" + bytes(callback_kwargs[key]))
  78. print(request.body)
  79. file_name = os.path.join(BASE_DIR, "static/Upgrade/DVR/").replace('\\', '/') + Upgradename
  80. response = ResponseObject()
  81. if os.path.isfile(file_name):
  82. try:
  83. print(file_name)
  84. JSON = response.formal(0)
  85. wrapper = FileWrapper(open(file_name, 'rb'))
  86. response = HttpResponse(wrapper, content_type="application/octet-stream")
  87. response['Content-Length'] = os.path.getsize(file_name)
  88. response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(file_name)
  89. response['Content-Error'] = JSON
  90. return response
  91. except Exception as e:
  92. errorJSON = response.formal(10, 'Wrong reason:' + repr(e))
  93. response = HttpResponse(errorJSON, content_type='text/plain', charset='utf-8')
  94. response['Content-Error'] = errorJSON
  95. return response
  96. else:
  97. errorJSON = response.formal(907)
  98. response = HttpResponse(errorJSON, content_type='text/plain', charset='utf-8')
  99. response['Content-Error'] = errorJSON
  100. return response