2022-04-05 05:22:26 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
@author: 猿小天
|
|
|
|
@contact: QQ:1638245306
|
|
|
|
@Created on: 2021/6/3 003 0:30
|
|
|
|
@Remark: 字典管理
|
|
|
|
"""
|
2022-05-10 07:54:13 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
2022-04-05 05:22:26 +00:00
|
|
|
from dvadmin.system.models import Dictionary
|
|
|
|
from dvadmin.utils.json_response import SuccessResponse
|
|
|
|
from dvadmin.utils.serializers import CustomModelSerializer
|
|
|
|
from dvadmin.utils.viewset import CustomModelViewSet
|
|
|
|
|
|
|
|
|
|
|
|
class DictionarySerializer(CustomModelSerializer):
|
|
|
|
"""
|
|
|
|
字典-序列化器
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Dictionary
|
|
|
|
fields = "__all__"
|
|
|
|
read_only_fields = ["id"]
|
|
|
|
|
|
|
|
|
|
|
|
class DictionaryCreateUpdateSerializer(CustomModelSerializer):
|
|
|
|
"""
|
|
|
|
字典管理 创建/更新时的列化器
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Dictionary
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
|
|
|
|
class DictionaryTreeSerializer(CustomModelSerializer):
|
|
|
|
"""
|
|
|
|
字典表的树形序列化器
|
|
|
|
"""
|
|
|
|
children = serializers.SerializerMethodField(read_only=True)
|
|
|
|
|
|
|
|
def get_children(self, instance):
|
2022-05-12 15:56:12 +00:00
|
|
|
queryset = Dictionary.objects.filter(parent=instance.id).filter(status=1).values('label', 'value', 'type',
|
|
|
|
'color')
|
2022-04-05 05:22:26 +00:00
|
|
|
if queryset:
|
2022-05-10 07:09:40 +00:00
|
|
|
return queryset
|
2022-04-05 05:22:26 +00:00
|
|
|
else:
|
2022-05-10 07:09:40 +00:00
|
|
|
return []
|
2022-04-05 05:22:26 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Dictionary
|
2022-05-10 07:09:40 +00:00
|
|
|
fields = ['id', 'value', 'children']
|
2022-04-05 05:22:26 +00:00
|
|
|
read_only_fields = ["id"]
|
|
|
|
|
|
|
|
|
|
|
|
class DictionaryViewSet(CustomModelViewSet):
|
|
|
|
"""
|
|
|
|
字典管理接口
|
|
|
|
list:查询
|
|
|
|
create:新增
|
|
|
|
update:修改
|
|
|
|
retrieve:单例
|
|
|
|
destroy:删除
|
|
|
|
"""
|
|
|
|
queryset = Dictionary.objects.all()
|
|
|
|
serializer_class = DictionarySerializer
|
|
|
|
extra_filter_backends = []
|
|
|
|
search_fields = ['label']
|
2022-05-10 07:09:40 +00:00
|
|
|
|
2022-05-10 07:54:13 +00:00
|
|
|
|
|
|
|
class InitDictionaryViewSet(APIView):
|
|
|
|
"""
|
|
|
|
获取初始化配置
|
|
|
|
"""
|
|
|
|
authentication_classes = []
|
|
|
|
permission_classes = []
|
|
|
|
queryset = Dictionary.objects.all()
|
|
|
|
|
|
|
|
def get(self, request):
|
2022-05-10 07:09:40 +00:00
|
|
|
dictionary_key = self.request.query_params.get('dictionary_key')
|
|
|
|
if dictionary_key:
|
|
|
|
if dictionary_key == 'all':
|
|
|
|
queryset = self.queryset.filter(status=True, is_value=False)
|
|
|
|
serializer = DictionaryTreeSerializer(queryset, many=True, request=request)
|
|
|
|
data = serializer.data
|
|
|
|
else:
|
2022-05-12 15:56:12 +00:00
|
|
|
data = self.queryset.filter(parent__value=dictionary_key, status=True).values('label', 'value', 'type',
|
|
|
|
'color')
|
2022-05-10 07:09:40 +00:00
|
|
|
return SuccessResponse(data=data, msg="获取成功")
|
2022-05-10 07:54:13 +00:00
|
|
|
return SuccessResponse(data=[], msg="获取成功")
|