import logging from rest_framework.views import APIView from rest_framework.response import Response from apps.group.models import Group from apps.user.middleware.rolecontrol import RoleControl from apps.user.models import User logger = logging.getLogger('django') class GroupUserView(APIView): # create group @staticmethod @RoleControl def get(request, *args, **kwargs): groups = Group.objects.filter(delete=False) users = set(User.objects.filter(delete=False)) users_info = [] for group in groups: user_group = {'groupName': group.name, 'userList': list() } users = users - set(group.users.all()) for user in group.users.all(): user_group['userList'].append({ 'userid': user.id, 'username': user.username, 'identify': user.identify, 'is_login': user.is_login }) users_info.append(user_group) user_group = {'groupName': '默认分组', 'userList': list() } for user in users: user_group['userList'].append({ 'userid': user.id, 'username': user.username, 'identify': user.identify, 'is_login': user.is_login }) users_info.append(user_group) return Response(users_info)