userjoinview.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  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. group.users.add(user)
  26. executor, action, method = get_log(request)
  27. gen_log(action, "加入分组", group.name, method, executor)
  28. return HttpResponse(status=200, content='加入成功')