Skip to content

Commit

Permalink
DATASHADES-319 / extension scuffold and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Apr 29, 2024
1 parent 28d7462 commit bdacda4
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 54 deletions.
57 changes: 57 additions & 0 deletions ckanext/charts/assets/js/charts-form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
ckan.module("charts-json-form", function ($, _) {
"use strict";
return {
options: {},

initialize: function () {
$.proxyAll(this, /_/);

// var editor = new JSONEditor(document.getElementById('editor_holder'), {
// theme: 'bootstrap5',
// schema: {
// type: "object",
// title: "Car",
// properties: {
// make: {
// type: "string",
// enum: [
// "Toyota",
// "BMW",
// "Honda",
// "Ford",
// "Chevy",
// "VW"
// ]
// },
// model: {
// type: "string"
// },
// year: {
// type: "integer",
// enum: [
// 1995, 1996, 1997, 1998, 1999,
// 2000, 2001, 2002, 2003, 2004,
// 2005, 2006, 2007, 2008, 2009,
// 2010, 2011, 2012, 2013, 2014
// ],
// default: 2008
// },
// safety: {
// type: "integer",
// format: "rating",
// maximum: "5",
// exclusiveMaximum: false,
// readonly: false
// }
// }
// }
// });

// // Hook up the submit button to log to the console
// document.getElementById('submit').addEventListener('click', function () {
// // Get the value from the editor
// console.log(editor.getValue());
// });
}
};
});
14 changes: 0 additions & 14 deletions ckanext/charts/assets/vendor/chart.js

This file was deleted.

8 changes: 8 additions & 0 deletions ckanext/charts/assets/vendor/jsoneditor.min.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion ckanext/charts/assets/webassets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ chartjs:

plotly:
filter: rjsmin
output: ckanext-charts/%(version)s-chartjs.js
output: ckanext-charts/%(version)s-chartjs-plotly.js
contents:
- vendor/plotly.js
- vendor/jsoneditor.min.js

- js/charts-form.js

extra:
preload:
- base/main
Expand Down
70 changes: 35 additions & 35 deletions ckanext/charts/templates/charts/charts_form.html
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
{% import "macros/form.html" as form %}
{% asset "charts/plotly" %}

<fieldset class="charts-view--chart-settings"
hx-get="{{ h.url_for('charts_view.form', resource_id=resource_id) }}"
hx-trigger="submit,change"
hx-include="closest form"
hx-swap="outerHTML"
>
<div id="editor_holder" data-module="charts-json-form"></div>

<fieldset class="charts-view--chart-settings" hx-get="{{ h.url_for('charts_view.form', resource_id=resource_id) }}"
hx-trigger="submit,change" hx-include="closest form" hx-swap="outerHTML">

{{ form.select("engine", label=_("Engine"), selected=settings.engine, options=[
{"value": "", "text": ""},
{"value": "plotly", "text": "plotly"},
]) }}
{"value": "", "text": ""},
{"value": "plotly", "text": "plotly"},
]) }}

{{ form.input("query", label=_("Query"), value=settings.query) }}

{{settings}}
{% if settings.engine == "plotly" %}
{{ form.select("type", label=_("Type"), selected=settings.type, options=[
{"value": "", "text": ""},
{"value": "bar", "text": "Bar"},
{"value": "line", "text": "Line"},
{"value": "scatter", "text": "Scatter"},
]) }}

{% if settings.type in ["bar", "line", "scatter"] %}
{{ form.select("x", label=_("X Axis"), selected=settings.x, options=column_options) }}
{{ form.checkbox("log_x", label=_("Log-scale x-axis"), checked=settings.log_x, value="yes") }}

{{ form.select("y", label=_("Y Axis"), selected=settings.y, options=column_options) }}
{{ form.checkbox("log_y", label=_("Log-scale y-axis"), checked=settings.log_y, value="yes") }}

{{ form.select("color", label=_("Color"), selected=settings.color, options=[{"text": "", "value": ""}] + column_options) }}

{{ form.select("animation_frame", label=_("Animation frame"), selected=settings.animation_frame, options=[{"text": "", "value": ""}] + column_options) }}
{{ form.select("hover_name", label=_("Tooltip label"), selected=settings.hover_name, options=column_options) }}
{{ form.input("limit", label=_("Limit"), value=settings.limit, type="number", attrs={"max": 10000, "class": "form-control"}) }}

{{ form.select("size", label=_("Size"), selected=settings.size, options=[{"text": "", "value": ""}] + column_options) }}
{{ form.input("size_max", label=_("Max size"), value=settings.size_max|default(20), type="number") }}
{% endif %}
{{ form.select("type", label=_("Type"), selected=settings.type, options=[
{"value": "", "text": ""},
{"value": "bar", "text": "Bar"},
{"value": "line", "text": "Line"},
{"value": "scatter", "text": "Scatter"},
]) }}

{% if settings.type in ["bar", "line", "scatter"] %}
{{ form.select("x", label=_("X Axis"), selected=settings.x, options=column_options) }}
{{ form.checkbox("log_x", label=_("Log-scale x-axis"), checked=settings.log_x, value="yes") }}

{{ form.select("y", label=_("Y Axis"), selected=settings.y, options=column_options) }}
{{ form.checkbox("log_y", label=_("Log-scale y-axis"), checked=settings.log_y, value="yes") }}

{{ form.select("color", label=_("Color"), selected=settings.color, options=[{"text": "", "value": ""}] +
column_options) }}

{{ form.select("animation_frame", label=_("Animation frame"), selected=settings.animation_frame, options=[{"text":
"", "value": ""}] + column_options) }}
{{ form.select("hover_name", label=_("Tooltip label"), selected=settings.hover_name, options=column_options) }}
{{ form.input("limit", label=_("Limit"), value=settings.limit, type="number", attrs={"max": 10000, "class":
"form-control"}) }}

{{ form.select("size", label=_("Size"), selected=settings.size, options=[{"text": "", "value": ""}] +
column_options) }}
{{ form.input("size_max", label=_("Max size"), value=settings.size_max|default(20), type="number") }}
{% endif %}
{% endif %}

<div class="charts-view--preview" id="charts-view--preview">
{% include ["charts/snippets/" ~ settings.engine ~ "_chart.html", "charts/snippets/unknown_chart.html"] %}
</div>

<input type="hidden" name="{{ g.csrf_field_name }}" value="{{ csrf_token() }}"
hx-swap-oob='outerHTML:[name="{{ g.csrf_field_name }}"]'
/>
hx-swap-oob='outerHTML:[name="{{ g.csrf_field_name }}"]' />

</fieldset>
Empty file.
28 changes: 28 additions & 0 deletions ckanext/charts/tests/test_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# encoding: utf-8

import pytest
from ckan.common import config

from urllib.parse import urljoin

import ckan.lib.helpers as h
import ckanext.textview.plugin as plugin
from ckan.tests import factories


@pytest.mark.usefixtures("clean_db", "with_plugins")
class TestAllowedViews:

def test_not_in_datastore(self):
dataset = factories.Dataset()
resource = factories.Resource(package_id=dataset["id"])

assert not h.get_allowed_view_types(resource, dataset)

def test_in_datastore(self):
dataset = factories.Dataset()
resource = factories.Resource(package_id=dataset["id"], datastore_active=True)

assert h.get_allowed_view_types(resource, dataset) == [
("charts_view", "Chart", "chart-line")
]
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
plotly
pandas
6 changes: 2 additions & 4 deletions test.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ error_email_from = ckan@localhost
[app:main]
use = config:../ckan/test-core.ini

# Insert any custom config settings to be used when running your extension's
# tests here. These will override the one defined in CKAN core's test-core.ini
ckan.plugins = charts

ckan.plugins = charts charts_view
ckan.views.default_views = charts_view

# Logging configuration
[loggers]
Expand Down

0 comments on commit bdacda4

Please sign in to comment.