Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datatables View Ext: Null / NoneType Front-End Label Option & Helper #7574

Merged
merged 11 commits into from May 21, 2023
6 changes: 6 additions & 0 deletions changes/7574.feature
@@ -0,0 +1,6 @@
Added `ckan.datatables.null_label` config option.

Added `datatablesview_null_label` helper.

Datatables Views will now show blank cells
for NoneType field values by default.
4 changes: 3 additions & 1 deletion ckanext/datatablesview/blueprint.py
Expand Up @@ -122,8 +122,10 @@ def ajax(resource_view_id: str):
dtdata = {u'error': query_error}
else:
data = []
null_label = h.datatablesview_null_label()
for row in response[u'records']:
record = {colname: str(row.get(colname, u''))
record = {colname: str(null_label if row.get(colname, u'')
is None else row.get(colname, u''))
for colname in cols}
# the DT_RowId is used in DT to set an element id for each record
record['DT_RowId'] = 'row' + str(row.get(u'_id', u''))
Expand Down
6 changes: 6 additions & 0 deletions ckanext/datatablesview/config_declaration.yaml
Expand Up @@ -93,3 +93,9 @@ groups:
the user to view, copy and print the details of a specific row. This
value can be overridden at the resource level when configuring a
DataTables resource view.

- key: ckan.datatables.null_label
example: N/A
description: |
The option defines the label used to display NoneType values for the front-end.
This should be a string and can be translated via po files.
JVickery-TBS marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions ckanext/datatablesview/helpers.py
@@ -0,0 +1,5 @@
from ckan.plugins.toolkit import _, config


def datatablesview_null_label():
return _(config.get('ckan.datatables.null_label', u''))
10 changes: 8 additions & 2 deletions ckanext/datatablesview/plugin.py
Expand Up @@ -2,11 +2,11 @@
from __future__ import annotations

from ckan.common import CKANConfig
from typing import Any, cast
from typing import Any, cast, Callable
from ckan.types import Context, ValidatorFactory
import ckan.plugins as p
import ckan.plugins.toolkit as toolkit
from ckanext.datatablesview import blueprint
from ckanext.datatablesview import blueprint, helpers

default = cast(ValidatorFactory, toolkit.get_validator(u'default'))
boolean_validator = toolkit.get_validator(u'boolean_validator')
Expand All @@ -22,6 +22,7 @@ class DataTablesView(p.SingletonPlugin):
p.implements(p.IConfigurer, inherit=True)
p.implements(p.IResourceView, inherit=True)
p.implements(p.IBlueprint)
p.implements(p.ITemplateHelpers)
JVickery-TBS marked this conversation as resolved.
Show resolved Hide resolved

# IBlueprint

Expand Down Expand Up @@ -96,3 +97,8 @@ def info(self) -> dict[str, Any]:
u'filterable': [default(True), boolean_validator],
}
}

# ITemplateHelpers

def get_helpers(self) -> dict[str, Callable[..., object]]:
return {'datatablesview_null_label': helpers.datatablesview_null_label}