-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #775 from longguikeji/loopbing_2.5
更新了app创建逻辑
- Loading branch information
Showing
79 changed files
with
5,730 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from . import loginpage, auth, extension_config, register | ||
from . import loginpage, auth, extension_config, register, app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from ninja import Schema | ||
from pydantic import Field | ||
from arkid.core.api import api | ||
from arkid.core.models import App | ||
from django.db import transaction | ||
from arkid.core.translation import gettext_default as _ | ||
from arkid.extension.models import TenantExtensionConfig | ||
from arkid.core.event import Event, register_event, dispatch_event | ||
from arkid.core.extension.app_protocol import create_app_protocol_extension_config_schema | ||
from arkid.core.event import CREATE_APP, UPDATE_APP, DELETE_APP | ||
|
||
register_event(CREATE_APP, _('create app','创建应用')) | ||
register_event(UPDATE_APP, _('update app','修改应用')) | ||
register_event(DELETE_APP, _('delete app','删除应用')) | ||
|
||
class AppConfigSchemaIn(Schema): | ||
pass | ||
|
||
create_app_protocol_extension_config_schema( | ||
AppConfigSchemaIn, | ||
) | ||
|
||
|
||
class AppConfigSchemaOut(Schema): | ||
config_id: str | ||
|
||
|
||
@transaction.atomic | ||
@api.post("/{tenant_id}/app/", response=AppConfigSchemaOut, auth=None) | ||
def create_app_config(request, tenant_id: str, data: AppConfigSchemaIn): | ||
tenant = request.tenant | ||
# 事件分发 | ||
results = dispatch_event(Event(tag=CREATE_APP, tenant=tenant, request=request, data=data)) | ||
for func, (result, extension) in results: | ||
if result: | ||
# 创建config | ||
config = extension.create_tenant_config(tenant, data.config.data.dict()) | ||
# 创建app | ||
app = App() | ||
app.name = data.name | ||
app.url = data.url | ||
app.logo = data.logo | ||
app.type = data.app_type | ||
app.description = data.description | ||
app.config = config | ||
app.tenant_id = tenant_id | ||
app.save() | ||
break | ||
return {"app_id": app.id.hex} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
from django.contrib import admin | ||
from .models import( | ||
User, UserGroup, | ||
App, AppGroup, Permission, | ||
Approve, ExpiringToken, TenantConfig, | ||
) | ||
|
||
# admin.site.register(Tenant) | ||
admin.site.register(User) | ||
admin.site.register(UserGroup) | ||
admin.site.register(App) | ||
admin.site.register(AppGroup) | ||
admin.site.register(Permission) | ||
admin.site.register(Approve) | ||
admin.site.register(ExpiringToken) | ||
admin.site.register(TenantConfig) | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from ninja import Schema | ||
from pydantic import Field | ||
from typing import Optional | ||
from abc import abstractmethod | ||
from typing import Union, Literal | ||
from ninja.orm import create_schema | ||
from arkid.core.extension import Extension | ||
from arkid.extension.models import TenantExtensionConfig | ||
from arkid.core.translation import gettext_default as _ | ||
from arkid.core.models import App | ||
from arkid.core import api as core_api, event as core_event | ||
|
||
app_protocol_schema_map = {} | ||
|
||
def create_app_protocol_extension_config_schema(schema_cls, **field_definitions): | ||
"""创建应用协议类插件配置的Schema | ||
schema_cls只接受一个空定义的Schema | ||
Examples: | ||
>>> from ninja import Schema | ||
>>> from pydantic import Field | ||
>>> class ExampleExtensionConfigSchema(Schema): | ||
>>> pass | ||
>>> create_app_protocol_extension_config_schema( | ||
>>> ExampleExtensionConfigSchema, | ||
>>> field_name=( field_type, Field(...) ) | ||
>>> ) | ||
Args: | ||
schema_cls (ninja.Schema): 需要创建的Schema class | ||
field_definitions (Any): 任意数量的field,格式为: field_name=(field_type, Field(...)) | ||
""" | ||
for schema in app_protocol_schema_map.values(): | ||
core_api.add_fields(schema, **field_definitions) | ||
core_api.add_fields(schema_cls, __root__=(Union[tuple(app_protocol_schema_map.values())], Field(discriminator='app_type'))) # type: ignore | ||
|
||
|
||
class AppProtocolExtension(Extension): | ||
|
||
app_type_map = [] | ||
|
||
def load(self): | ||
super().load() | ||
|
||
self.listen_event(core_event.CREATE_APP, self.filter_event_handler) | ||
self.listen_event(core_event.UPDATE_APP, self.filter_event_handler) | ||
self.listen_event(core_event.DELETE_APP, self.filter_event_handler) | ||
|
||
|
||
def register_config_schema(self, schema, app_type, package=None,**kwargs): | ||
# 父类 | ||
super().register_config_schema(schema, package, **kwargs) | ||
|
||
# app_type = kwargs.get('app_type', None) | ||
# if app_type is None: | ||
# raise Exception('') | ||
new_schema = create_schema(App, | ||
name=self.package+'_config', | ||
exclude=['is_del', 'is_active', 'updated', 'created', 'tenant', 'secret'], | ||
custom_fields=[ | ||
("app_type", Literal[app_type], Field()), | ||
("config", schema, Field()) | ||
], | ||
) | ||
app_protocol_schema_map[app_type] = new_schema | ||
self.app_type_map.append(app_type) | ||
# | ||
|
||
def filter_event_handler(self, event, **kwargs): | ||
if event.data.app_type in self.app_type_map: | ||
if event.tag == core_event.CREATE_APP: | ||
return self.create_app(event, data.config) | ||
elif event.tag == core_event.UPDATE_APP: | ||
return self.update_app(event, data.config) | ||
elif event.tag == core_event.DELETE_APP: | ||
return self.delete_app(event, data.config) | ||
return False | ||
|
||
|
||
@abstractmethod | ||
def create_app(self, event, config): | ||
pass | ||
|
||
@abstractmethod | ||
def update_app(self, event, config): | ||
pass | ||
|
||
@abstractmethod | ||
def delete_app(self, event, config): | ||
pass | ||
|
Oops, something went wrong.