123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import datetime
- import logging
- from django.http import HttpResponse
- from rest_framework.views import APIView
- from rest_framework.response import Response
- 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
- from utils.util_add_id import get_id
- from utils.util_random import Produce
- logger = logging.getLogger('django')
- class GroupListView(APIView):
- # create group
- @staticmethod
- @RoleControl
- def post(request, *args, **kwargs):
- name = request.POST.get('name')
- manager_id = request.POST.get('manager_id')
- manager = User.objects.filter(id=manager_id)
- if not manager:
- logger.error("用户不存在")
- return HttpResponse(status=500, content='用户不存在')
- manager = manager[0]
- group = Group.objects.filter(name=name, delete=False)
- if group:
- logger.error("组名已存在,请修改组名重新上传")
- return HttpResponse(status=500, content='组名已存在,请修改组名重新上传')
- create_time = update_time = datetime.datetime.now()
- group_id = get_id(Group, 'TestLaboratory_V1_Group_1')
- code = Produce.auth_code(option='num')
- group = Group.objects.create(id=group_id, code=code, manager=manager_id, name=name, create_time=create_time,
- update_time=update_time)
- executor, action, method = get_log(request)
- gen_log(action, "分组", group.name, method, executor)
- return Response({
- 'id': group.id,
- 'name': group.name,
- 'code': group.code,
- 'manager': manager.username,
- })
- # view log_list
- @staticmethod
- @RoleControl
- def get(request, *args, **kwargs):
- group_id = request.GET.get('id')
- name = request.GET.get('name')
- code = request.GET.get('code')
- manager_id = request.GET.get('manager_id')
- user_id = request.GET.get('user_id')
- groups = Group.objects.order_by('-create_time').filter(delete=False)
- if group_id:
- groups = groups.filter(id=group_id)
- if name:
- groups = groups.filter(name=name)
- if code:
- groups = groups.filter(code=code)
- if manager_id:
- groups = groups.filter(manager=manager_id)
- if user_id:
- user = User.objects.filter(id=user_id)
- if not user:
- logger.error("用户不存在")
- return HttpResponse(status=500, content='用户不存在')
- user = user[0]
- groups = groups & user.group_set.all()
- return_groups = list()
- for group in groups:
- manager = User.objects.filter(id=group.manager)[0]
- user_info = list()
- users = group.users.order_by('-create_time')
- for user in users:
- user_info.append({
- 'id': user.id,
- 'username': user.username,
- 'identity': user.identify
- })
- return_groups.append({
- 'id': group.id,
- 'name': group.name,
- 'code': group.code,
- 'manager': manager.username,
- 'members': user_info
- })
- return Response(return_groups)
|