Skip to content

Commit

Permalink
feature: use autogenerated config page from admin-panel
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Jan 28, 2024
1 parent 56fb0a5 commit 6029ce2
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 97 deletions.
14 changes: 13 additions & 1 deletion ckanext/drupal_api/config_declaration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@ groups:
- annotation: ckanext-drupal-api
options:
- key: ckanext.drupal_api.drupal_url
default: ""
editable: true
validators: ignore_empty unicode_safe url_validator

- key: ckanext.drupal_api.cache.duration
type: int
default: 3600
editable: true

- key: ckanext.drupal_api.timeout
type: int
default: 5
editable: true

- key: ckanext.drupal_api.request.user
editable: true

- key: ckanext.drupal_api.request.pass
editable: true

- key: ckanext.drupal_api.api_version
default: core
editable: true

- key: ckanext.drupal_api.core.menu_export_endpoint
editable: true
default: "/resource/layout/export"

- key: ckanext.drupal_api.footer_html
editable: true
49 changes: 49 additions & 0 deletions ckanext/drupal_api/config_schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
scheming_version: 2
schema_id: drupal_api_config
about: An extension config form schema

fields:
- field_name: ckanext.drupal_api.cache.duration
label: Cache TTL
form_placeholder: 600
validators: default(3600) int_validator
input_type: number

- field_name: ckanext.drupal_api.drupal_url
label: Drupal base URL
required: true

- field_name: ckanext.drupal_api.api_version
label: API version
validators: default(core), one_of([json, core])
preset: select
required: true
choices:
- value: json
label: JSON API
- value: core
label: Core REST API

- field_name: ckanext.drupal_api.core.menu_export_endpoint
label: Menu export API endpoint
validators: unicode_safe
required: true
help_text: If you are using the core API version, you might face the situation when your endpoint differ from the default one
form_attrs:
data-module: ap-disable-field
data-module-field-id: ckanext.drupal_api.api_version
data-module-field-value: json

- field_name: ckanext.drupal_api.timeout
label: API request timeout
validators: default(5) int_validator
input_type: number

- field_name: ckanext.drupal_api.request.user
label: HTTP auth user
validators: unicode_safe

- field_name: ckanext.drupal_api.request.pass
label: HTTP auth password
validators: unicode_safe
input_type: password
10 changes: 9 additions & 1 deletion ckanext/drupal_api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def get_helpers():

@helper
@cached
def menu(name: str, cache_extras: Optional[dict[str, Any]] = None) -> MaybeNotCached[Menu]:
def menu(
name: str, cache_extras: Optional[dict[str, Any]] = None
) -> MaybeNotCached[Menu]:
api_connector = get_api_version()
drupal_api = api_connector.get()

Expand All @@ -42,6 +44,7 @@ def menu(name: str, cache_extras: Optional[dict[str, Any]] = None) -> MaybeNotCa

return menu


@helper
@cached
def custom_endpoint(endpoint: str) -> dict:
Expand All @@ -66,3 +69,8 @@ def custom_endpoint(endpoint: str) -> dict:
return DontCache({})

return resp


@helper
def get_drupal_url() -> str:
return da_conf.get_drupal_url()
8 changes: 0 additions & 8 deletions ckanext/drupal_api/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import ckanext.ap_main.types as ap_types
from ckanext.ap_main.interfaces import IAdminPanel

import ckanext.drupal_api.config as da_config


class DrupalApiPlugin(p.SingletonPlugin):
p.implements(p.ITemplateHelpers)
Expand All @@ -28,12 +26,6 @@ def get_helpers(self):
def update_config(self, config_):
tk.add_template_directory(config_, "templates")

def update_config_schema(self, schema):
for _, config in da_config.get_config_options().items():
schema.update({config["key"]: config["validators"]})

return schema

# IBlueprint

def get_blueprint(self):
Expand Down
62 changes: 9 additions & 53 deletions ckanext/drupal_api/templates/drupal_api/config.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{% extends 'admin_panel/base.html' %}
{% extends 'admin_panel/config/autogenerated_config.html' %}

{% import 'macros/autoform.html' as autoform %}
{% import 'macros/form.html' as form %}

{% block breadcrumb_content %}
<li>{% link_for _("Configuration"), named_route='drupal_api.config' %}</li>
{% endblock breadcrumb_content %}
{% block before_form %}
{% if not h.drupal_api_get_drupal_url() %}
<div class="h4 text-warning mb-3">
<i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
{{ _('ERROR. Missing Drupal URL. The extension won\'t work properly!') }}
</div>
{% endif %}

{% block ap_content %}
<h1>{{ _("Drupal API config") }}</h1>

<form method="POST" action="{{ h.url_for('drupal_api.clear_cache') }}" class="mb-3">
<button type="submit" name="clear-menu-cache" value="clear_cache" class="btn btn-primary">
Expand All @@ -19,47 +18,4 @@ <h1>{{ _("Drupal API config") }}</h1>
</i>{{ _('Clear custom endpoint cache') }}
</button>
</form>

{% if not configs.drupal_url.value %}
<div class="h4 text-warning mb-3">
<i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
{{ _('ERROR. Missing Drupal URL. The extension won\'t work properly!') }}
</div>
{% endif %}

<form method="POST">
{% for _, config in configs.items() %}
{% set default_attrs = {'class': 'form-control'} %}
{% set value = data[config.key] or config.value %}

{% if config.disabled %}
{% do default_attrs.update({"disabled": 1}) %}
{% endif %}

{% macro info(text) %}
{% if text %}
<div class="info-block">
<i class="fa fa-info-circle"></i>
{{ text }}
</div>
{% endif %}
{% endmacro %}

{% if config.type == "select" %}
{% call form.select(config.key if not config.disabled else "", label=config.label, options=config.options, selected=value, error=errors[config.key], attrs=default_attrs, is_required=config.required) %}
{{ info(config.help_text) }}
{% endcall %}
{% elif config.type in ("text", "number", "password") %}
{% call form.input(config.key if not config.disabled else "", label=config.label, value=value, error=errors[config.key], type=config.type, attrs=default_attrs, is_required=config.required) %}
{{ info(config.help_text) }}
{% endcall %}
{% else %}
{% call form.textarea(config.key if not config.disabled else "", label=config.label, value=value, error=errors[config.key], attrs=default_attrs, is_required=config.required) %}
{{ info(config.help_text) }}
{% endcall %}
{% endif %}
{% endfor %}

<button type="submit" class="btn btn-primary">{{ _('Update') }}</button>
</form>
{% endblock ap_content %}
{% endblock %}
40 changes: 7 additions & 33 deletions ckanext/drupal_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from flask.views import MethodView

import ckan.plugins.toolkit as tk
from ckan.logic import parse_params

from ckanext.ap_main.utils import ap_before_request
from ckanext.ap_main.views.generics import ApConfigurationPageView

import ckanext.drupal_api.config as da_conf
from ckanext.drupal_api.utils import drop_cache_for
Expand All @@ -18,36 +18,6 @@
drupal_api.before_request(ap_before_request)


class ConfigView(MethodView):
def get(self):
return tk.render(
"drupal_api/config.html",
{"configs": da_conf.get_config_options(), "data": {}, "errors": {}},
)

def post(self):
data_dict = parse_params(tk.request.form)

try:
tk.get_action("config_option_update")(
{"user": tk.current_user.name},
data_dict,
)
except tk.ValidationError as e:
return tk.render(
"drupal_api/config.html",
extra_vars={
"data": data_dict,
"errors": e.error_dict,
"error_summary": e.error_summary,
"configs": da_conf.get_config_options(),
},
)

tk.h.flash_success(tk._("Config options have been updated"))
return tk.h.redirect_to("drupal_api.config")


class ConfigClearCacheView(MethodView):
def post(self):
if "clear-menu-cache" in tk.request.form:
Expand All @@ -63,8 +33,12 @@ def post(self):

drupal_api.add_url_rule(
"/config",
view_func=ConfigView.as_view("config"),
methods=("GET", "POST"),
view_func=ApConfigurationPageView.as_view(
"config",
"drupal_api_config",
render_template="drupal_api/config.html",
page_title=tk._("Drupal API config")
)
)
drupal_api.add_url_rule(
"/clear_cache",
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# http://packaging.python.org/en/latest/tutorial.html#version
version='0.7.0',
version='0.7.1',

description='''''',
long_description=long_description,
Expand Down

0 comments on commit 6029ce2

Please sign in to comment.