From 99355cfdf5b019319baa2ba632309c50741083b0 Mon Sep 17 00:00:00 2001 From: mutantsan Date: Mon, 17 Jun 2024 13:13:27 +0300 Subject: [PATCH] implement user chart builder view --- ckanext/charts/assets/js/charts-global.js | 14 +++---- ckanext/charts/plugin.py | 40 +++++++++++++++++++ .../templates/charts/charts_builder_form.html | 9 +++++ .../templates/package/snippets/view_form.html | 2 +- setup.cfg | 3 +- 5 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 ckanext/charts/templates/charts/charts_builder_form.html diff --git a/ckanext/charts/assets/js/charts-global.js b/ckanext/charts/assets/js/charts-global.js index cecf2e7..76f59a1 100644 --- a/ckanext/charts/assets/js/charts-global.js +++ b/ckanext/charts/assets/js/charts-global.js @@ -5,14 +5,14 @@ ckan.module("charts-global", function ($, _) { initialize: function () { $.proxyAll(this, /_/); - // // initialize CKAN modules for HTMX loaded pages - // htmx.on("htmx:afterSettle", function (event) { - // var elements = event.target.querySelectorAll("[data-module]"); + // initialize CKAN modules for HTMX loaded pages + htmx.on("htmx:afterSettle", function (event) { + var elements = event.target.querySelectorAll("[data-module]"); - // for (let node of elements) { - // ckan.module.initializeElement(node); - // } - // }); + for (let node of elements) { + ckan.module.initializeElement(node); + } + }); } }; }); diff --git a/ckanext/charts/plugin.py b/ckanext/charts/plugin.py index 6ecca41..60a3794 100644 --- a/ckanext/charts/plugin.py +++ b/ckanext/charts/plugin.py @@ -193,3 +193,43 @@ def before_resource_delete( cache.invalidate_by_key( fetchers.DatastoreDataFetcher(resource["id"]).make_cache_key(), ) + + +class ChartsBuilderViewPlugin(p.SingletonPlugin): + p.implements(p.IResourceView) + + # IResourceView + + def info(self) -> dict[str, Any]: + return { + "name": "charts_builder_view", + "title": tk._("Chart Builder"), + "schema": settings_schema(), + "icon": "chart-line", + "iframed": False, + "filterable": False, + "preview_enabled": False, + "requires_datastore": True, + } + + def can_view(self, data_dict: dict[str, Any]) -> bool: + if data_dict["resource"].get("datastore_active"): + return True + + return False + + def setup_template_variables( + self, + context: types.Context, + data_dict: dict[str, Any], + ) -> dict[str, Any]: + return { + "settings": {}, + "resource_id": data_dict["resource"]["id"], + } + + def view_template(self, context: types.Context, data_dict: dict[str, Any]) -> str: + return "charts/charts_view.html" + + def form_template(self, context: types.Context, data_dict: dict[str, Any]) -> str: + return "charts/charts_builder_form.html" diff --git a/ckanext/charts/templates/charts/charts_builder_form.html b/ckanext/charts/templates/charts/charts_builder_form.html new file mode 100644 index 0000000..b8b4143 --- /dev/null +++ b/ckanext/charts/templates/charts/charts_builder_form.html @@ -0,0 +1,9 @@ +{% import 'macros/form.html' as form %} + +{% set data = data or {} %} +{% set errors = errors or {} %} + +{{ form.errors(error_summary) }} + +{{ form.input('title', id='field-title', label=_('Title'), placeholder=_('eg. My View'), value=data.title, error=errors.title, classes=['control-full', 'control-large'], is_required=true) }} +{{ form.markdown('description', id='field-description', label=_('Description'), placeholder=_('eg. Information about my view'), value=data.description, error=errors.description) }} diff --git a/ckanext/charts/templates/package/snippets/view_form.html b/ckanext/charts/templates/package/snippets/view_form.html index 8ac9e9d..e60e264 100644 --- a/ckanext/charts/templates/package/snippets/view_form.html +++ b/ckanext/charts/templates/package/snippets/view_form.html @@ -5,7 +5,7 @@ {{ form.errors(error_summary) }} -{% if data.view_type == "charts_view" %} +{% if data.view_type in ["charts_view", "charts_builder_view"] %} {% include form_template %} {% else %} {{ form.input('title', id='field-title', label=_('Title'), placeholder=_('eg. My View'), value=data.title, error=errors.title, classes=['control-full', 'control-large'], is_required=true) }} diff --git a/setup.cfg b/setup.cfg index 3ce8ed1..7b569eb 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = ckanext-charts -version = 0.0.2 +version = 1.0.0 description = long_description = file: README.md long_description_content_type = text/markdown @@ -29,6 +29,7 @@ include_package_data = True [options.entry_points] ckan.plugins = charts_view = ckanext.charts.plugin:ChartsViewPlugin + charts_builder_view = ckanext.charts.plugin:ChartsBuilderViewPlugin babel.extractors = ckan = ckan.lib.extract:extract_ckan