You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from django.utils.decorators import method_decorator
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import ModelViewSet
from ERP_5.utils.base_views import MultipleDestroyMixin
from ERP_5.utils.paginations import GlobalPagination
from erp_system.models import DeptModel
from erp_system.serializers.dept_serializer import DeptSerializer
# 定义好了一个swagger的参数
pid_param = openapi.Parameter(name='pid', in_=openapi.IN_QUERY, description='父部门的ID', required=False,
type=openapi.TYPE_INTEGER)
@method_decorator(name='list', decorator=swagger_auto_schema(
manual_parameters=[pid_param],
operation_description="查询部门的树形结构数据, pid=0或者不传pid表示查询顶级机构的树。")
)
class DeptViewSet(ModelViewSet, MultipleDestroyMixin):
"""
create:
部门机构--新增
部门机构新增, status: 201(成功), return: 新增部门机构信息
destroy:
部门机构--删除
部门机构删除, status: 204(成功), return: None
multiple_delete:
部门机构--批量删除,必传参数ids=[1,2,3,4...]
部门机构批量删除, status: 204(成功), return: None
update:
部门机构--修改,不能修改部门机构,只是修改部门机构名字。
部门机构修改, status: 200(成功), return: 修改后的部门机构信息
partial_update:
部门机构--局部修改(部门机构批量授权),只能真的某一个部门机构一次性授权,之前的的授权会被覆盖
部门机构批量授权, status: 200(成功), return: 修改后的部门机构信息
list:
部门机构--获取列表, 如果传参pid=0或者pid不传则查询所有顶级部门列表 如果传参pid=具体的值,则查询某个父部门下的所有子部门
部门机构列表信息, status: 200(成功), return: 部门机构信息列表
retrieve:
查询某一个部门机构
查询指定ID的部门机构, status: 200(成功), return: 用户部门机构
"""
def get_queryset(self):
if self.action == 'list':
q = self.request.query_params.get('pid', None)
if q: # 参数中有pid
pid = int(q)
if pid == 0:
return DeptModel.objects.filter(parent__isnull=True).all()
else:
return DeptModel.objects.filter(parent__id=pid).all()
else:
return DeptModel.objects.filter(parent__isnull=True).all()
else:
return DeptModel.objects.all()
queryset = DeptModel.objects.all()
serializer_class = DeptSerializer
pagination_class = GlobalPagination
# permission_classes = [IsAuthenticated]
# @swagger_auto_schema(method='GET', manual_parameters=[pid_param])
# @action(methods=['GET'], detail=False)
# def find(self, request, *args, **kwargs):
# return super(DeptViewSet, self).list(request, *args, **kwargs)