groupuserview.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import logging
  2. from rest_framework.views import APIView
  3. from rest_framework.response import Response
  4. from apps.group.models import Group
  5. from apps.user.middleware.rolecontrol import RoleControl
  6. from apps.user.models import User
  7. logger = logging.getLogger('django')
  8. class GroupUserView(APIView):
  9. # create group
  10. @staticmethod
  11. @RoleControl
  12. def get(request, *args, **kwargs):
  13. groups = Group.objects.filter(delete=False)
  14. users = set(User.objects.filter(delete=False))
  15. users_info = []
  16. for group in groups:
  17. user_group = {'groupName': group.name,
  18. 'userList': list()
  19. }
  20. users = users - set(group.users.all())
  21. for user in group.users.all():
  22. user_group['userList'].append({
  23. 'userid': user.id,
  24. 'username': user.username,
  25. 'identify': user.identify,
  26. 'is_login': user.is_login
  27. })
  28. users_info.append(user_group)
  29. user_group = {'groupName': '默认分组',
  30. 'userList': list()
  31. }
  32. for user in users:
  33. user_group['userList'].append({
  34. 'userid': user.id,
  35. 'username': user.username,
  36. 'identify': user.identify,
  37. 'is_login': user.is_login
  38. })
  39. users_info.append(user_group)
  40. return Response(users_info)