CheckUserData.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import re
  4. from random import Random # 用于生成随机码
  5. from wsgiref.util import FileWrapper
  6. from django.http import HttpResponse
  7. from django.views.decorators.csrf import csrf_exempt
  8. from Ansjer.config import BASE_DIR
  9. from Object.ResponseObject import ResponseObject
  10. # 生成随机字符串
  11. def RandomStr(randomlength=8, number=False):
  12. str = ''
  13. if number == False:
  14. characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \
  15. 'tUuVvWwXxYyZz0123456789'
  16. else:
  17. characterSet = '0123456789'
  18. length = len(characterSet) - 1
  19. random = Random()
  20. for index in range(randomlength):
  21. str += characterSet[random.randint(0, length)]
  22. return str
  23. def date_handler(obj):
  24. return obj.isoformat()
  25. class DataValid:
  26. def __init__(self):
  27. # 用户名正则
  28. # self.re_name = re.compile(r'^[A-Za-z0-9\u4e00-\u9fa5\.\_]{1,16}$')
  29. self.re_name = re.compile(r'^[A-Za-z0-9\u4e00-\u9fa5\.\_\-\@]{4,40}$')
  30. # 密码强度正则
  31. self.re_password = re.compile(r'^[\w\.\_\@\-]{1,16}$')
  32. # 手机号码正则
  33. self.re_mobile = re.compile(r'^\d{1,16}$')
  34. # 邮箱地址正则
  35. self.re_email = re.compile(r'^[A-Za-z0-9\u4e00-\u9fa5\.\_\-]+@[A-Za-z0-9_-]+(\.[A-Za-z0-9_-]+)+$')
  36. def name_validate(self, value):
  37. if self.re_name.match(value):
  38. return True
  39. else:
  40. return False
  41. def password_validate(self, value):
  42. if self.re_password.match(value):
  43. return True
  44. else:
  45. return False
  46. def email_validate(self, value):
  47. if self.re_email.match(value):
  48. return True
  49. else:
  50. return False
  51. def mobile_validate(self, value):
  52. if self.re_mobile.match(value):
  53. return True
  54. else:
  55. return False
  56. @csrf_exempt
  57. def download_file(request, Upgradename, *callback_args, **callback_kwargs):
  58. """
  59. 下载单个文件
  60. :param request:
  61. :param Upgradename:
  62. :param callback_args:
  63. :param callback_kwargs:
  64. :return:
  65. """
  66. print(Upgradename)
  67. print(callback_args, callback_kwargs)
  68. for value in callback_args:
  69. print("other args:", value)
  70. # 打印dict类型的不定长参数 args
  71. for key in callback_kwargs:
  72. print("dictargs:" + key + ":" + bytes(callback_kwargs[key]))
  73. print(request.body)
  74. file_name = os.path.join(BASE_DIR, "static/Upgrade/DVR/").replace('\\', '/') + Upgradename
  75. response = ResponseObject()
  76. if os.path.isfile(file_name):
  77. try:
  78. print(file_name)
  79. JSON = response.formal(0)
  80. wrapper = FileWrapper(open(file_name, 'rb'))
  81. response = HttpResponse(wrapper, content_type="application/octet-stream")
  82. response['Content-Length'] = os.path.getsize(file_name)
  83. response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(file_name)
  84. response['Content-Error'] = JSON
  85. return response
  86. except Exception as e:
  87. errorJSON = response.formal(10, 'Wrong reason:' + repr(e))
  88. response = HttpResponse(errorJSON, content_type='text/plain', charset='utf-8')
  89. response['Content-Error'] = errorJSON
  90. return response
  91. else:
  92. errorJSON = response.formal(907)
  93. response = HttpResponse(errorJSON, content_type='text/plain', charset='utf-8')
  94. response['Content-Error'] = errorJSON
  95. return response