grouplistview.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import datetime
  2. import logging
  3. from django.http import HttpResponse
  4. from rest_framework.views import APIView
  5. from rest_framework.response import Response
  6. from apps.group.models import Group
  7. from apps.log.models import get_log, gen_log
  8. from apps.user.middleware.rolecontrol import RoleControl
  9. from apps.user.models import User
  10. from utils.util_add_id import get_id
  11. from utils.util_random import Produce
  12. logger = logging.getLogger('django')
  13. class GroupListView(APIView):
  14. # create group
  15. @staticmethod
  16. @RoleControl
  17. def post(request, *args, **kwargs):
  18. name = request.POST.get('name')
  19. manager_id = request.POST.get('manager_id')
  20. manager = User.objects.filter(id=manager_id)
  21. if not manager:
  22. logger.error("用户不存在")
  23. return HttpResponse(status=500, content='用户不存在')
  24. manager = manager[0]
  25. group = Group.objects.filter(name=name, delete=False)
  26. if group:
  27. logger.error("组名已存在,请修改组名重新上传")
  28. return HttpResponse(status=500, content='组名已存在,请修改组名重新上传')
  29. create_time = update_time = datetime.datetime.now()
  30. group_id = get_id(Group, 'TestLaboratory_V1_Group_1')
  31. code = Produce.auth_code(option='num')
  32. group = Group.objects.create(id=group_id, code=code, manager=manager_id, name=name, create_time=create_time,
  33. update_time=update_time)
  34. executor, action, method = get_log(request)
  35. gen_log(action, "分组", group.name, method, executor)
  36. return Response({
  37. 'id': group.id,
  38. 'name': group.name,
  39. 'code': group.code,
  40. 'manager': manager.username,
  41. })
  42. # view log_list
  43. @staticmethod
  44. @RoleControl
  45. def get(request, *args, **kwargs):
  46. group_id = request.GET.get('id')
  47. name = request.GET.get('name')
  48. code = request.GET.get('code')
  49. manager_id = request.GET.get('manager_id')
  50. user_id = request.GET.get('user_id')
  51. groups = Group.objects.order_by('-create_time').filter(delete=False)
  52. if group_id:
  53. groups = groups.filter(id=group_id)
  54. if name:
  55. groups = groups.filter(name=name)
  56. if code:
  57. groups = groups.filter(code=code)
  58. if manager_id:
  59. groups = groups.filter(manager=manager_id)
  60. if user_id:
  61. user = User.objects.filter(id=user_id)
  62. if not user:
  63. logger.error("用户不存在")
  64. return HttpResponse(status=500, content='用户不存在')
  65. user = user[0]
  66. groups = groups & user.group_set.all()
  67. return_groups = list()
  68. for group in groups:
  69. manager = User.objects.filter(id=group.manager)[0]
  70. user_info = list()
  71. users = group.users.order_by('-create_time')
  72. for user in users:
  73. user_info.append({
  74. 'id': user.id,
  75. 'username': user.username,
  76. 'identity': user.identify
  77. })
  78. return_groups.append({
  79. 'id': group.id,
  80. 'name': group.name,
  81. 'code': group.code,
  82. 'manager': manager.username,
  83. 'members': user_info
  84. })
  85. return Response(return_groups)