Explorar o código

APP设备新增V2图标

linhaohong hai 1 ano
pai
achega
2acc0f66b3
Modificáronse 1 ficheiros con 101 adicións e 48 borrados
  1. 101 48
      AdminController/DeviceManagementController.py

+ 101 - 48
AdminController/DeviceManagementController.py

@@ -545,10 +545,10 @@ class DeviceManagement(View):
                 app_bundle_qs = app_bundle_qs.filter(app_device_type__devicenamelanguage__lang=lang)
             app_bundle_qs = app_bundle_qs.annotate(
                 model=F('app_device_type__model'), type=F('app_device_type__type'), icon=F('app_device_type__icon'),
-                lang=F('app_device_type__devicenamelanguage__lang'),
+                lang=F('app_device_type__devicenamelanguage__lang'), iconV2=F('app_device_type__iconV2'),
                 name=F('app_device_type__devicenamelanguage__name'),
                 sort=F('app_device_type__devicenamelanguage__sort')).values('id', 'app_device_type__id', 'model',
-                                                                            'type', 'icon', 'app_bundle_id',
+                                                                            'type', 'icon', 'iconV2', 'app_bundle_id',
                                                                             'app_device_type__devicenamelanguage__id',
                                                                             'lang', 'name', 'sort',
                                                                             'app_device_type__app_version_number_id',
@@ -590,12 +590,13 @@ class DeviceManagement(View):
         model = request_dict.get('model', None)
         type = request_dict.get('type', None)
         icon = request.FILES.get('icon', None)
+        iconV2 = request.FILES.get('iconV2', None)
         # device_name_language表数据
         device_name_language_id = request_dict.get('app_device_type__devicenamelanguage__id', None)
         sort = request_dict.get('sort', None)
         version_number = request_dict.get('version_number', None)
         config = request_dict.get('config', None)
-
+        now_time = int(time.time())
         if not all([app_device_type_id, device_name_language_id, model, type, sort, version_number]):
             return response.json(444)
 
@@ -605,28 +606,40 @@ class DeviceManagement(View):
 
         try:
             with transaction.atomic():
-                if icon:
-                    icon_path = 'https://ansjerfilemanager.s3.amazonaws.com/app/device_type_images/{}'.format(icon)
+                if icon or iconV2:
+                    bucket_name = 'ansjerfilemanager'
+                    s3 = AmazonS3Util(AWS_ACCESS_KEY_ID[1], AWS_SECRET_ACCESS_KEY[1], AWS_SES_ACCESS_REGION)
+                    icon_path = ""
+                    icon_v2_path = ""
+                    if icon:
+                        icon_path = f'https://ansjerfilemanager.s3.amazonaws.com/app/device_type_images/{now_time}_{icon.name}'
+                        file_key = f'app/device_type_images/{now_time}_{icon.name}'
+                        s3.upload_file_obj(
+                            bucket_name,
+                            file_key,
+                            icon,
+                            {'ContentType': icon.content_type, 'ACL': 'public-read'})
+                    if iconV2:
+                        icon_v2_path = f'https://ansjerfilemanager.s3.amazonaws.com/app/device_type_images/{now_time}_v2_{iconV2.name}'
+                        file_v2_key = f'app/device_type_images/{now_time}_v2_{iconV2.name}'
+                        s3.upload_file_obj(
+                            bucket_name,
+                            file_v2_key,
+                            iconV2,
+                            {'ContentType': iconV2.content_type, 'ACL': 'public-read'})
                     if config:
                         try:
                             config = json.loads(config)
                         except:
                             return response.json(444, 'config必须是一个json数据')
-                        AppDeviceType.objects.filter(id__in=list_app_device_type_id) \
-                            .update(model=model, type=type, icon=icon_path, app_version_number_id=version_number,
+                        AppDeviceType.objects.filter(id=app_device_type_id) \
+                            .update(model=model, type=type, icon=icon_path, iconV2=icon_v2_path,
+                                    app_version_number_id=version_number,
                                     config=config)
                     else:
-                        AppDeviceType.objects.filter(id__in=list_app_device_type_id) \
-                            .update(model=model, type=type, icon=icon_path, app_version_number_id=version_number)
-                    bucket_name = 'ansjerfilemanager'
-                    file_key = 'app/device_type_images/{}'.format(icon)
-                    s3 = AmazonS3Util(AWS_ACCESS_KEY_ID[1], AWS_SECRET_ACCESS_KEY[1], AWS_SES_ACCESS_REGION)
-                    # 地址:https://ansjerfilemanager.s3.amazonaws.com/app/device_type_images/camera_c190.png
-                    s3.upload_file_obj(
-                        bucket_name,
-                        file_key,
-                        icon,
-                        {'ContentType': icon.content_type, 'ACL': 'public-read'})
+                        AppDeviceType.objects.filter(id=app_device_type_id) \
+                            .update(model=model, type=type, icon=icon_path, iconV2=icon_v2_path,
+                                    app_version_number_id=version_number)
                 else:
                     if config:
                         try:
@@ -652,6 +665,7 @@ class DeviceManagement(View):
         model = request_dict.get('model', None)
         type = request_dict.get('type', None)
         icon = request.FILES.get('icon', None)
+        iconV2 = request.FILES.get('iconV2', None)
         # device_name_language表数据
         device_name_language_id = request_dict.get('app_device_type__devicenamelanguage__id', None)
         lang = request_dict.get('lang', None)
@@ -659,33 +673,45 @@ class DeviceManagement(View):
         sort = request_dict.get('sort', None)
         version_number = request_dict.get('version_number', None)
         config = request_dict.get('config', None)
-
+        now_time = int(time.time())
         if not all([app_device_type_id, model, type, device_name_language_id, lang, name, sort]):
             return response.json(444)
         try:
             with transaction.atomic():
-                if icon:
-                    icon_path = 'https://ansjerfilemanager.s3.amazonaws.com/app/device_type_images/{}'.format(icon)
+                if icon or iconV2:
+                    bucket_name = 'ansjerfilemanager'
+                    s3 = AmazonS3Util(AWS_ACCESS_KEY_ID[1], AWS_SECRET_ACCESS_KEY[1], AWS_SES_ACCESS_REGION)
+                    icon_path = ""
+                    icon_v2_path = ""
+                    if icon:
+                        icon_path = f'https://ansjerfilemanager.s3.amazonaws.com/app/device_type_images/{now_time}_{icon.name}'
+                        file_key = f'app/device_type_images/{now_time}_{icon.name}'
+                        s3.upload_file_obj(
+                            bucket_name,
+                            file_key,
+                            icon,
+                            {'ContentType': icon.content_type, 'ACL': 'public-read'})
+                    if iconV2:
+                        icon_v2_path = f'https://ansjerfilemanager.s3.amazonaws.com/app/device_type_images/{now_time}_v2_{iconV2.name}'
+                        file_v2_key = f'app/device_type_images/{now_time}_v2_{iconV2.name}'
+                        s3.upload_file_obj(
+                            bucket_name,
+                            file_v2_key,
+                            iconV2,
+                            {'ContentType': iconV2.content_type, 'ACL': 'public-read'})
                     if config:
                         try:
                             config = json.loads(config)
                         except:
                             return response.json(444, 'config必须是一个json数据')
                         AppDeviceType.objects.filter(id=app_device_type_id) \
-                            .update(model=model, type=type, icon=icon_path, app_version_number_id=version_number,
+                            .update(model=model, type=type, icon=icon_path, iconV2=icon_v2_path,
+                                    app_version_number_id=version_number,
                                     config=config)
                     else:
                         AppDeviceType.objects.filter(id=app_device_type_id) \
-                            .update(model=model, type=type, icon=icon_path, app_version_number_id=version_number)
-                    bucket_name = 'ansjerfilemanager'
-                    file_key = 'app/device_type_images/{}'.format(icon)
-                    s3 = AmazonS3Util(AWS_ACCESS_KEY_ID[1], AWS_SECRET_ACCESS_KEY[1], AWS_SES_ACCESS_REGION)
-                    # 地址:https://ansjerfilemanager.s3.amazonaws.com/app/device_type_images/camera_c190.png
-                    s3.upload_file_obj(
-                        bucket_name,
-                        file_key,
-                        icon,
-                        {'ContentType': icon.content_type, 'ACL': 'public-read'})
+                            .update(model=model, type=type, icon=icon_path, iconV2=icon_v2_path,
+                                    app_version_number_id=version_number)
                 else:
                     if config:
                         try:
@@ -785,6 +811,7 @@ class DeviceManagement(View):
         config = request_dict.get('config', None)
         # 上传图标
         file = request.FILES.get('iconFile', None)
+        file_v2 = request.FILES.get('iconV2File', None)
         if config:
             config = json.loads(config)
         try:
@@ -818,13 +845,18 @@ class DeviceManagement(View):
             if need_upload:
                 with transaction.atomic():
                     fileName = file.name
+                    fileV2Name = file_v2.name
                     now_time = int(time.time())
-                    response_url = f'https://ansjerfilemanager.s3.amazonaws.com/app/device_type_images/{now_time}_{fileName}'
+                    icon = f'https://ansjerfilemanager.s3.amazonaws.com/app/device_type_images/{now_time}_{fileName}'
+                    iconV2 = f'https://ansjerfilemanager.s3.amazonaws.com/app/device_type_images/{now_time}_v2_{fileV2Name}'
                     bucket_name = 'ansjerfilemanager'
                     file_key = f'app/device_type_images/{now_time}_{fileName}'
+                    file_v2_key = f'app/device_type_images/{now_time}_v2_{fileV2Name}'
                     s3 = AmazonS3Util(AWS_ACCESS_KEY_ID[1], AWS_SECRET_ACCESS_KEY[1], AWS_SES_ACCESS_REGION)
                     s3.upload_file_obj(bucket_name, file_key, file,
                                        {'ContentType': file.content_type, 'ACL': 'public-read'})
+                    s3.upload_file_obj(bucket_name, file_v2_key, file,
+                                       {'ContentType': file_v2.content_type, 'ACL': 'public-read'})
                     for app_id in app_bundle_id:
                         for k, v in data_name.items():
                             record_key = {"app_bundle_id": app_id, "type": type, "version_number": version_number,
@@ -833,7 +865,7 @@ class DeviceManagement(View):
                             if not any(rec == record_key for rec in existing_records):
                                 app_bundle = AppBundle.objects.filter(id=app_id).values('id', 'app_bundle_id').first()
                                 app_device_type_qs = AppDeviceType.objects.create(model=model, type=type,
-                                                                                  icon=response_url,
+                                                                                  icon=icon, iconV2=iconV2,
                                                                                   app_version_number_id=version_number,
                                                                                   config=config)
                                 DeviceNameLanguage.objects.create(lang=k, name=v, sort=sort,
@@ -868,6 +900,9 @@ class DeviceManagement(View):
         lang = request_dict.get('lang', 'en')
         app_bundle_id = request_dict.get('appBundleId', None)
         version_number = request_dict.get('versionNumber', None)
+        # 查询设备信息图标
+        api_version = request_dict.get('apiVersion', None)
+
         if not all([lang, app_bundle_id, version_number]):
             return response.json(444)
         try:
@@ -892,22 +927,37 @@ class DeviceManagement(View):
                                                          app_device_type__app_version_number_id=version_number). \
                     annotate(
                     model=F('app_device_type__model'), type=F('app_device_type__type'), icon=F('app_device_type__icon'),
-                    name=F('app_device_type__devicenamelanguage__name'),
+                    name=F('app_device_type__devicenamelanguage__name'), iconV2=F('app_device_type__iconV2'),
                     sort=F('app_device_type__devicenamelanguage__sort'),
                     config=F('app_device_type__config'),
                     app_version_number_id=F('app_device_type__app_version_number_id')).values('model', 'type', 'icon',
                                                                                               'name', 'sort', 'config',
+                                                                                              'iconV2',
                                                                                               'app_device_type__app_version_number_id')
-                for app_bundle in app_bundle_qs:
-                    app_bundle_list.append({
-                        'model': app_bundle['model'],
-                        'type': app_bundle['type'],
-                        'icon': app_bundle['icon'],
-                        'name': app_bundle['name'],
-                        'sort': app_bundle['sort'],
-                        'config': app_bundle['config'],
-                        'app_device_type__app_version_number_id': app_bundle['app_device_type__app_version_number_id'],
-                    })
+                if api_version == "V2":
+                    for app_bundle in app_bundle_qs:
+                        app_bundle_list.append({
+                            'model': app_bundle['model'],
+                            'type': app_bundle['type'],
+                            'icon': app_bundle['iconV2'] if app_bundle['iconV2'] != "" else app_bundle['icon'],
+                            'name': app_bundle['name'],
+                            'sort': app_bundle['sort'],
+                            'config': app_bundle['config'],
+                            'app_device_type__app_version_number_id': app_bundle[
+                                'app_device_type__app_version_number_id'],
+                        })
+                else:
+                    for app_bundle in app_bundle_qs:
+                        app_bundle_list.append({
+                            'model': app_bundle['model'],
+                            'type': app_bundle['type'],
+                            'icon': app_bundle['icon'],
+                            'name': app_bundle['name'],
+                            'sort': app_bundle['sort'],
+                            'config': app_bundle['config'],
+                            'app_device_type__app_version_number_id': app_bundle[
+                                'app_device_type__app_version_number_id'],
+                        })
             dvr_list = [app_bundle for app_bundle in app_bundle_list if app_bundle['model'] == 1]
             ipc_list = [app_bundle for app_bundle in app_bundle_list if app_bundle['model'] == 2]
             res = {
@@ -1031,13 +1081,16 @@ class DeviceManagement(View):
         config = request_dict.get('config', None)
         # 上传图标
         files = request.FILES.get('iconFile', None)
+        # 上传图标
+        files_v2 = request.FILES.get('iconV2File', None)
         # 需要添加到哪一个服
         region = request_dict.get("region", None)  # 获取需要同步的区域
 
         # 检查必需参数
-        if not all([model, type, name, sort, version_number, files, app_bundle_name, region]):
+        if not all([model, type, name, sort, version_number, app_bundle_name, region]):
+            return response.json(444, 'Missing required parameters')
+        if files is None and files_v2 is None:
             return response.json(444, 'Missing required parameters')
-
         regions = region.split(",")  # 将同步区域拆分为列表
         return_value_list = []
         for region in regions:
@@ -1070,7 +1123,7 @@ class DeviceManagement(View):
                 # 发送 POST 请求调用 add_app_device_type 方法
                 response_value = requests.post(url + "/deviceManagement/addAppDeviceType",
                                                data=form_data,
-                                               files={'iconFile': files})
+                                               files={'iconFile': files, 'iconV2File': files_v2})
                 return_value = {region: response_value.json()["data"]}
                 return_value_list.append(return_value)
             except Exception as e: