from account.decorators import login_required from .models import Folder from account.models import get_user from utils.debug import debug_view from utils.http import make_json_response from utils.permission import can_delete from utils.crypto import secure_transport # Create your views here. @secure_transport # @debug_view() @login_required def get_root_folder(request): # 获取根目录 user = get_user(request) return make_json_response(root_folder_id=user.get_root_folder().folder_id) # 在根目录下列出所有文件夹与文件 @secure_transport # @debug_view('folder_id') @login_required def folder_list(request): data = request.POST user = get_user(request) folder_id = data.get('folder_id') try: folder = Folder.objects.get(folder_id=folder_id) except: return make_json_response(code=400, error='文件夹不存在') if not folder.check_permission(user=user): return make_json_response(code=404, error='没有权限') children_folders = folder.children_folders.all() children_files = folder.children_files.all() children = [*map(lambda f: {'type': 'folder', **f.to_json()}, children_folders), *map(lambda f: {'type': 'file', **f.to_json()}, children_files)] return make_json_response(children=children) # 增文件夹 @secure_transport # @debug_view('father_folder_id', 'folder_name') @login_required def add_folder(request): data = request.POST user = get_user(request) # folder_id_random = ''.join(random.sample(string.digits, 8)) # father_folder = Folder.objects.filter(folder_id=data.get('father_folder_id')).get() father_folder_id = data.get('father_folder_id') try: father_folder = Folder.objects.get(folder_id=father_folder_id) except: return make_json_response(code=400, error='文件夹不存在') if not father_folder.check_permission(user): return make_json_response(code=404, error='没有权限') folder_name = data.get('folder_name') print(f'folder_name={folder_name} father_folder_id={father_folder_id}') Folder.objects.create(folder_name=folder_name, father_folder=father_folder, owner=user, group=father_folder.group) return make_json_response() # 删除文件夹 @secure_transport # @debug_view('folder_id') @login_required def delete_folder(request): data = request.POST user = get_user(request) folder_id = data.get('folder_id') try: folder = Folder.objects.get(folder_id=folder_id) except: return make_json_response(code=400, error='无此文件夹') if not can_delete(user=user, f=folder): return make_json_response(code=404, error='没有删除文件的权限') folder.delete() return make_json_response()