feat: 后端-部门管理-上移下移排序功能&新增部门人数字段
This commit is contained in:
@@ -9,9 +9,8 @@ from rest_framework import serializers
|
|||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
|
||||||
from dvadmin.system.models import Dept, RoleMenuButtonPermission
|
from dvadmin.system.models import Dept, RoleMenuButtonPermission, Users
|
||||||
from dvadmin.utils.json_response import DetailResponse, SuccessResponse
|
from dvadmin.utils.json_response import DetailResponse, SuccessResponse, ErrorResponse
|
||||||
from dvadmin.utils.permission import AnonymousUserPermission
|
|
||||||
from dvadmin.utils.serializers import CustomModelSerializer
|
from dvadmin.utils.serializers import CustomModelSerializer
|
||||||
from dvadmin.utils.viewset import CustomModelViewSet
|
from dvadmin.utils.viewset import CustomModelViewSet
|
||||||
|
|
||||||
@@ -25,6 +24,11 @@ class DeptSerializer(CustomModelSerializer):
|
|||||||
has_children = serializers.SerializerMethodField()
|
has_children = serializers.SerializerMethodField()
|
||||||
hasChild = serializers.SerializerMethodField()
|
hasChild = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
dept_user_count = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
def get_dept_user_count(self, obj: Dept):
|
||||||
|
return Users.objects.filter(dept=obj).count()
|
||||||
|
|
||||||
def get_hasChild(self, instance):
|
def get_hasChild(self, instance):
|
||||||
hasChild = Dept.objects.filter(parent=instance.id)
|
hasChild = Dept.objects.filter(parent=instance.id)
|
||||||
if hasChild:
|
if hasChild:
|
||||||
@@ -56,9 +60,6 @@ class DeptImportSerializer(CustomModelSerializer):
|
|||||||
read_only_fields = ["id"]
|
read_only_fields = ["id"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class DeptCreateUpdateSerializer(CustomModelSerializer):
|
class DeptCreateUpdateSerializer(CustomModelSerializer):
|
||||||
"""
|
"""
|
||||||
部门管理 创建/更新时的列化器
|
部门管理 创建/更新时的列化器
|
||||||
@@ -149,9 +150,40 @@ class DeptViewSet(CustomModelViewSet):
|
|||||||
queryset = Dept.objects.filter(id__in=dept_list).values('id', 'name', 'parent')
|
queryset = Dept.objects.filter(id__in=dept_list).values('id', 'name', 'parent')
|
||||||
return DetailResponse(data=queryset, msg="获取成功")
|
return DetailResponse(data=queryset, msg="获取成功")
|
||||||
|
|
||||||
|
|
||||||
@action(methods=["GET"], detail=False, permission_classes=[IsAuthenticated], extra_filter_class=[])
|
@action(methods=["GET"], detail=False, permission_classes=[IsAuthenticated], extra_filter_class=[])
|
||||||
def all_dept(self, request, *args, **kwargs):
|
def all_dept(self, request, *args, **kwargs):
|
||||||
queryset = self.filter_queryset(self.get_queryset())
|
queryset = self.filter_queryset(self.get_queryset())
|
||||||
data = queryset.filter(status=True).order_by('sort').values('name', 'id', 'parent')
|
data = queryset.filter(status=True).order_by('sort').values('name', 'id', 'parent')
|
||||||
return DetailResponse(data=data, msg="获取成功")
|
return DetailResponse(data=data, msg="获取成功")
|
||||||
|
|
||||||
|
@action(methods=['POST'], detail=False, permission_classes=[])
|
||||||
|
def move_up(self, request):
|
||||||
|
"""部门上移"""
|
||||||
|
dept_id = request.data.get('dept_id')
|
||||||
|
try:
|
||||||
|
dept = Dept.objects.get(id=dept_id)
|
||||||
|
except Dept.DoesNotExist:
|
||||||
|
return ErrorResponse(msg="部门不存在")
|
||||||
|
previous_menu = Dept.objects.filter(sort__lt=dept.sort, parent=dept.parent).order_by('-sort').first()
|
||||||
|
if previous_menu:
|
||||||
|
previous_menu.sort, dept.sort = dept.sort, previous_menu.sort
|
||||||
|
previous_menu.save()
|
||||||
|
dept.save()
|
||||||
|
|
||||||
|
return SuccessResponse(data=[], msg="上移成功")
|
||||||
|
|
||||||
|
@action(methods=['POST'], detail=False, permission_classes=[])
|
||||||
|
def move_down(self, request):
|
||||||
|
"""部门下移"""
|
||||||
|
dept_id = request.data['dept_id']
|
||||||
|
try:
|
||||||
|
dept = Dept.objects.get(id=dept_id)
|
||||||
|
except Dept.DoesNotExist:
|
||||||
|
return ErrorResponse(msg="部门不存在")
|
||||||
|
next_menu = Dept.objects.filter(sort__gt=dept.sort, parent=dept.parent).order_by('sort').first()
|
||||||
|
if next_menu:
|
||||||
|
next_menu.sort, dept.sort = dept.sort, next_menu.sort
|
||||||
|
next_menu.save()
|
||||||
|
dept.save()
|
||||||
|
|
||||||
|
return SuccessResponse(data=[], msg="下移成功")
|
||||||
|
|||||||
Reference in New Issue
Block a user