# -*- coding: utf-8 -*- import time from django.db.models import Count,Q 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 Model.models import Device_Info, Role, UserExModel, User_Brand, UidSetModel, AppFrequencyYearStatisticsModel, \ AppFrequencyStatisticsModel from Service.ModelService import ModelService from django.utils import timezone from Model.models import Access_Log, Device_User from django.views.decorators.http import require_http_methods from Object.ResponseObject import ResponseObject from Object.TokenObject import TokenObject from Ansjer.config import OFF_LINE_TIME_DELTA, DEVICE_TYPE import datetime, simplejson as json from Service.CommonService import CommonService from Object.RedisObject import RedisObject ''' http://192.168.136.40:8077/adminManage/manage?operation=getAllDeviceArea&token=debug http://192.168.136.40:8077/adminManage/manage?operation=getAllUserName&token=debug http://192.168.136.40:8077/adminManage/manage?operation=getAllUID&token=debug http://127.0.0.1:8000/adminManage/manage?operation=getAllOnLine&token=stest http://127.0.0.1:8000/adminManage/manage?operation=getOnLine&token=stest ''' 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): response = ResponseObject() token = request_dict.get('token', None) tko = TokenObject(token) response.lang = tko.lang if tko.code != 0: return response.json(tko.code) userID = tko.userID operation = request_dict.get('operation', None) if userID is None or operation is None: return response.json(444, 'operation') if operation == 'resetUserPwd': return self.resetUserPwd(request_dict, userID, response) if operation == 'getAllOnLine': return self.getAllOnLine(userID, response) if operation == 'getOnLine': return self.getRedisOnline(userID, response) # return self.getOnLine(userID, response) if operation == 'getAllUserName': return self.getAllUserName(userID, response) if operation == 'getStatisAccess': return self.getStatisAccess(userID, request_dict, response) if operation == 'getAllUID': return self.getAllUID(userID, response) if operation == 'getAllDeviceArea': return self.getAllDeviceArea(userID, response) if operation == 'getUserBrand': return self.getUserBrand(userID, response) if operation == 'getAreaDeviceType': return self.getAreaDeviceType(userID, response) if operation == 'getDeviceType': return self.getDeviceType(userID, response) if operation == 'getAppFrequency': return self.getAppFrequency(userID, request_dict, response) if operation == 'getHistoryAppFrequency': return self.getAllAppFrequency(userID, response) def resetUserPwd(self, request_dict, userID, response): own_permission = ModelService.check_perm(userID=userID, permID=50) if not own_permission: return response.json(404) duserID = request_dict.get('duserID', None) userPwd = request_dict.get('userPwd', None) if not duserID: return response.json(444, 'duserID') 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 response.json(0) else: return response.json(177) else: return response.json(104) def getAllUserName(self, userID, response): # 权限固定为30 own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) username_list = Device_User.objects.all().values_list('username', flat=True) if username_list: return response.json(0, {'username_list': list(username_list)}) else: return response.json(0) # 获取全部用户的在线个数 def getAllOnLine(self, userID, response): # 权限固定为30 own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) allonline = Device_User.objects.all().values('online') # 两个变量,分别是在线,离线 onlinenum = 0 noonlinenum = 0 for q in allonline: if q['online'] == True: onlinenum += 1 else: noonlinenum += 1 print("在线人数:") print(onlinenum) return response.json(0, {"onlinenum": onlinenum, "no_onlinenum": noonlinenum}) # 获取全部用户的在线人数 def getOnLine(self, userID, response): # 权限固定为30 own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is True: online_list = Device_User.objects.all().values('online', 'last_login') # 两个变量,分别是在线,离线 onlinenum = 0 noonlinenum = 0 for q in online_list: # print(q['last_login'] ) # 最后访问时间加5分种 dl_time = q['last_login'] + datetime.timedelta(minutes=OFF_LINE_TIME_DELTA) # print(dl_time) # 当前时间 now_time = timezone.localtime(timezone.now()) # print(now_time) # 如果当前时间大于最后访问的时间 if now_time < dl_time: onlinenum += 1 else: noonlinenum += 1 print("在线人") print(onlinenum) return response.json(0, {"onlinenum": onlinenum, "no_onlinenum": noonlinenum}) else: return response.json(404) # 获取全部用户的在线人数 def getRedisOnline(self, userID, response): # 权限固定为30 own_perm = ModelService.check_perm(userID, 30) if own_perm: count = int(Device_User.objects.count()) redisObj = RedisObject(db=3) onlines = int(redisObj.get_size()) print(onlines) return response.json(0, {"onlinenum": onlines, "no_onlinenum": count - onlines}) else: return response.json(404) # 获取所有设备地区 def getAllDeviceArea(self, userID, response): own_permission = ModelService.check_perm(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 response.json(0, {'area': area_dict}) else: return response.json(0) else: return response.json(404) ''' 统计一天访问量 http://192.168.136.45:8077/adminManage/manage?token=test&operation=getStatisAccess×tamp=1528773308 ''' def getStatisAccess(self, userID, request_dict, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) 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 response.json(0, {'count': res}) def getAllUID(self, userID, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) uid_list = Device_Info.objects.all().values_list('UID', flat=True) res = {} if uid_list: res = {'count': uid_list.count(), 'uid_list': list(uid_list)} return response.json(0, res) def getUserBrand(self, userID, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) # 手机型号统计 print('手机型号统计:') ub_qs = User_Brand.objects.values('deviceSupplier', 'deviceModel').annotate(value=Count('id')).order_by() res = { 'value': 0, 'data': [] } data = res['data'] tmpDict = {} for ub in ub_qs: deviceSupplier = ub['deviceSupplier'] if not tmpDict.__contains__(deviceSupplier): tmpDict[deviceSupplier] = { 'name': deviceSupplier, 'value': 0, 'children': [] } item = tmpDict[deviceSupplier] item['value'] = item['value'] + ub['value'] res['value'] = res['value'] + item['value'] model = { 'name': ub['deviceModel'], 'value': ub['value'] } item['children'].append(model) for k, v in tmpDict.items(): data.append(v) print(res) return response.json(0, res) def getAreaDeviceType(self, userID, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) print('区域设备类型统计:') di_qs = Device_Info.objects.values('area', 'Type').annotate(value=Count('UID', distinct=True)).order_by() res = { 'quantity': 0, 'data': [] } data = res['data'] tmpDict = {} tmpDict['null'] = { 'area': '未知', 'quantity': 0, 'models': [] } for di in di_qs: area = di['area'] if area is None or area == '': area = 'null' if not tmpDict.__contains__(area): tmpDict[area] = { 'area': area, 'quantity': 0, 'models': [] } item = tmpDict[area] item['quantity'] = item['quantity'] + di['value'] res['quantity'] = res['quantity'] + item['quantity'] device_model = None if DEVICE_TYPE.__contains__(di['Type']): device_model = DEVICE_TYPE[di['Type']] else: device_model = DEVICE_TYPE[0] model = { 'model': device_model, 'quantity': di['value'] } item['models'].append(model) for k, v in tmpDict.items(): data.append(v) return response.json(0, res) def getDeviceType(self, userID, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) # 设备类型统计 di_qs = Device_Info.objects.values('Type').annotate(quantity=Count('UID', distinct=True)).order_by() # 设备型号统计 us_qs = UidSetModel.objects.values('deviceModel').annotate(quantity=Count('id')).order_by() res = { 'type_data': { 'quantity': 0, 'data': [] }, 'model_data': { 'quantity': 0, 'data': [] } } type_data = res['type_data'] data = type_data['data'] quantity = 0 for di in di_qs: di['Type'] = DEVICE_TYPE[di['Type']] quantity += di['quantity'] data.append(di) type_data['quantity'] = quantity model_data = res['model_data'] data = model_data['data'] quantity = 0 for us in us_qs: data.append(us) quantity += us['quantity'] model_data['quantity'] = quantity return response.json(0, res) def getAppFrequency(self, userID, request_dict, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) # 当前的年份 current_time = int(time.time()) localtime = time.localtime(current_time) current_year = localtime.tm_year current_month = localtime.tm_mon start_year = end_year = current_year end_month = current_month start_month = 1 result = [] if end_month != 12: start_month = end_month + 1 start_year = current_year - 1 time_struct = [start_year, start_month, 0, 0, 0, 0, 0, 0, 0] key_formal = '{year}{month}' for i in range(12): result.append({ 'date_time': key_formal.format(year=time_struct[0], month=str(time_struct[1]).zfill(2)), 'data': None }) month = time_struct[1] + 1 if month > 12: time_struct[0] = time_struct[0] + 1 time_struct[1] = 1 else: time_struct[1] = month # afs_qs = {} if start_year == end_year: afs_qs = list(AppFrequencyStatisticsModel.objects.filter(year=start_year, month__gte=start_month, month__lte=end_month).values().order_by('-year', 'month')) else: afs_qs = list(AppFrequencyStatisticsModel.objects.filter(year=start_year, month__gte=start_month).values().order_by('-year', 'month')) tmps_qs = list(AppFrequencyStatisticsModel.objects.filter(year=end_year, month__lte=end_month).values().order_by('-year', 'month')) for tmp in tmps_qs: afs_qs.append(tmp) tmp_dict = {} for afs in afs_qs: key = key_formal.format(year=afs['year'], month=str(afs['month']).zfill(2)) tmp_dict[key] = json.loads(afs['data']) for res in result: if tmp_dict.__contains__(res['date_time']): res['data'] = tmp_dict[res['date_time']] print(result) return response.json(0, result) def getAllAppFrequency(self, userID, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) # 取出请求年份的统计好的数据 print('start') time_struct = time.localtime() current_year = time_struct.tm_year start_year = current_year - 5 + 1 afs_qs = AppFrequencyYearStatisticsModel.objects.filter(year__gte=start_year, year__lt=current_year).order_by( '-year') if afs_qs.exists(): afs_qs = afs_qs.values('id', 'data', 'num', 'year') res = [] for afs in afs_qs: res.append({ 'year': afs['year'], 'data': json.loads(afs['data']) }) return response.json(0, res) else: return response.json(0, []) @require_http_methods(["GET"]) def getUserIds(request): token = request.GET.get('token', None) response = ResponseObject() tko = TokenObject(token) response.lang = tko.lang if tko.code != 0: return response.json(tko.code) userID = tko.userID own_perm = ModelService.check_perm(userID, 30) if own_perm is not True: return response.json(404) dn = Device_User.objects.all().values('userID', 'username') return response.json(0, {"datas": list(dn)}) @csrf_exempt def search_user_by_content(request): if request.method == 'GET': request_dict = request.GET if request.method == 'POST': request_dict = request.POST token = request_dict.get('token', None) page = request_dict.get('page', None) line = request_dict.get('line', None) content = request_dict.get('content', None) rstime = request_dict.get('rstime', None) retime = request_dict.get('retime', None) response = ResponseObject() if page is not None and line is not None: page = int(page) line = int(line) else: return response.json(10, 'page,line is none') tko = TokenObject(token) response.lang = tko.lang if tko.code != 0: return response.json(tko.code) userID = tko.userID own_perm = ModelService.check_perm(userID=userID, permID=20) if own_perm is not True: return response.json(404) try: content = json.loads(content) search_kwargs = CommonService.get_kwargs(data=content) queryset = Device_User.objects.filter(**search_kwargs) except Exception as e: return response.json(444, repr(e)) if rstime is not None and rstime != '' and retime is not None and retime != '': startt = datetime.datetime.fromtimestamp(int(rstime)) rstime = startt.strftime("%Y-%m-%d %H:%M:%S.%f") endt = datetime.datetime.fromtimestamp(int(retime)) retime = endt.strftime("%Y-%m-%d %H:%M:%S.%f") queryset = queryset.filter(data_joined__range=(rstime, retime)) elif rstime is not None and rstime != '': startt = datetime.datetime.fromtimestamp(int(rstime)) rstime = startt.strftime("%Y-%m-%d %H:%M:%S.%f") queryset = queryset.filter(data_joined__gte=rstime) elif retime is not None and retime != '': endt = datetime.datetime.fromtimestamp(int(retime)) retime = endt.strftime("%Y-%m-%d %H:%M:%S.%f") queryset = queryset.filter(data_joined__lte=retime) if queryset.exists(): count = queryset.count() res = queryset[(page - 1) * line:page * line] sqlDict = CommonService.qs_to_dict(res) for k, v in enumerate(sqlDict["datas"]): if len(v['fields']['role']) > 0: role_query_set = Role.objects.get(rid=v['fields']['role'][0]) sqlDict["datas"][k]['fields']['role'].append(role_query_set.roleName) for val in res: if v['pk'] == val.userID: if sqlDict["datas"][k]['fields']['online'] is True: dl_time = val.last_login + datetime.timedelta(minutes=5) now_time = timezone.localtime(timezone.now()) if now_time > dl_time: sqlDict["datas"][k]['fields']['online'] = False ue = UserExModel.objects.filter(userID=v['pk']) if ue.exists(): sqlDict["datas"][k]['fields']['appBundleId'] = ue[0].appBundleId else: sqlDict["datas"][k]['fields']['appBundleId'] = '' sqlDict['count'] = count return response.json(0, sqlDict) return response.json(0, {'datas': [], 'count': 0})