views.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from account.decorators import login_required
  2. from .models import Folder
  3. from account.models import get_user
  4. from utils.debug import debug_view
  5. from utils.http import make_json_response
  6. from utils.permission import can_delete
  7. from utils.crypto import secure_transport
  8. # Create your views here.
  9. @secure_transport
  10. # @debug_view()
  11. @login_required
  12. def get_root_folder(request):
  13. # 获取根目录
  14. user = get_user(request)
  15. return make_json_response(root_folder_id=user.get_root_folder().folder_id)
  16. # 在根目录下列出所有文件夹与文件
  17. @secure_transport
  18. # @debug_view('folder_id')
  19. @login_required
  20. def folder_list(request):
  21. data = request.POST
  22. user = get_user(request)
  23. folder_id = data.get('folder_id')
  24. try:
  25. folder = Folder.objects.get(folder_id=folder_id)
  26. except:
  27. return make_json_response(code=400, error='文件夹不存在')
  28. if not folder.check_permission(user=user):
  29. return make_json_response(code=404, error='没有权限')
  30. children_folders = folder.children_folders.all()
  31. children_files = folder.children_files.all()
  32. children = [*map(lambda f: {'type': 'folder', **f.to_json()}, children_folders),
  33. *map(lambda f: {'type': 'file', **f.to_json()}, children_files)]
  34. return make_json_response(children=children)
  35. # 增文件夹
  36. @secure_transport
  37. # @debug_view('father_folder_id', 'folder_name')
  38. @login_required
  39. def add_folder(request):
  40. data = request.POST
  41. user = get_user(request)
  42. # folder_id_random = ''.join(random.sample(string.digits, 8))
  43. # father_folder = Folder.objects.filter(folder_id=data.get('father_folder_id')).get()
  44. father_folder_id = data.get('father_folder_id')
  45. try:
  46. father_folder = Folder.objects.get(folder_id=father_folder_id)
  47. except:
  48. return make_json_response(code=400, error='文件夹不存在')
  49. if not father_folder.check_permission(user):
  50. return make_json_response(code=404, error='没有权限')
  51. folder_name = data.get('folder_name')
  52. print(f'folder_name={folder_name} father_folder_id={father_folder_id}')
  53. Folder.objects.create(folder_name=folder_name, father_folder=father_folder, owner=user, group=father_folder.group)
  54. return make_json_response()
  55. # 删除文件夹
  56. @secure_transport
  57. # @debug_view('folder_id')
  58. @login_required
  59. def delete_folder(request):
  60. data = request.POST
  61. user = get_user(request)
  62. folder_id = data.get('folder_id')
  63. try:
  64. folder = Folder.objects.get(folder_id=folder_id)
  65. except:
  66. return make_json_response(code=400, error='无此文件夹')
  67. if not can_delete(user=user, f=folder):
  68. return make_json_response(code=404, error='没有删除文件的权限')
  69. folder.delete()
  70. return make_json_response()