groupview.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from django.http import HttpResponse
  2. from rest_framework.response import Response
  3. from rest_framework.views import APIView
  4. from apps.group.models import Group
  5. from apps.log.models import get_log, gen_log
  6. from apps.user.middleware.rolecontrol import RoleControl
  7. from apps.user.models import User
  8. import logging
  9. logger = logging.getLogger('django')
  10. class GroupView(APIView):
  11. authentication_classes = []
  12. # 删除分组
  13. @staticmethod
  14. @RoleControl
  15. def delete(request, group_id, *args, **kwargs):
  16. group = Group.objects.filter(id=group_id, delete=False)
  17. if not group:
  18. logger.error("分组已删除或不存在")
  19. group = group[0]
  20. users = group.users.all()
  21. for user in users:
  22. group.users.remove(user)
  23. group.delete = True
  24. group.save()
  25. executor, action, method = get_log(request)
  26. gen_log(action, "分组", group.name, method, executor)
  27. return HttpResponse("删除完成")
  28. class GroupEdit(APIView):
  29. authentication_classes = []
  30. # 编辑分组
  31. @staticmethod
  32. @RoleControl
  33. def post(request, group_id, *args, **kwargs):
  34. name = request.POST.get('group_name')
  35. if not name:
  36. logger.error("组名不能位空")
  37. return HttpResponse(status=500, content='组名不能位空')
  38. group = Group.objects.filter(id=group_id, delete=False)
  39. if not group:
  40. logger.error("id为" + group_id + "的分组不存在")
  41. return HttpResponse(status=404, content="id为" + group_id + "的分组不存在")
  42. group = group[0]
  43. group.name = name
  44. group.save()
  45. executor, action, method = get_log(request)
  46. gen_log(action, "分组", group.name, method, executor)
  47. return HttpResponse(status=200, content='修改已保存')