#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import time from django.db import transaction from Object.ResponseObject import ResponseObject from Object.TokenObject import TokenObject from django.views.generic.base import View from Service.ModelService import ModelService from Service.CommonService import CommonService from Model.models import Device_Info, UidSetModel, LogModel, UID_Bucket, Unused_Uid_Meal, Order_Model, StsCrdModel, \ VodHlsModel, ExperienceContextModel, DeviceTypeModel class DeviceManagement(View): def get(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') return self.validation(request.GET, request, operation) def post(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') return self.validation(request.POST, request, operation) def validation(self, request_dict, request, operation): language = request_dict.get('language', 'en') response = ResponseObject(language, 'pc') tko = TokenObject( request.META.get('HTTP_AUTHORIZATION'), returntpye='pc') if tko.code != 0: return response.json(tko.code) response.lang = tko.lang userID = tko.userID if not userID: return response.json(444, 'userID') if operation == 'getDeviceInfoList': return self.getDeviceInfoList(request_dict, response) elif operation == 'deleteDevice': return self.deleteDevice(request_dict, response) elif operation == 'resetVod': return self.resetVod(request, request_dict, response) elif operation == 'resetPrimaryUser': return self.resetPrimaryUser(request, request_dict, response) else: return response.json(444, 'operation') # 获取设备信息数据 def getDeviceInfoList(self, request_dict, response): pageNo = request_dict.get('pageNo', None) pageSize = request_dict.get('pageSize', None) UID = request_dict.get('UID', None) if not all([pageNo, pageSize]): return response.json(444) page = int(pageNo) line = int(pageSize) try: device_info_qs = Device_Info.objects.all() if UID: device_info_qs = device_info_qs.filter(UID__contains=UID) if not device_info_qs.exists(): return response.json(0, {'list': {}, 'total': 0}) count = device_info_qs.count() device_info_qs = device_info_qs[(page - 1) * line:page * line] device_info_list = CommonService.qs_to_dict(device_info_qs) for k, v in enumerate(device_info_list["datas"]): for device_info in device_info_qs: if v['pk'] == device_info.id: # 设备的用户名和主用户 username = ModelService.get_user_name(device_info_list["datas"][k]['fields']['userID']) device_info_list["datas"][k]['fields']['username'] = username device_info_list["datas"][k]['fields']['vodPrimaryMaster'] = device_info_list["datas"][k]['fields']['vodPrimaryMaster'] # 设备类型,是否支持Alexa和ip type = device_info_list["datas"][k]['fields']['Type'] device_type_qs = DeviceTypeModel.objects.filter(type=type).values('name') if device_type_qs.exists(): device_info_list["datas"][k]['fields']['Type'] = device_type_qs[0]['name'] uid_set_qs = UidSetModel.objects.filter(uid=device_info_list["datas"][k]['fields']['UID']).values('is_alexa', 'ip') if uid_set_qs.exists(): isAlexa = '是' if uid_set_qs[0]['is_alexa'] else '否' device_info_list["datas"][k]['fields']['isAlexa'] = isAlexa device_info_list["datas"][k]['fields']['ip'] = uid_set_qs[0]['ip'] return response.json(0, {'list': device_info_list, 'total': count}) except Exception as e: print(e) return response.json(500, repr(e)) # 根据id删除设备 def deleteDevice(self, request_dict, response): deviceID = request_dict.get('deviceID', None) if not deviceID: return response.json(444) try: Device_Info.objects.filter(id=deviceID).delete() return response.json(0) except Exception as e: print(e) return response.json(500, repr(e)) # 重置设备主用户 def resetPrimaryUser(self, request, request_dict, response): uid = request_dict.get('uid', None) if not uid: return response.json(404) try: # 记录操作日志 ip = CommonService.get_ip_address(request) content = json.loads(json.dumps(request_dict)) log = { 'ip': ip, 'user_id': 2, 'status': 200, 'time': int(time.time()), 'url': 'deviceManagement/resetPrimaryUser', 'content': json.dumps(content), 'operation': '{}重置设备主用户'.format(uid), } LogModel.objects.create(**log) Device_Info.objects.filter(UID=uid).update(vodPrimaryUserID='', vodPrimaryMaster='') return response.json(0) except Exception as e: return response.json(500, repr(e)) # 重置设备云存 def resetVod(self, request, request_dict, response): uid = request_dict.get('uid', None) if not uid: return response.json(404) try: # 记录操作日志 ip = CommonService.get_ip_address(request) content = json.loads(json.dumps(request_dict)) log = { 'ip': ip, 'user_id': 2, 'status': 200, 'time': int(time.time()), 'url': 'deviceManagement/resetVod', 'content': json.dumps(content), 'operation': '{}重置设备云存'.format(uid), } with transaction.atomic(): LogModel.objects.create(**log) # 删除和更新设备云存相关数据 UID_Bucket.objects.filter(uid=uid).delete() Unused_Uid_Meal.objects.filter(uid=uid).delete() Order_Model.objects.filter(UID=uid).delete() StsCrdModel.objects.filter(uid=uid).delete() VodHlsModel.objects.filter(uid=uid).delete() ExperienceContextModel.objects.filter(uid=uid).delete() Device_Info.objects.filter(UID=uid).update(vodPrimaryUserID='', vodPrimaryMaster='') return response.json(0) except Exception as e: print(e) return response.json(500, repr(e))