123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- # -*- coding: utf-8 -*-
- from django.views.decorators.csrf import csrf_exempt
- from django.views.generic import TemplateView
- from django.utils.decorators import method_decorator
- from django.contrib.auth.hashers import make_password # 对密码加密模块
- from Service.TokenManager import JSONTokenManager
- from Model.models import Device_User, Device_Info
- from Service.CommonService import CommonService
- from Service.ModelService import ModelService
- import datetime
- from Model.models import Access_Log
- from Service.ResponseService import *
- from django.views.decorators.http import require_http_methods
- '''
- http://13.56.215.252:8222/adminManage/manage?operation=getAllDeviceArea&token=test
- http://13.56.215.252:8222/adminManage/manage?operation=getAllUserName&token=test
- http://13.56.215.252:8222/adminManage/manage?operation=getAllUID&token=test
- '''
- class AdminManage(TemplateView):
- @method_decorator(csrf_exempt)
- def dispatch(self, *args, **kwargs):
- return super(AdminManage, self).dispatch(*args, **kwargs)
- def get(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- return self.validation(request_dict=request.GET)
- def post(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- return self.validation(request_dict=request.POST)
- def validation(self, request_dict, *args, **kwargs):
- token = request_dict.get('token', None)
- if token is not None:
- tokenManager = JSONTokenManager()
- error_code = tokenManager.verify_AToken(token)
- if error_code == 0:
- userID = tokenManager.accessDict.get('userID', None)
- operation = request_dict.get('operation', None)
- param_flag = CommonService.get_param_flag(data=[userID, operation])
- if param_flag is True:
- if operation == 'resetUserPwd':
- return self.resetUserPwd(request_dict=request_dict, userID=userID)
- if operation == 'getAllUserName':
- return self.getAllUserName(userID=userID)
- if operation == 'getStatisAccess':
- return self.getStatisAccess(userID=userID,request_dict=request_dict)
- if operation == 'getAllUID':
- return self.getAllUID(userID=userID)
- if operation == 'getAllDeviceArea':
- return self.getAllDeviceArea(userID=userID)
- else:
- return ResponseJSON(444)
- else:
- return HttpResponse(tokenManager.errorCodeInfo(error_code))
- else:
- return ResponseJSON(311)
- def resetUserPwd(self, request_dict, userID):
- own_permission = ModelService.check_permission(userID=userID, permID=50)
- if own_permission is True:
- duserID = request_dict.get('duserID', None)
- userPwd = request_dict.get('userPwd', None)
- param_flag = CommonService.get_param_flag(data=[duserID])
- if param_flag is True:
- UserValid = Device_User.objects.filter(userID=duserID)
- if UserValid:
- if userPwd is None:
- userPwd = '123456'
- is_update = UserValid.update(password=make_password(userPwd))
- if is_update:
- return ResponseJSON(0)
- else:
- return ResponseJSON(106)
- else:
- return ResponseJSON(444)
- else:
- return ResponseJSON(404)
- def getAllUserName(self, userID):
- own_permission = ModelService.check_permission(userID=userID, permID=30)
- if own_permission is True:
- username_list = Device_User.objects.all().values_list('username', flat=True)
- if username_list:
- return ResponseJSON(0,{'username_list': list(username_list)})
- else:
- return ResponseJSON(0)
- else:
- return ResponseJSON(404)
- # 获取所有设备地区
- def getAllDeviceArea(self, userID):
- own_permission = ModelService.check_permission(userID=userID, permID=30)
- if own_permission is True:
- qs = Device_Info.objects.all().values('area','UID')
- uid_area_dict = {}
- for q in qs:
- if q['UID'] and q['area']:
- uid_area_dict[q['UID']]=q['area']
- if len(uid_area_dict):
- area_dict = {}
- for k,v in uid_area_dict.items():
- if v in area_dict:
- area_dict[v] += 1
- else:
- area_dict[v] = 1
- return ResponseJSON(0,{'area':area_dict})
- else:
- return ResponseJSON(0)
- else:
- return ResponseJSON(404)
- '''
- 统计一天访问量
- http://192.168.136.45:8077/adminManage/manage?token=test&operation=getStatisAccess×tamp=1528773308
- '''
- def getStatisAccess(self,userID,request_dict):
- own_permission = ModelService.check_permission(userID=userID, permID=30)
- if own_permission is True:
- time_stamp = int(request_dict.get('timestamp', None))
- times = datetime.datetime.fromtimestamp(time_stamp)
- time_dict = CommonService.getTimeDict(times)
- res = {}
- for k, v in time_dict.items():
- start_date = time_dict[k]
- end_date = time_dict[k] + datetime.timedelta(hours=1)
- count = Access_Log.objects.filter(time__range=(start_date, end_date)).count()
- if count:
- res[k] = count
- else:
- res[k] = 0
- return ResponseJSON(0, {'count': res})
- else:
- return ResponseJSON(404)
- def getAllUID(self,userID):
- own_permission = ModelService.check_permission(userID=userID, permID=30)
- if own_permission is True:
- uid_list = Device_Info.objects.all().values_list('UID', flat=True)
- if uid_list:
- return ResponseJSON(0, {'count': len(uid_list),'uid_list':list(uid_list)})
- else:
- return ResponseJSON(404)
- @csrf_exempt
- @require_http_methods(["GET"])
- def getUserIds(request):
- token = request.GET.get('token', None)
- if token is not None:
- tokenManager = JSONTokenManager()
- error_code = tokenManager.verify_AToken(token)
- if error_code == 0:
- userID = tokenManager.accessDict.get('userID', None)
- own_perm = ModelService.check_permission(userID,30)
- if own_perm is True:
- # userID_list = Device_User.objects.all().values_list('userID', flat=True)
- dn = Device_User.objects.all().values('userID', 'username')
- return ResponseJSON(0,{"datas":list(dn)})
- else:
- return ResponseJSON(404)
- else:
- return HttpResponse(tokenManager.errorCodeInfo(error_code))
- else:
- return ResponseJSON(311)
|