Quellcode durchsuchen

设备信息相关接口

locky vor 3 Jahren
Ursprung
Commit
9d9b382a3d
1 geänderte Dateien mit 93 neuen und 104 gelöschten Zeilen
  1. 93 104
      AdminController/DeviceManagementController.py

+ 93 - 104
AdminController/DeviceManagementController.py

@@ -10,9 +10,8 @@ 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
-import datetime
-from django.utils import timezone
+    VodHlsModel, ExperienceContextModel, DeviceTypeModel
+
 
 class DeviceManagement(View):
     def get(self, request, *args, **kwargs):
@@ -35,27 +34,21 @@ class DeviceManagement(View):
             return response.json(tko.code)
         response.lang = tko.lang
         userID = tko.userID
-        if userID is not None:
-            if operation == 'query':
-                return self.query(request_dict, userID, response)
-            elif operation == 'findByUser':
-                return self.findByUser(request_dict, userID, response)
-            elif operation == 'delete':
-                return self.delete(request_dict, userID, response)
-            elif operation == 'uidSetQuery':
-                return self.uidSetQuery(request_dict, userID, response)
-            elif operation == 'vod_reset':
-                return self.vod_reset(request, request_dict, response)
-            elif operation == 'primary_user_reset':
-                return self.primary_user_reset(request, request_dict, response)
-        return response.json(444, 'operation')
-
-
-    def query(self, request_dict, userID, response):
-        own_perm = ModelService.check_perm(userID=userID, permID=30)
-        if not own_perm :
-            return response.json(404)
+        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)
@@ -65,89 +58,52 @@ class DeviceManagement(View):
         page = int(pageNo)
         line = int(pageSize)
 
-        queryset = Device_Info.objects.all()
-        if UID:
-            queryset = queryset.filter(UID__contains=UID)
-        if queryset.exists():
-            count = queryset.count()
-            res = queryset[(page - 1) * line:page * line]
-            send_dict = CommonService.qs_to_dict(res)
-            for k, v in enumerate(send_dict["datas"]):
-                for val in res:
-                    if v['pk'] == val.id:
-                        if send_dict["datas"][k]['fields']['Online'] is True:
-                            dl_time = val.update_time+datetime.timedelta(minutes=60)
-                            now_time = timezone.localtime(timezone.now())
-                            if now_time > dl_time:
-                                pass
-                                # send_dict["datas"][k]['fields']['Online'] = False
-                        username= ModelService.get_user_name(send_dict["datas"][k]['fields']['userID'])
-                        send_dict["datas"][k]['fields']['username']=username
-                        primary= ModelService.get_user_name(send_dict["datas"][k]['fields']['primaryUserID'])
-                        send_dict["datas"][k]['fields']['primaryusername']=primary
-            return response.json(
-                0, {'list': send_dict, 'total': count})
-        return response.json(
-            0, {'list': {}, 'total': 0})
-
-    # 管理员查询接口
-    def uidSetQuery(self, request_dict, userID, response):
+        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))
 
-        pageNo = request_dict.get('pageNo', None)
-        pageSize = request_dict.get('pageSize', None)
-        UID = request_dict.get('UID', None)
-        if not all([pageNo, pageSize]):
+    # 根据id删除设备
+    def deleteDevice(self, request_dict, response):
+        deviceID = request_dict.get('deviceID', None)
+        if not deviceID:
             return response.json(444)
 
-        page = int(pageNo)
-        line = int(pageSize)
-
-        omqs = UidSetModel.objects.all().order_by('-id')
-        if UID:
-            omqs = omqs.filter(uid__contains=UID)
-        count = omqs.count()
-        order_ql = omqs[(page - 1) * line:page * line]
-        uidset_json = CommonService.qs_to_dict(order_ql)
-        uidset_json['count'] = count
-        return response.json(
-            0, {'list': uidset_json, 'total': count})
-
-    # 重置设备云存
-    def vod_reset(self, request, request_dict, response):
-        uid = request_dict.get('uid', None)
-        if uid:
-            try:
-                # 记录操作日志
-                ip = CommonService.get_ip_address(request)
-                content = json.loads(json.dumps(request_dict))
-                log = {
-                    'ip': ip,
-                    'user_id': 1,
-                    'status': 200,
-                    'time': int(datetime.time.time()),
-                    'url': 'uidset/vod_reset',
-                    '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()
-                    # UIDMainUser.objects.filter(UID=uid).delete()
-                    Device_Info.objects.filter(UID=uid).update(vodPrimaryUserID='', vodPrimaryMaster='')
-            except Exception as e:
-                print(e)
-                return response.json(500, repr(e))
-            else:
-                return response.json(0)
+        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 primary_user_reset(self, request, request_dict, response):
+    def resetPrimaryUser(self, request, request_dict, response):
         uid = request_dict.get('uid', None)
         if not uid:
             return response.json(404)
@@ -157,10 +113,10 @@ class DeviceManagement(View):
             content = json.loads(json.dumps(request_dict))
             log = {
                 'ip': ip,
-                'user_id': 1,
+                'user_id': 2,
                 'status': 200,
                 'time': int(time.time()),
-                'url': 'uidset/primary_user_reset',
+                'url': 'deviceManagement/resetPrimaryUser',
                 'content': json.dumps(content),
                 'operation': '{}重置设备主用户'.format(uid),
             }
@@ -168,4 +124,37 @@ class DeviceManagement(View):
             Device_Info.objects.filter(UID=uid).update(vodPrimaryUserID='', vodPrimaryMaster='')
             return response.json(0)
         except Exception as e:
-            return response.json(500, repr(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))