views.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from account.decorators import login_required
  2. from .models import Group
  3. from account.models import User, get_user
  4. from utils.debug import debug_view
  5. from utils.http import make_json_response
  6. from utils.crypto import secure_transport
  7. # Create your views here.
  8. @secure_transport
  9. # @debug_view('group_id')
  10. @login_required
  11. def get_group_root_folder(request):
  12. user = get_user(request)
  13. data = request.POST
  14. group_id = data.get('group_id')
  15. try:
  16. group = user.joined_groups.get(group_id=group_id)
  17. except:
  18. return make_json_response(code=403, error='群不存在')
  19. root_folder = group.get_root_folder()
  20. return make_json_response(root_folder_id=root_folder.folder_id)
  21. @secure_transport
  22. # @debug_view('group_id')
  23. @login_required
  24. def join_group(request):
  25. user = get_user(request)
  26. data = request.POST
  27. group_id = data.get('group_id')
  28. if user.joined_groups.filter(group_id=group_id).exists():
  29. return make_json_response(code=402, error='已在群内')
  30. try:
  31. group = Group.objects.get(group_id=group_id)
  32. except:
  33. return make_json_response(code=403, error='群不存在')
  34. group.members.add(user)
  35. group.save()
  36. return make_json_response()
  37. @secure_transport
  38. # @debug_view('group_name')
  39. @login_required
  40. def create_group(request):
  41. user = get_user(request)
  42. data = request.POST
  43. group_name = data.get('group_name')
  44. try:
  45. Group.objects.create(group_name=group_name, creator=user)
  46. except:
  47. return make_json_response(code=500, error='新建群失败')
  48. return make_json_response()
  49. @secure_transport
  50. # @debug_view('group_id')
  51. @login_required
  52. def quit_group(request):
  53. user = get_user(request)
  54. data = request.POST
  55. group_id = data.get('group_id')
  56. try:
  57. group = user.joined_groups.get(group_id=group_id)
  58. except:
  59. return make_json_response(code=403, error='群不存在')
  60. if group.creator == user:
  61. return make_json_response(code=421, error='群主不可退群')
  62. group.members.remove(user)
  63. group.save()
  64. return make_json_response()
  65. # 获取你所在的所有群组
  66. @secure_transport
  67. # @debug_view()
  68. @login_required
  69. def group_list(request):
  70. user = get_user(request)
  71. _list = list(map(lambda g: g.to_json(), user.joined_groups.all()))
  72. return make_json_response(group_list=_list)
  73. # 删除群组