AdminManage.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # -*- coding: utf-8 -*-
  2. from django.views.decorators.csrf import csrf_exempt
  3. from django.views.generic import TemplateView
  4. from django.utils.decorators import method_decorator
  5. from django.contrib.auth.hashers import make_password # 对密码加密模块
  6. from Service.TokenManager import JSONTokenManager
  7. from Model.models import Device_User, Device_Info
  8. from Service.CommonService import CommonService
  9. from Service.ModelService import ModelService
  10. import datetime
  11. from Model.models import Access_Log
  12. from Service.ResponseService import *
  13. from django.views.decorators.http import require_http_methods
  14. '''
  15. http://13.56.215.252:8222/adminManage/manage?operation=getAllDeviceArea&token=test
  16. http://13.56.215.252:8222/adminManage/manage?operation=getAllUserName&token=test
  17. http://13.56.215.252:8222/adminManage/manage?operation=getAllUID&token=test
  18. '''
  19. class AdminManage(TemplateView):
  20. @method_decorator(csrf_exempt)
  21. def dispatch(self, *args, **kwargs):
  22. return super(AdminManage, self).dispatch(*args, **kwargs)
  23. def get(self, request, *args, **kwargs):
  24. request.encoding = 'utf-8'
  25. return self.validation(request_dict=request.GET)
  26. def post(self, request, *args, **kwargs):
  27. request.encoding = 'utf-8'
  28. return self.validation(request_dict=request.POST)
  29. def validation(self, request_dict, *args, **kwargs):
  30. token = request_dict.get('token', None)
  31. if token is not None:
  32. tokenManager = JSONTokenManager()
  33. error_code = tokenManager.verify_AToken(token)
  34. if error_code == 0:
  35. userID = tokenManager.accessDict.get('userID', None)
  36. operation = request_dict.get('operation', None)
  37. param_flag = CommonService.get_param_flag(data=[userID, operation])
  38. if param_flag is True:
  39. if operation == 'resetUserPwd':
  40. return self.resetUserPwd(request_dict=request_dict, userID=userID)
  41. if operation == 'getAllUserName':
  42. return self.getAllUserName(userID=userID)
  43. if operation == 'getStatisAccess':
  44. return self.getStatisAccess(userID=userID,request_dict=request_dict)
  45. if operation == 'getAllUID':
  46. return self.getAllUID(userID=userID)
  47. if operation == 'getAllDeviceArea':
  48. return self.getAllDeviceArea(userID=userID)
  49. else:
  50. return ResponseJSON(444)
  51. else:
  52. return HttpResponse(tokenManager.errorCodeInfo(error_code))
  53. else:
  54. return ResponseJSON(311)
  55. def resetUserPwd(self, request_dict, userID):
  56. own_permission = ModelService.check_permission(userID=userID, permID=50)
  57. if own_permission is True:
  58. duserID = request_dict.get('duserID', None)
  59. userPwd = request_dict.get('userPwd', None)
  60. param_flag = CommonService.get_param_flag(data=[duserID])
  61. if param_flag is True:
  62. UserValid = Device_User.objects.filter(userID=duserID)
  63. if UserValid:
  64. if userPwd is None:
  65. userPwd = '123456'
  66. is_update = UserValid.update(password=make_password(userPwd))
  67. if is_update:
  68. return ResponseJSON(0)
  69. else:
  70. return ResponseJSON(106)
  71. else:
  72. return ResponseJSON(444)
  73. else:
  74. return ResponseJSON(404)
  75. def getAllUserName(self, userID):
  76. own_permission = ModelService.check_permission(userID=userID, permID=30)
  77. if own_permission is True:
  78. username_list = Device_User.objects.all().values_list('username', flat=True)
  79. if username_list:
  80. return ResponseJSON(0,{'username_list': list(username_list)})
  81. else:
  82. return ResponseJSON(0)
  83. else:
  84. return ResponseJSON(404)
  85. # 获取所有设备地区
  86. def getAllDeviceArea(self, userID):
  87. own_permission = ModelService.check_permission(userID=userID, permID=30)
  88. if own_permission is True:
  89. qs = Device_Info.objects.all().values('area','UID')
  90. uid_area_dict = {}
  91. for q in qs:
  92. if q['UID'] and q['area']:
  93. uid_area_dict[q['UID']]=q['area']
  94. if len(uid_area_dict):
  95. area_dict = {}
  96. for k,v in uid_area_dict.items():
  97. if v in area_dict:
  98. area_dict[v] += 1
  99. else:
  100. area_dict[v] = 1
  101. return ResponseJSON(0,{'area':area_dict})
  102. else:
  103. return ResponseJSON(0)
  104. else:
  105. return ResponseJSON(404)
  106. '''
  107. 统计一天访问量
  108. http://192.168.136.45:8077/adminManage/manage?token=test&operation=getStatisAccess&timestamp=1528773308
  109. '''
  110. def getStatisAccess(self,userID,request_dict):
  111. own_permission = ModelService.check_permission(userID=userID, permID=30)
  112. if own_permission is True:
  113. time_stamp = int(request_dict.get('timestamp', None))
  114. times = datetime.datetime.fromtimestamp(time_stamp)
  115. time_dict = CommonService.getTimeDict(times)
  116. res = {}
  117. for k, v in time_dict.items():
  118. start_date = time_dict[k]
  119. end_date = time_dict[k] + datetime.timedelta(hours=1)
  120. count = Access_Log.objects.filter(time__range=(start_date, end_date)).count()
  121. if count:
  122. res[k] = count
  123. else:
  124. res[k] = 0
  125. return ResponseJSON(0, {'count': res})
  126. else:
  127. return ResponseJSON(404)
  128. def getAllUID(self,userID):
  129. own_permission = ModelService.check_permission(userID=userID, permID=30)
  130. if own_permission is True:
  131. uid_list = Device_Info.objects.all().values_list('UID', flat=True)
  132. if uid_list:
  133. return ResponseJSON(0, {'count': len(uid_list),'uid_list':list(uid_list)})
  134. else:
  135. return ResponseJSON(404)
  136. @csrf_exempt
  137. @require_http_methods(["GET"])
  138. def getUserIds(request):
  139. token = request.GET.get('token', None)
  140. if token is not None:
  141. tokenManager = JSONTokenManager()
  142. error_code = tokenManager.verify_AToken(token)
  143. if error_code == 0:
  144. userID = tokenManager.accessDict.get('userID', None)
  145. own_perm = ModelService.check_permission(userID,30)
  146. if own_perm is True:
  147. # userID_list = Device_User.objects.all().values_list('userID', flat=True)
  148. dn = Device_User.objects.all().values('userID', 'username')
  149. return ResponseJSON(0,{"datas":list(dn)})
  150. else:
  151. return ResponseJSON(404)
  152. else:
  153. return HttpResponse(tokenManager.errorCodeInfo(error_code))
  154. else:
  155. return ResponseJSON(311)