Browse Source

Merge remote-tracking branch 'origin/test'

locky 3 years ago
parent
commit
c0a4d55633
2 changed files with 122 additions and 105 deletions
  1. 122 104
      AdminController/DeviceManagementController.py
  2. 0 1
      Model/models.py

+ 122 - 104
AdminController/DeviceManagementController.py

@@ -4,15 +4,16 @@ import json
 import time
 import time
 
 
 from django.db import transaction
 from django.db import transaction
+from django.db.models import Q
+
 from Object.ResponseObject import ResponseObject
 from Object.ResponseObject import ResponseObject
 from Object.TokenObject import TokenObject
 from Object.TokenObject import TokenObject
 from django.views.generic.base import View
 from django.views.generic.base import View
 from Service.ModelService import ModelService
 from Service.ModelService import ModelService
 from Service.CommonService import CommonService
 from Service.CommonService import CommonService
 from Model.models import Device_Info, UidSetModel, LogModel, UID_Bucket, Unused_Uid_Meal, Order_Model, StsCrdModel, \
 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):
 class DeviceManagement(View):
     def get(self, request, *args, **kwargs):
     def get(self, request, *args, **kwargs):
@@ -35,119 +36,93 @@ class DeviceManagement(View):
             return response.json(tko.code)
             return response.json(tko.code)
         response.lang = tko.lang
         response.lang = tko.lang
         userID = tko.userID
         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)
+        elif operation == 'getDeviceTypeList':
+            return self.getDeviceTypeList(request_dict, response)
+        else:
+            return response.json(444, 'operation')
 
 
+    # 获取设备信息数据
+    def getDeviceInfoList(self, request_dict, response):
         pageNo = request_dict.get('pageNo', None)
         pageNo = request_dict.get('pageNo', None)
         pageSize = request_dict.get('pageSize', None)
         pageSize = request_dict.get('pageSize', None)
         UID = request_dict.get('UID', None)
         UID = request_dict.get('UID', None)
+        NickName = request_dict.get('NickName', None)
+        username = request_dict.get('username', None)
+
         if not all([pageNo, pageSize]):
         if not all([pageNo, pageSize]):
             return response.json(444)
             return response.json(444)
 
 
         page = int(pageNo)
         page = int(pageNo)
         line = int(pageSize)
         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):
-
-        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)
+        try:
+            if UID or NickName or username:    # 条件查询
+                if UID:
+                    device_info_qs = Device_Info.objects.filter(UID__contains=UID)
+                if NickName:
+                    device_info_qs = Device_Info.objects.filter(NickName__contains=NickName)
+                if username:
+                    device_info_qs = Device_Info.objects.filter(Q(userID__username__contains=username) |
+                                                                Q(userID__userEmail__contains=username) |
+                                                                Q(userID__phone__contains=username))
+                total = len(device_info_qs)
+                if not total:
+                    return response.json(0, {'list': {}, 'total': 0})
+                device_info_qs = device_info_qs[(page - 1) * line:page * line]
+            else:   # 查询全部
+                total = Device_Info.objects.filter().count()
+                device_info_qs = Device_Info.objects.filter()[(page - 1) * line:page * line]
+                if not device_info_qs.exists():
+                    return response.json(0, {'list': {}, 'total': 0})
 
 
-        page = int(pageNo)
-        line = int(pageSize)
+            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': total})
+        except Exception as e:
+            print(e)
+            return response.json(500, repr(e))
 
 
-        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})
+    # 根据id删除设备
+    def deleteDevice(self, request_dict, response):
+        deviceID = request_dict.get('deviceID', None)
+        if not deviceID:
+            return response.json(444)
 
 
-    # 重置设备云存
-    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)
         uid = request_dict.get('uid', None)
         if not uid:
         if not uid:
             return response.json(404)
             return response.json(404)
@@ -157,10 +132,10 @@ class DeviceManagement(View):
             content = json.loads(json.dumps(request_dict))
             content = json.loads(json.dumps(request_dict))
             log = {
             log = {
                 'ip': ip,
                 'ip': ip,
-                'user_id': 1,
+                'user_id': 2,
                 'status': 200,
                 'status': 200,
                 'time': int(time.time()),
                 'time': int(time.time()),
-                'url': 'uidset/primary_user_reset',
+                'url': 'deviceManagement/resetPrimaryUser',
                 'content': json.dumps(content),
                 'content': json.dumps(content),
                 'operation': '{}重置设备主用户'.format(uid),
                 'operation': '{}重置设备主用户'.format(uid),
             }
             }
@@ -168,4 +143,47 @@ class DeviceManagement(View):
             Device_Info.objects.filter(UID=uid).update(vodPrimaryUserID='', vodPrimaryMaster='')
             Device_Info.objects.filter(UID=uid).update(vodPrimaryUserID='', vodPrimaryMaster='')
             return response.json(0)
             return response.json(0)
         except Exception as e:
         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))
+
+    # 获取设备类型数据
+    def getDeviceTypeList(self, request_dict, response):
+        deviceType = request_dict.get('deviceType', None)
+
+        try:
+            return response.json(0)
+        except Exception as e:
+            print(e)
+            return response.json(500, repr(e))

+ 0 - 1
Model/models.py

@@ -798,7 +798,6 @@ class SysMsgModel(models.Model):
     updTime = models.IntegerField(verbose_name='更新时间', default=0)
     updTime = models.IntegerField(verbose_name='更新时间', default=0)
     eventType = models.IntegerField(verbose_name='消息类型', default=0)  # 默认系统消息类型,0系统消息,1 ipcamera消息
     eventType = models.IntegerField(verbose_name='消息类型', default=0)  # 默认系统消息类型,0系统消息,1 ipcamera消息
     uid = models.CharField(default='', max_length=20, db_index=True, verbose_name='设备UID')
     uid = models.CharField(default='', max_length=20, db_index=True, verbose_name='设备UID')
-    electricity = models.CharField(default='', max_length=10, verbose_name='电量')
 
 
     class Meta:
     class Meta:
         db_table = 'sys_msg'
         db_table = 'sys_msg'