reactor: 增加DynamicMappingSerializer类,实现Serializer中的字段可以动态改变的功能 (#5379)

* reactor: 增加DynamicMappingSerializer类,实现Serializer中的字段可以动态改变的功能

* reactor: 增加DynamicMappingSerializer类,实现Serializer中的字段可以动态改变的功能 (2)

* reactor: 增加DynamicMappingSerializer类,实现Serializer中的字段可以动态改变的功能 (3)

Co-authored-by: Bai <bugatti_it@163.com>
This commit is contained in:
fit2bot
2021-01-05 23:39:38 +08:00
committed by GitHub
parent 3188692691
commit 17a01a12db
45 changed files with 164 additions and 1000 deletions

View File

@@ -1,14 +1,10 @@
import copy
from common.drf.fields import IgnoreSensitiveInfoReadOnlyJSONField
from tickets import const
from . import apply_asset, apply_application, login_confirm
from .ticket_type import apply_asset, apply_application, login_confirm
__all__ = [
'get_meta_field_dynamic_mapping_rules',
'get_meta_field_mapping_rule_by_view',
'meta_field_dynamic_mapping_serializers',
]
#
# ticket type
# -----------
@@ -17,7 +13,6 @@ type_apply_asset = const.TicketTypeChoices.apply_asset.value
type_apply_application = const.TicketTypeChoices.apply_application.value
type_login_confirm = const.TicketTypeChoices.login_confirm.value
#
# ticket action
# -------------
@@ -29,13 +24,11 @@ action_reject = const.TicketActionChoices.reject.value
action_close = const.TicketActionChoices.close.value
#
# defines the dynamic mapping rules for the DynamicMappingField `meta`
# --------------------------------------------------------------------
# defines `meta` field dynamic mapping serializers
# ------------------------------------------------
__META_FIELD_DYNAMIC_MAPPING_RULES = {
'default': IgnoreSensitiveInfoReadOnlyJSONField,
meta_field_dynamic_mapping_serializers = {
'type': {
type_apply_asset: {
action_open: apply_asset.ApplySerializer,
@@ -52,22 +45,3 @@ __META_FIELD_DYNAMIC_MAPPING_RULES = {
}
# Note:
# The dynamic mapping rules of `meta` field is obtained
# through call method `get_meta_field_dynamic_mapping_rules`
def get_meta_field_dynamic_mapping_rules():
return copy.deepcopy(__META_FIELD_DYNAMIC_MAPPING_RULES)
#
# get `meta dynamic field` mapping rule by `view object`
# ------------------------------------------------------
def get_meta_field_mapping_rule_by_view(view):
query_type = view.request.query_params.get('type')
query_action = view.request.query_params.get('action')
action = query_action if query_action else view.action
mapping_rule = ['type', query_type, action]
return mapping_rule