You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
flyapps/fir_ser/api/views/storage.py

131 lines
5.2 KiB

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# project: 3月
# author: liuyu
# date: 2020/3/4
from rest_framework.views import APIView
from api.utils.response import BaseResponse
from api.utils.auth import ExpiringTokenAuthentication
from rest_framework.response import Response
import json
from api.utils.storage.caches import del_cache_storage
5 years ago
from api.models import AppStorage, UserInfo
from api.utils.serializer import StorageSerializer
import logging
logger = logging.getLogger(__file__)
class StorageView(APIView):
authentication_classes = [ExpiringTokenAuthentication, ]
def get(self, request):
res = BaseResponse()
# [1,2] 表示七牛存储和阿里云存储
storage_obj = AppStorage.objects.filter(user_id=request.user, storage_type__in=[1, 2])
if storage_obj:
storage_serializer = StorageSerializer(storage_obj, many=True)
storage_data_lists = storage_serializer.data
storage_lists = {}
for storage_data in storage_data_lists:
storage_type = storage_data.get("storage_type")
if not storage_lists.get(storage_type):
storage_lists[storage_type] = []
storage_lists[storage_type].append(storage_data)
res.data = storage_data_lists
use_storage_obj = request.user.storage
if use_storage_obj:
res.storage = use_storage_obj.id
else:
5 years ago
res.storage = -1 # 默认存储
res.storage_list = []
storage_org_list = list(AppStorage.storage_choices)
for storage_t in storage_org_list:
if storage_t[0] in [0, 3]: continue
res.storage_list.append({'id': storage_t[0], 'name': storage_t[1]})
admin_storage = UserInfo.objects.filter(is_superuser=True).order_by('pk').first()
5 years ago
res.is_admin_storage = False
if admin_storage and admin_storage.uid == request.user.uid:
res.is_admin_storage = True
return Response(res.dict)
def post(self, request):
res = BaseResponse()
data = request.data
logger.info("user %s add new storage data:%s" % (request.user, data))
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))
serializer = StorageSerializer(data=data, context={'user_obj': request.user})
if serializer.is_valid():
serializer.save()
res.msg = serializer.validated_data
logger.info("user %s add new storage success" % (request.user))
else:
logger.info("user %s add new storage failed" % (request.user))
res.msg = serializer.errors
res.code = 1005
return Response(res.dict)
def put(self, request):
res = BaseResponse()
data = request.data
logger.info("user %s update storage data:%s" % (request.user, data))
5 years ago
use_storage_id = data.get("use_storage_id", None)
if use_storage_id:
try:
if use_storage_id == -1:
UserInfo.objects.filter(pk=request.user.pk).update(storage=None)
else:
UserInfo.objects.filter(pk=request.user.pk).update(storage_id=use_storage_id)
del_cache_storage(request.user)
except Exception as e:
logger.error("update user %s storage failed Exception:%s" % (request.user, e))
5 years ago
res.code = 1006
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:
storage_obj = 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():
serializer.save()
res.msg = serializer.validated_data
else:
res.msg = serializer.errors
res.code = 1005
else:
res.msg = '该存储不存在'
res.code = 1007
return Response(res.dict)
def delete(self, request):
res = BaseResponse()
storage_id = request.query_params.get("id", None)
if storage_id:
try:
AppStorage.objects.filter(user_id=request.user, id=storage_id).delete()
logger.error("user %s delete storage id:%s success" % (request.user, storage_id))
except Exception as e:
logger.error("user %s delete storage id:%s failed Exception:%s" % (request.user, storage_id, e))
else:
res.code = 1004
res.msg = '该存储不存在'
return Response(res.dict)