Skip to content

Plug -in base class#

The most basic base class of plug -in arkid.core.extension.Extension

The core method of supporting hot insertion is start() and stop() Will callload()andunload()method,These two methods are in each plug -in subclass,Use the operation of installation and uninstallation

  • load() Abstract method,It is required that each plug -in should be realized。
  • unload() Can also be reloaded。Will all registered APIs registered in the process of load (),event,Wait for destruction。

therefore,Try to avoid directly using the kernel API in the plug -in,And to be encapsulated in the plug -in register Starting method。transfer register When the type of method,The destruction of related operations will be executed in STOP,No need to do it in Unload。

Some changes to the page content,For example, Actions related modifications,The destruction displayed in Unload。

Hint

In fact,Callstart()The presence will be carried out migrate,yes!Plug -in migrations Still effective。

arkid.core.extension.Extension (ABC) #

Parameters:

Name Type Description Default
name str,

插件名字,package中点“.”替换为下划线"_"

None

ext_dir property writable #

插件完整路径,用.分隔

full_name property readonly #

插件完整路径,用/分隔

model property readonly #

插件对应数据库model

__init__(self, package=None, version=None, name=None, logo=None, description=None, labels=None, homepage=None, author=None) special #

summary

Parameters:

Name Type Description Default
package str

插件包名,唯一标识

None
version str

版本号

None
name str

名称

None
logo str

插件的图标

None
description str

描述

None
labels List[str]

标签

None
homepage str

主页,URL

None
author str

作者

None

create_composite_config_schema(schema_cls_name, exclude=[], **field_definitions) classmethod #

创造复合类型 运行时配置 的Schema

Parameters:

Name Type Description Default
schema_cls_name str

复合类型运行时配置的Schema的名字

required
exclude list

去掉的字段列表. Defaults to [].

[]

Returns:

Type Description
Schema

创建好的Schema

create_config_schema(name, **field_definitions) classmethod #

创建并返回插件 运行时配置 的Schema

Parameters:

Name Type Description Default
name str

需要创建的 Schema Class 的名字

required
field_definitions Any

任意数量的field,格式为: field_name=(field_type, Field(...))

{}

create_profile_schema(name, **field_definitions) classmethod #

创建并返回插件配置的Schema

Parameters:

Name Type Description Default
name str

需要创建的 Schema Class 的名字

required
field_definitions Any

任意数量的field,格式为: field_name=(field_type, Field(...))

{}

create_settings_schema(name, **field_definitions) classmethod #

创建并返回插件 租户配置(settings) 的Schema

Parameters:

Name Type Description Default
name str

需要创建的 Schema Class 的名字

required
field_definitions Any

任意数量的field,格式为: field_name=(field_type, Field(...))

{}

create_tenant_config(self, tenant, config, name, type) #

创建运行时配置

Parameters:

Name Type Description Default
tenant Tenant

租户

required
config dict

config

required
name str

运行时配置名字

required
type str

配置类型

required

Returns:

Type Description
TenantExtensionConfig

创建的对象

delete_tenant_config(self, id) #

删除运行时配置

Parameters:

Name Type Description Default
id str

config_id

required

Returns:

Type Description
TenantExtensionConfig

删除的对象

dispatch_event(self, event) #

抛出事件

Parameters:

Name Type Description Default
event Event

事件实例

required

Returns:

Type Description
(tuple[Function, Result])

事件处理的返回值

error(self, enum=None, **kwargs) #

API接口错误dict

Parameters:

Name Type Description Default
enum Enum

错误的枚举类,如果为None,标识成功返回. Defaults to None.

None

Returns:

Type Description
dict

生成的错误dict

get_config_by_id(self, id) #

通过config_id来获取config

Parameters:

Name Type Description Default
id UUID

config_id

required

Returns:

Type Description
TenantExtensionConfig

config

get_settings(self, tenant) #

获取租户配置

Parameters:

Name Type Description Default
tenant Tenant

租户

required

Returns:

Type Description
TenantExtension

租户配置

get_tenant_configs(self, tenant) #

获取当前租户下所有的运行时配置

Parameters:

Name Type Description Default
tenant Tenant

租户

required

Returns:

Type Description
List[TenantExtensionConfig]

tenant下所有的运行时配置

listen_event(self, tag, func) #

侦听事件

Parameters:

Name Type Description Default
tag str

事件的tag

required
func Function

回调函数, event, **kwargs为必有参数。其中只有当前插件的package在event.packages中时,该插件才响应该事件。

required

load(self) #

抽象方法,插件加载的入口方法

register_api(self, path, method, view_func, *, tenant_path=False, auth=ninja.constants.NOT_SET_TYPE, response=ninja.constants.NOT_SET_TYPE, operation_id=None, summary=None, description=None, tags=None, deprecated=None, by_alias=False, exclude_unset=False, exclude_defaults=False, exclude_none=False, url_name=None, include_in_schema=True) #

Django-ninja的方式注册自定义API

Parameters:

Name Type Description Default
path str

请求路径

required
method str

请求方法,值为:GET,POST,DELETE,PUT等

required
view_func Callable

api方法

required
tenant_path bool

是否需要tenant开头,为Ture时,path前自动加上'/tenant/{tenant_id}'的结构. Defaults to False.

False
auth Any

认证方法. Defaults to NOT_SET.

ninja.constants.NOT_SET_TYPE
response Any

response schema. Defaults to NOT_SET.

ninja.constants.NOT_SET_TYPE
operation_id Optional[str]

Defaults to None.

None
summary Optional[str]

Defaults to None.

None
description Optional[str]

Defaults to None.

None
tags Optional[List[str]]

Defaults to None.

None
deprecated Optional[bool]

Defaults to None.

None
by_alias bool

Defaults to False.

False
exclude_unset bool

Defaults to False.

False
exclude_defaults bool

Defaults to False.

False
exclude_none bool

Defaults to False.

False
url_name Optional[str]

Defaults to None.

None
include_in_schema bool

Defaults to True.

True

Returns:

Type Description
str

真实的地址path

register_composite_config_schema(self, schema, composite_value, exclude=[], package=None) #

注册复合类型 运行时配置 的Schema

Parameters:

Name Type Description Default
schema class

Schema类

required
composite_value str

复合类型

required
exclude list

从schema的字段中删掉的字段列表. Defaults to [].

[]
package str

自定义package名字,不传就使用self.package, 正常情况不用设置.

None

register_config_schema(self, schema, schema_tag=None) #

注册插件的 运行时配置 config schema

Parameters:

Name Type Description Default
schema class

schema的类

required
schema_tag str

shema的标识, 默认为self.package

None

register_event(self, tag, name, data_schema=None, description='') #

注册事件

Parameters:

Name Type Description Default
tag str

事件标识

required
name str

事件名字

required
data_schema schema class

event.data的schema. Defaults to None.

None
description str

事件描述. Defaults to ''.

''

Returns:

Type Description
str

真实事件标识tag,为self.package +'.'+ tag

register_event_type(self, event_type) #

注册事件类型

Parameters:

Name Type Description Default
event_type EventType

事件类型对象

required

Returns:

Type Description
EventType

tag = package+'.'+tag

register_extend_api(self, *api_schema_cls, **field_definitions) #

注册扩展内核API

Parameters:

Name Type Description Default
api_schema_cls class

API Schema Class

()
field_definitions name=tuple(Type,Field

需要增加的字段,example:name=(str, Field(title='名字'))

{}

register_extend_field(self, model_cls, model_field, alias=None) #

注册扩展数据库字段,对原本数据库字段进行扩展

Parameters:

Name Type Description Default
model_cls Union[ core_expand.TenantExpandAbstract, core_expand.UserExpandAbstract, core_expand.UserGroupExpandAbstract, core_expand.AppExpandAbstract, core_expand.AppGroupExpandAbstract, ]

扩展定义的model

required
model_field str

扩展的字段

required
alias str

扩展字段在原model中的别名. None意味着就使用model_field作为其在原model中的别名

None

Exceptions:

Type Description
Exception

非法的扩展字段类对应的父类

register_front_pages(self, page) #

注册前端页面

Parameters:

Name Type Description Default
page core_pages.FrontPage

前端页面

required

register_front_routers(self, router, primary=None) #

注册前端路由

Parameters:

Name Type Description Default
router core_routers.FrontRouter

前端路由实例

required
primary core_routers.FrontRouter

一级路由名字,由 core_routers 文件提供定义. Defaults to None.

None

register_profile_schema(self, schema, schema_tag=None) #

注册插件配置 profile schema

Parameters:

Name Type Description Default
schema class

schema的类

required
schema_tag str

shema的标识, 默认为self.package

None

register_routers(self, urls_ext, tenant_urls=False) #

注册路由

Parameters:

Name Type Description Default
urls_ext List[partial]

需要注册的路由

required
tenant_urls bool

是否要添加 tenant/{tenant_id}/ 前缀. Defaults to False.

False

register_settings_schema(self, schema, schema_tag=None) #

注册插件的 租户配置 settings schema

Parameters:

Name Type Description Default
schema class

schema的类

required
schema_tag str

shema的标识, 默认为self.package

None

stop(self) #

插件卸载

success(self, data=None, **kwargs) #

API接口成功dict

Parameters:

Name Type Description Default
data dict

成功时需要返回的数据. Defaults to None.

None

Returns:

Type Description
dict

生成的成功dict

unregister_extend_api(self, *api_schema_cls, *, field_keys=[]) #

移除扩展内核API

Parameters:

Name Type Description Default
api_schema_cls class

API Schema Class

()
field_keys list

需要移除的字段名称,example:field_keys=['nickname','mobile'])

[]

update_or_create_settings(self, tenant, settings, is_active, use_platform_config) #

更新或创建租户配置

Parameters:

Name Type Description Default
tenant Tenant

租户

required
settings dict

租户配置

required
is_active bool

是否启用

required
use_platform_config bool

是否使用平台配置

required

Returns:

Type Description
TenantExtension

更新或创建的对象

update_tenant_config(self, id, config, name, type) #

更新运行时配置

Parameters:

Name Type Description Default
id str

config_id

required
config dict

config

required
name str

运行时配置名字

required
type str

配置类型

required

Returns:

Type Description
bool

更新成功True,没有找到该配置返回False

arkid.core.extension.create_extension_schema(name, file_path, fields=None, base_schema=<class 'ninja.schema.Schema'>, exclude=[]) #

提供给插件用来创建Schema的方法

注意

插件必须使用此方法来定义Schema,避免与其它Schema的命名冲突

Parameters:

Name Type Description Default
name str

Schema的类名

required
file_path str

指插件__init__.py文件所在的路径, 用来通过插件的config.toml文件获取package,从而避免schema的命名冲突

required
fields Optional[List[Tuple[str, Any, Any]]]

Schema的字段定义

None
base_schema Type[Schema]

Schema的基类. 默认为: ninja.Schema

<class 'ninja.schema.Schema'>

Returns:

Type Description
ninja.Schema

创建的Schema类

arkid.core.extension.create_extension_schema_by_package(name, package='', fields=None, base_schema=<class 'ninja.schema.Schema'>, exclude=[]) #

提供给插件用来创建Schema的方法

注意

插件必须使用此方法来定义Schema,避免与其它Schema的命名冲突

Parameters:

Name Type Description Default
name str

Schema的类名

required
package str

如果是插件调用的该方法,一定要将插件的package传过来,以避免命名冲突

''
fields Optional[List[Tuple[str, Any, Any]]]

Schema的字段定义

None
base_schema Type[Schema]

Schema的基类. 默认为: ninja.Schema

<class 'ninja.schema.Schema'>

Returns:

Type Description
ninja.Schema

创建的Schema类

arkid.core.extension.create_extension_schema_from_django_model(model, *, name='', depth=0, fields=None, exclude=None, custom_fields=None, base_class=<class 'ninja.schema.Schema'>) #

提供给插件通过Django.Model创建Schema的方法

注意

插件必须使用此方法来定义Schema,避免与其它Schema的命名冲突

Parameters:

Name Type Description Default
model Type[Model]

基于的 Django Model

required
name str

Schema的类名.

''
depth int

遍历Django Model的深度.

0
fields Optional[List[str]]

从Django Model中获取的字段名, 如果是所有的就设为 __all__ .

None
exclude Optional[List[str]]

从Django Model中排除的字段名.

None
custom_fields Optional[List[Tuple[str, Any, Any]]]

添加的自定义字段.

None
base_class Type[Schema]

Schema的基类.

<class 'ninja.schema.Schema'>

Returns:

Type Description
ninjia.Schema

新创建的Schema类

评论