Skip to content

Commit

Permalink
refactor: use signals to register config schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Apr 24, 2024
1 parent 45d098b commit 37bc7bc
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 50 deletions.
41 changes: 24 additions & 17 deletions ckanext/ap_doi/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,33 @@ def after_dataset_update(self, context: Any, pkg_dict: dict[str, Any]):
def get_signal_subscriptions(self) -> SignalMapping:
return {
tk.signals.ckanext.signal("ap_main:collect_config_sections"): [
collect_config_sections_subscriber,
self.collect_config_sections_subscriber,
],
tk.signals.ckanext.signal("ap_main:collect_config_schemas"): [
self.collect_config_schemas_subs
],
}

@staticmethod
def collect_config_sections_subscriber(sender: None):
return ap_types.SectionConfig(
name="DOI",
configs=[
ap_types.ConfigurationItem(
name="Dashboard",
blueprint="doi_dashboard.list",
),
ap_types.ConfigurationItem(
name="DOI settings",
blueprint="doi_dashboard.config",
),
],
)

@staticmethod
def collect_config_schemas_subs(sender: None):
return ["ckanext.ap_doi:config_schema.yaml"]

# IConfigDeclaration

def declare_config_options(self, declaration: Declaration, key: Key):
Expand All @@ -92,22 +115,6 @@ def get_collection_factories(self) -> dict[str, CollectionFactory]:
return {"ap-doi": ApDOICollection}


def collect_config_sections_subscriber(sender: None):
return ap_types.SectionConfig(
name="DOI",
configs=[
ap_types.ConfigurationItem(
name="Dashboard",
blueprint="doi_dashboard.list",
),
ap_types.ConfigurationItem(
name="DOI settings",
blueprint="doi_dashboard.config",
),
],
)


class ApDOIPlugin(DOIPlugin):
def after_dataset_create(self, context, pkg_dict):
if config.is_mock_api_calls():
Expand Down
2 changes: 1 addition & 1 deletion ckanext/ap_doi/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class ApConfigurationDisplayPageView(MethodView):
def get(self):
self.schema = tk.h.ap_get_arbitrary_schema("ap_doi_config")
self.schema = tk.h.ap_get_config_schema("ap_doi_config")
data = self.get_config_form_data()

return tk.render(
Expand Down
45 changes: 26 additions & 19 deletions ckanext/ap_example/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,35 @@ def update_config(self, config_: tk.CKANConfig):
def get_signal_subscriptions(self) -> SignalMapping:
return {
tk.signals.ckanext.signal("ap_main:collect_config_sections"): [
collect_config_sections_subscriber,
self.collect_config_sections_subscriber,
],
tk.signals.ckanext.signal("ap_main:collect_config_schemas"): [
self.collect_config_schemas_subs
],
}

@staticmethod
def collect_config_sections_subscriber(sender: None):
return ap_types.SectionConfig(
name="Admin panel example",
configs=[
ap_types.ConfigurationItem(
name="Example settings",
blueprint="ap_example.config",
info="An example of schema-generated configuration form",
),
ap_types.ConfigurationItem(
name="Example display",
blueprint="ap_example.display",
info="Example of displaying values submitted from a form",
),
],
)

@staticmethod
def collect_config_schemas_subs(sender: None):
return ["ckanext.ap_example:config_schema.yaml"]

# IConfigDeclaration

def declare_config_options(self, declaration: Declaration, key: Key):
Expand All @@ -45,21 +70,3 @@ def declare_config_options(self, declaration: Declaration, key: Key):
data_dict = safe_load(file)

return declaration.load_dict(data_dict)


def collect_config_sections_subscriber(sender: None):
return ap_types.SectionConfig(
name="Admin panel example",
configs=[
ap_types.ConfigurationItem(
name="Example settings",
blueprint="ap_example.config",
info="An example of schema-generated configuration form",
),
ap_types.ConfigurationItem(
name="Example display",
blueprint="ap_example.display",
info="Example of displaying values submitted from a form",
),
],
)
2 changes: 1 addition & 1 deletion ckanext/ap_example/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

class ApConfigurationDisplayPageView(MethodView):
def get(self):
self.schema = tk.h.ap_get_arbitrary_schema("admin_panel_example_config")
self.schema = tk.h.ap_get_config_schema("admin_panel_example_config")
data = self.get_config_form_data()

return tk.render(
Expand Down
18 changes: 7 additions & 11 deletions ckanext/ap_main/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,16 @@ def generate_page_unique_class() -> str:


@helper
def get_arbitrary_schema(schema_id: str) -> dict[Any, Any] | None:
"""This is a temporary code. We've created a PR #403 to ckanext-scheming
to support an arbitrary schemas. For now, we are creating a polyfill"""
def get_config_schema(schema_id: str) -> dict[Any, Any] | None:
"""Get a schema by its id from the loaded schemas"""
from ckanext.scheming.plugins import _load_schemas, _expand_schemas

SCHEMA_OPTION = "scheming.arbitrary_schemas"
SCHEMA_TYPE_FIELD = "schema_id"
for _, schemas_paths in ap_utils.collect_config_schemas_signal.send():
schemas = _load_schemas(schemas_paths, "schema_id")
expanded_schemas = _expand_schemas(schemas)

schema_urls = tk.config.get(SCHEMA_OPTION, "").split()
schemas = _load_schemas(schema_urls, SCHEMA_TYPE_FIELD)

expanded_schemas = _expand_schemas(schemas)

return expanded_schemas.get(schema_id)
if schema := expanded_schemas.get(schema_id):
return schema


@helper
Expand Down
5 changes: 5 additions & 0 deletions ckanext/ap_main/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"Collect configuration section from subscribers",
)

collect_config_schemas_signal = tk.signals.ckanext.signal(
"ap_main:collect_config_schemas",
"Collect config schemas from subscribers",
)


def ap_before_request() -> None:
try:
Expand Down
2 changes: 1 addition & 1 deletion ckanext/ap_main/views/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def get_config_schema(self) -> dict[str, Any]:
"""Fetch a full schema or use the fields user provides and put them inside
a dict to imitate a schema"""
schema = (
tk.h.ap_get_arbitrary_schema(self.schema_id)
tk.h.ap_get_config_schema(self.schema_id)
if not self.fields
else {"schema_id": self.schema_id, "fields": self.fields}
)
Expand Down

0 comments on commit 37bc7bc

Please sign in to comment.