import logging from django.http import HttpResponse from rest_framework.views import APIView from apps.group.models import Group from apps.log.models import get_log, gen_log from apps.user.middleware.rolecontrol import RoleControl from apps.user.models import User logger = logging.getLogger('django') class UserJoinView(APIView): authentication_classes = [] @staticmethod @RoleControl def post(request, user_id, *args, **kwargs): code = request.POST.get('code') group = Group.objects.filter(code=code, delete=False) if not group: logger.error("分组不存在") return HttpResponse(status=500, content='分组不存在') group = group[0] user = User.objects.filter(id=user_id) if not user: logger.error("用户不存在") return HttpResponse(status=404, content='用户不存在') user = user[0] if user.identify == 1: logger.error("管理员角色不能加入小组") return HttpResponse(status=400, content='管理员不能加入小组') group.users.add(user) executor, action, method = get_log(request) gen_log(action, "加入分组", group.name, method, executor) return HttpResponse(status=200, content='加入成功')