功能变化: 导入时,同步启用更新

pull/84/head
猿小天 2022-12-29 13:07:47 +08:00
parent f6e8fb3d09
commit 43e21c9c64
4 changed files with 92 additions and 40 deletions

View File

@ -22,6 +22,13 @@ class DeptSerializer(CustomModelSerializer):
parent_name = serializers.CharField(read_only=True, source='parent.name')
status_label = serializers.SerializerMethodField()
has_children = serializers.SerializerMethodField()
hasChild = serializers.SerializerMethodField()
def get_hasChild(self, instance):
hasChild = Dept.objects.filter(parent=instance.id)
if hasChild:
return True
return False
def get_status_label(self, obj: Dept):
if obj.status:
@ -133,31 +140,19 @@ class DeptViewSet(CustomModelViewSet):
def list(self, request, *args, **kwargs):
# 如果懒加载,则只返回父级
queryset = self.filter_queryset(self.get_queryset())
lazy = self.request.query_params.get('lazy')
parent = self.request.query_params.get('parent')
if lazy:
# 如果懒加载模式,返回全部
if not parent:
role_list = request.user.role.filter(status=1).values("admin", "data_range")
is_admin = False
for ele in role_list:
if 3 == ele.get("data_range") or ele.get("admin") == True:
is_admin = True
break
if self.request.user.is_superuser or is_admin:
queryset = queryset.filter(parent__isnull=True)
else:
queryset = queryset.filter(id=self.request.user.dept_id)
serializer = self.get_serializer(queryset, many=True, request=request)
return SuccessResponse(data=serializer.data, msg="获取成功")
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True, request=request)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True, request=request)
return SuccessResponse(data=serializer.data, msg="获取成功")
params = request.query_params
parent = params.get('parent', None)
if params:
if parent:
queryset = self.queryset.filter(status=True, parent=parent)
else:
queryset = self.queryset.filter(status=True)
else:
queryset = self.queryset.filter(status=True, parent__isnull=True)
queryset = self.filter_queryset(queryset)
serializer = DeptSerializer(queryset, many=True, request=request)
data = serializer.data
return SuccessResponse(data=data)
def dept_lazy_tree(self, request, *args, **kwargs):
parent = self.request.query_params.get('parent')
@ -170,9 +165,9 @@ class DeptViewSet(CustomModelViewSet):
data = queryset.filter(status=True).order_by('sort').values('name', 'id', 'parent')
return DetailResponse(data=data, msg="获取成功")
@action(methods=["GET"], detail=False, permission_classes=[AnonymousUserPermission])
def all_dept(self, request, *args, **kwargs):
self.extra_filter_backends = []
queryset = self.filter_queryset(self.get_queryset())
data = queryset.filter(status=True).order_by('sort').values('name', 'id', 'parent')
return DetailResponse(data=data, msg="获取成功")

View File

@ -8,12 +8,13 @@
"""
from rest_framework import serializers
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from dvadmin.system.models import Role, Menu
from dvadmin.system.views.dept import DeptSerializer
from dvadmin.system.views.menu import MenuSerializer
from dvadmin.system.views.menu_button import MenuButtonSerializer
from dvadmin.utils.json_response import SuccessResponse
from dvadmin.utils.json_response import SuccessResponse, DetailResponse
from dvadmin.utils.serializers import CustomModelSerializer
from dvadmin.utils.validator import CustomUniqueValidator
from dvadmin.utils.viewset import CustomModelViewSet
@ -96,12 +97,58 @@ class RoleViewSet(CustomModelViewSet):
serializer_class = RoleSerializer
create_serializer_class = RoleCreateUpdateSerializer
update_serializer_class = RoleCreateUpdateSerializer
search_fields = ['name','key']
@action(methods=['GET'], detail=True, permission_classes=[])
def roleId_get_menu(self, request, *args, **kwargs):
@action(methods=['GET'], detail=True, permission_classes=[IsAuthenticated])
def roleId_get_menu(self, request,pk):
"""通过角色id获取该角色用于的菜单"""
# instance = self.get_object()
# queryset = instance.menu.all()
queryset = Menu.objects.filter(status=1).all()
instance = Role.objects.filter(id=pk).first()
queryset = instance.menu.all()
# queryset = Menu.objects.filter(status=1).all()
queryset = self.filter_queryset(queryset)
serializer = MenuPermissonSerializer(queryset, many=True)
return SuccessResponse(data=serializer.data)
@action(methods=['GET'], detail=False, permission_classes=[IsAuthenticated])
def data_scope(self,request):
is_superuser = request.user.is_superuser
is_admin = Role.objects.filter(users__id=request.user.id,admin=True)
if is_superuser or is_admin:
data = [
{
"value": 0,
"label": '仅本人数据权限'
},
{
"value": 1,
"label": '本部门及以下数据权限'
},
{
"value": 2,
"label": '本部门数据权限'
},
{
"value": 3,
"label": '全部数据权限'
},
{
"value": 4,
"label": '自定义数据权限'
}
]
else:
data = [
{
"value": 0,
"label": '仅本人数据权限'
},
{
"value": 1,
"label": '本部门及以下数据权限'
},
{
"value": 2,
"label": '本部门数据权限'
}
]
return DetailResponse(data=data)

View File

@ -40,9 +40,10 @@ def CustomExceptionHandler(ex, context):
elif isinstance(ex, DRFAPIException):
set_rollback()
msg = ex.detail
for k, v in msg.items():
for i in v:
msg = "%s:%s" % (k, i)
if isinstance(msg,dict):
for k, v in msg.items():
for i in v:
msg = "%s:%s" % (k, i)
elif isinstance(ex, ProtectedError):
set_rollback()
msg = "删除失败:该条数据与其他数据有相关绑定"

View File

@ -57,6 +57,11 @@ class ImportSerializerMixin:
length += 2.1 if ord(char) > 256 else 1
return round(length, 1) if length <= self.export_column_width else self.export_column_width
@action(methods=['get'],detail=False)
def update_field(self,request:Request):
data = [{"label":value,"value":key} for key,value in self.import_field_dict.items()]
return DetailResponse(data=data)
@action(methods=['get','post'],detail=False)
@transaction.atomic # Django 事务,防止出错
def import_data(self, request: Request, *args, **kwargs):
@ -148,12 +153,15 @@ class ImportSerializerMixin:
unique_list = [
ele.name for ele in queryset.model._meta.get_fields() if hasattr(ele, "unique") and ele.unique == True
]
updateField = request.data.get("updateField")
for ele in data:
# 获取 unique 字段
if queryset.model._meta.unique_together: # 判断是否存在联合主键
filter_dic = {i: ele.get(i) for i in list(queryset.model._meta.unique_together[0])}
else:
filter_dic = {i: ele.get(i) for i in list(set(unique_list)) if ele.get(i) is not None}
# # 获取 unique 字段
# if queryset.model._meta.unique_together: # 判断是否存在联合主键
# filter_dic = {i: ele.get(i) for i in list(queryset.model._meta.unique_together[0])}
# else:
# filter_dic = {i: ele.get(i) for i in list(set(unique_list)) if ele.get(i) is not None}
filter_dic = {updateField:ele.get(updateField)}
print(162,filter_dic)
instance = filter_dic and queryset.filter(**filter_dic).first()
if instance and not updateSupport:
continue
@ -207,6 +215,7 @@ class ExportSerializerMixin:
length += 2.1 if ord(char) > 256 else 1
return round(length, 1) if length <= self.export_column_width else self.export_column_width
@action(methods=['get'],detail=False)
def export_data(self, request: Request, *args, **kwargs):
"""
导出功能