Browse Source

完善app设备设备类型上传图标功能(先判断再上传)

guanhailong 3 years ago
parent
commit
719ae39a25
1 changed files with 22 additions and 29 deletions
  1. 22 29
      AdminController/DeviceManagementController.py

+ 22 - 29
AdminController/DeviceManagementController.py

@@ -74,35 +74,13 @@ class DeviceManagement(View):
                 return self.edit_app_device_type(request_dict, response)
             elif operation == 'deleteAppDeviceType':  # 删除app设备类型数据
                 return self.delete_app_device_type(request_dict, response)
-            elif operation == 'addAppDeviceType':  # 添加app设备类型数据
-                return self.add_app_device_type(request_dict, response)
+            elif operation == 'addAppDeviceType':  # 添加app设备类型数据并上传图标
+                return self.add_app_device_type(request_dict, response, request)
             elif operation == 'getAppBundle':  # 获取app版本包
                 return self.get_app_bundle(response)
-            elif operation == 'upFile':
-                return self.up_file(request_dict, request, response)
             else:
                 return response.json(444, 'operation')
 
-    @classmethod
-    def up_file(cls, request_dict, request, response):
-        file = request.FILES.get('iconFile', None)
-        name = file.name
-        if not all([file]):
-            return response.json(444)
-        try:
-            file_key = 'app/device_type_images/{}'.format(name)
-            s3 = AmazonS3Util(AWS_ACCESS_KEY_ID[1], AWS_SECRET_ACCESS_KEY[1], AWS_SES_ACCESS_REGION)
-            # 存储桶
-            bucket = 'ansjerfilemanager'
-            # 地址:https://ansjerfilemanager.s3.amazonaws.com/app/device_type_images/camera_c190.png
-            s3.upload_file_obj(bucket, file_key, file, {'ContentType': file.content_type, 'ACL':'public-read'})
-            return response.json(0)
-        except Exception as e:
-            print(e)
-            return response.json(500, repr(e))
-
-
-
     @classmethod
     def get_app_bundle(cls, response):
         app_bundle_qs = AppBundle.objects.all().values('id', 'app_bundle_id')
@@ -118,33 +96,48 @@ class DeviceManagement(View):
             return response.json(500, e)
 
     @staticmethod
-    def add_app_device_type(request_dict, response):
+    def add_app_device_type(request_dict, response, request):
         # 添加APP设备类型
         app_bundle_name = request_dict.get('appbundleName', None)
         app_bundle_id = request_dict.get(
             'app_bundle_id', '')[
-                     1:-1].split(',')  # '[1,2]' -> ['1','2']
+                        1:-1].split(',')  # '[1,2]' -> ['1','2']
         # app_device_type表数据
         model = request_dict.get('model', None)
         type = request_dict.get('type', None)
-        icon = request_dict.get('icon', None)
         # device_name_language表数据
         lang = request_dict.get('lang', None)
         name = request_dict.get('name', None)
         sort = request_dict.get('sort', None)
+        # 上传图标
+        file = request.FILES.get('iconFile', None)
+        fileName = file.name
 
-        if not all([model, type, icon, lang, name, sort]):
+        if not all([model, type, lang, name, sort]):
             return response.json(444)
         type = int(type)
 
         try:
             with transaction.atomic():
+
                 for app_id in app_bundle_id:
                     app_bundle_qs = AppBundle.objects.filter(id=app_id).values('id')
                     if not app_bundle_qs.exists():
                         AppBundle.objects.create(app_bundle_id=app_bundle_name, id=app_id)
                         return response.json(0)
-                app_device_type_qs = AppDeviceType.objects.create(model=model, type=type, icon=icon)
+                app_device_type_qs = AppDeviceType.objects.filter(type=type).values()
+                if app_device_type_qs.exists():
+                    return response.json(174)
+                # S3下文件夹路径+文件名 组成对象key
+                file_key = 'app/device_type_images/{}'.format(fileName)
+                s3 = AmazonS3Util(AWS_ACCESS_KEY_ID[1], AWS_SECRET_ACCESS_KEY[1], AWS_SES_ACCESS_REGION)
+                # 存储桶
+                bucket = 'ansjerfilemanager'
+                # 地址:https://ansjerfilemanager.s3.amazonaws.com/app/device_type_images/camera_c190.png
+                s3.upload_file_obj(bucket, file_key, file,
+                                   {'ContentType': file.content_type, 'ACL': 'public-read'})
+                response_url = s3.generate_file_obj_url(bucket, file_key)
+                app_device_type_qs = AppDeviceType.objects.create(model=model, type=type, icon=response_url)
                 DeviceNameLanguage.objects.create(lang=lang, name=name, sort=sort,
                                                   app_device_type_id=app_device_type_qs.id)
                 for app_id in app_bundle_id: