Ejemplo n.º 1
0
def import_field_config(field_name: str, field_detail: str,
                        config: dict) -> (str, str, dict, dict):
    """
    import form local meta config or other meta config or model class.
    return < 字段来源, 字段路径, 字段具体配置信息, 完整meta config >.
    """
    _field_detail = {}
    if isinstance(field_detail, str):
        path = field_detail
    if isinstance(field_detail, dict):
        _field_detail = copy.deepcopy(field_detail)
        path = _field_detail.pop(FIELD_SRC, field_name)
    # 内部扩展的属性,不属于meta和model
    if path.startswith('.'):
        return SOURCE_INTERNAL, path, {}, config
    # _path => ['user', 'username'] or ['username']
    _path = path.split('.')
    if len(_path) == 1:
        # {"meta": {"user": {"title":"","type":{}}}}
        #   _field_name => user
        #   field_config => {"title":"","type":{}}
        _field_name = _path[0]
        field_config = config[CONFIG_META][_field_name]
        return SOURCE_META, _field_name, field_config, config
    else:
        # {"user": {"model": "models.user.User"}}
        #   name => user
        #   detail => {"model": "models.user.User"}
        for name, detail in config[CONFIG_MODULE].items():
            if name == _path[0]:
                _field_name = _path[1]
                if SOURCE_MODEL in detail.keys():
                    cls = get_class_from_path(detail[SOURCE_MODEL])
                    for item in cls._meta.fields:
                        if _field_name == item.name:
                            field_type = MODEL_FIELD_MAPPING[item.__class__]
                            field_config = {
                                FIELD_TITLE: item.verbose_name,
                                FIELD_TYPE: {
                                    REVERSE_META_FIELD_MAPPING[field_type]: {}
                                },
                                **_field_detail
                            }
                            return SOURCE_MODEL, detail[
                                SOURCE_MODEL], field_config, config
                if SOURCE_META in detail.keys():
                    file_path = os.getcwd()
                    for item in detail[SOURCE_META].split('.'):
                        file_path = os.path.join(file_path, item)
                    file_path = f'{file_path}.json'
                    meta_config = json_load(file_path)
                    return SOURCE_META, detail[SOURCE_META], meta_config[
                        SOURCE_META][_field_name], meta_config

    raise Exception({'error': f'Path:{path} Config Not Exists!'})
Ejemplo n.º 2
0
 def get_meta(self, name):
     """
     get meta data.
     """
     for root, _, files in os.walk(self.flow.file_dir):
         for file in files:
             if file.endswith('.json'):
                 data = json_load(os.path.join(root, file))
                 if data[CONFIG_NAME] == name:
                     return data
     return self.flow.shutdown({'error': 'Meta Not Exists!'},
                               response_status=400)
Ejemplo n.º 3
0
    def __init__(self, data=None, file=None, view_flow=None, **kwargs):
        if data:
            assert isinstance(data, dict)
        if file:
            assert os.path.isfile(file)
            data = json_load(file)

        for item in REQUIRED_FIELDS:
            if item not in data.keys():
                raise Exception(
                    f'Field:{item} must be contained in meta config file.')

        self.data = data
        self.view_flow = view_flow or self.default_view_flow
        self._cls_attrs = kwargs.get('cls_attrs', None)
Ejemplo n.º 4
0
def get_permission(permissions: list, config: dict) -> list:
    """
    get permission from api permission field.
    permissions => ["admin"]
    """
    permission_config = config.get(CONFIG_PERMISSION)
    flows = []
    for item in permissions:
        meta_permission = json_load(
            os.path.join(
                os.getcwd(),
                f'{"/".join(permission_config[item.split(".")[0]].split("."))}.json'
            ))
        flows.append(
            get_class_from_path(
                f'{meta_permission[item.split(".")[1]][ROLE_FLOW]}.main.Main'))
    return flows
Ejemplo n.º 5
0
def merge_meta(meta: dict) -> dict:
    """
    merge data for api: get meta config.
    """
    modules = meta.pop(CONFIG_MODULE)
    _meta = {meta.pop(CONFIG_NAME): meta}
    for meta_name, detail in modules.items():
        if SOURCE_META in detail.keys():
            # meta config
            file_path = os.getcwd()
            for item in detail[SOURCE_META].split('.'):
                file_path = os.path.join(file_path, item)
            file_path = f'{file_path}.json'
            slave_meta = json_load(file_path)
            module_meta = merge_meta(slave_meta)
            _meta.update(**module_meta)

        if SOURCE_MODEL in detail.keys():
            # model config
            module_model = {
                meta_name: {
                    CONFIG_TYPE: "",
                    CONFIG_META: {},
                    CONFIG_API: {}
                }
            }

            # module_model = {"name": meta_name, "type": "", "meta": {}, "api": {}}
            cls = get_class_from_path(detail[SOURCE_MODEL])
            for item in cls._meta.fields:
                field_type = MODEL_FIELD_MAPPING[item.__class__]
                field_config = {
                    FIELD_TITLE: item.verbose_name,
                    FIELD_TYPE: {
                        REVERSE_META_FIELD_MAPPING[field_type]: {}
                    }
                }
                module_model[meta_name][SOURCE_META].update(
                    **{item.name: field_config})
            _meta.update(**module_model)

    return _meta