From 02cd05eb7e4b1dc693f6349d2a7434c84ad02d89 Mon Sep 17 00:00:00 2001 From: nineven Date: Mon, 1 Jun 2020 17:48:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AD=98=E5=82=A8=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=A4=B1=E8=B4=A5=20=E5=8D=B4=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fir_ser/api/models.py | 2 +- fir_ser/api/utils/utils.py | 5 +++-- fir_ser/api/views/storage.py | 28 ++++++++++++++++------------ 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/fir_ser/api/models.py b/fir_ser/api/models.py index ff59b76..d601ce5 100644 --- a/fir_ser/api/models.py +++ b/fir_ser/api/models.py @@ -173,7 +173,7 @@ class AppStorage(models.Model): return super(AppStorage, self).save(*args, **kwargs) else: return - super(AppStorage, self).save(*args, **kwargs) + return super(AppStorage, self).save(*args, **kwargs) def __str__(self): return "%s %s" % (self.user_id.get_username(), self.name) diff --git a/fir_ser/api/utils/utils.py b/fir_ser/api/utils/utils.py index 2a27814..66af8ae 100644 --- a/fir_ser/api/utils/utils.py +++ b/fir_ser/api/utils/utils.py @@ -109,5 +109,6 @@ def valid_captcha(cptch_key, code, username): def upload_oss_default_head_img(user_obj, storage_obj): head_img_full_path = os.path.join(MEDIA_ROOT, "head_img.jpeg") - storage_obj = Storage(user_obj, storage_obj) - return storage_obj.upload_file(head_img_full_path) + if storage_obj: + storage_obj = Storage(user_obj, storage_obj) + return storage_obj.upload_file(head_img_full_path) diff --git a/fir_ser/api/views/storage.py b/fir_ser/api/views/storage.py index 2cf80f7..8dcc070 100644 --- a/fir_ser/api/views/storage.py +++ b/fir_ser/api/views/storage.py @@ -72,6 +72,7 @@ class StorageView(APIView): res.msg = serializer.validated_data logger.info("user %s add new storage success" % (request.user)) else: + storage_obj.delete() logger.error("user %s add new storage failed" % (request.user)) res.msg = "文件上传校验失败,请检查参数是否正确" res.code = 1005 @@ -104,33 +105,36 @@ class StorageView(APIView): res.msg = '修改失败' return Response(res.dict) - try: - data['additionalparameters'] = json.dumps(data.get('additionalparameter', '')) - except Exception as e: - logger.error("user:%s additionalparameters %s dumps failed Exception:%s" % ( - request.user, data.get('additionalparameter', ''), e)) - storage_id = data.get("id", None) if storage_id: if storage_id == request.user.storage.id: res.msg = '存储正在使用中,无法修改' res.code = 1007 return Response(res.dict) + try: + data['additionalparameters'] = json.dumps(data.get('additionalparameter', '')) + except Exception as e: + del data['additionalparameters'] + logger.error("user:%s additionalparameters %s dumps failed Exception:%s" % ( + request.user, data.get('additionalparameter', ''), e)) + storage_obj = AppStorage.objects.filter(id=storage_id, user_id=request.user).first() + storage_obj_bak = AppStorage.objects.filter(id=storage_id, user_id=request.user).first() serializer = StorageSerializer(instance=storage_obj, data=data, context={'user_obj': request.user}, partial=True) if serializer.is_valid(): - storage_obj = serializer.save() - if storage_obj: - if upload_oss_default_head_img(request.user, storage_obj): + new_storage_obj = serializer.save() + if new_storage_obj: + if upload_oss_default_head_img(request.user, new_storage_obj): res.msg = serializer.validated_data - logger.info("user %s add new storage success" % (request.user)) + logger.info("user %s update storage success" % (request.user)) else: - logger.error("user %s add new storage failed" % (request.user)) + storage_obj_bak.save() + logger.error("user %s update storage failed" % (request.user)) res.msg = "文件上传校验失败,请检查参数是否正确" res.code = 1005 else: - logger.info("user %s add new storage failed" % (request.user)) + logger.info("user %s update storage failed" % (request.user)) res.msg = serializer.errors res.code = 1005 else: