userjoinview.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import logging
  2. from django.http import HttpResponse
  3. from rest_framework.views import APIView
  4. from apps.group.models import Group
  5. from apps.log.models import get_log, gen_log
  6. from apps.user.middleware.rolecontrol import RoleControl
  7. from apps.user.models import User
  8. logger = logging.getLogger('django')
  9. class UserJoinView(APIView):
  10. authentication_classes = []
  11. @staticmethod
  12. @RoleControl
  13. def post(request, user_id, *args, **kwargs):
  14. code = request.POST.get('code')
  15. group = Group.objects.filter(code=code, delete=False)
  16. if not group:
  17. logger.error("分组不存在")
  18. return HttpResponse(status=500, content='分组不存在')
  19. group = group[0]
  20. user = User.objects.filter(id=user_id)
  21. if not user:
  22. logger.error("用户不存在")
  23. return HttpResponse(status=404, content='用户不存在')
  24. user = user[0]
  25. if user.identify == 1:
  26. logger.error("管理员角色不能加入小组")
  27. return HttpResponse(status=400, content='管理员不能加入小组')
  28. group.users.add(user)
  29. executor, action, method = get_log(request)
  30. gen_log(action, "加入分组", group.name, method, executor)
  31. return HttpResponse(status=200, content='加入成功')