ckan / ckanext-hierarchy

Organization hierarchy - CKAN extension
GNU Affero General Public License v3.0
28 stars 64 forks source link

Template tag `resource` vs `asset` heuristic struggles with CKAN '2.10.0a0' #51

Closed florianm closed 2 years ago

florianm commented 2 years ago

While attempting to run ckanext-hierarchy master (roughly here) with CKAN master ('2.10.0a0'), I'm getting an exception on the template tag resource being used in [hierarchy_resource.html](https://github.com/ckan/ckanext-hierarchy/blob/master/ckanext/hierarchy/templates/hierarchy/snippets/hierarchy_resource.html), where CKAN 2.10 apparently requires asset.

    File "/usr/lib/ckan/venv/src/ckanext-hierarchy/ckanext/hierarchy/templates/hierarchy/snippets/hierarchy_resource.html", line 1, in template

    {% resource 'hierarchy/hierarchy_theme.css' %}

    jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'resource'.

It seems that the template organization_tree.html does actually toggle between resource and asset but gets the wrong CKAN version on my end.

{% set type = 'asset' if h.ckan_version() > '2.9' else 'resource' %}
{% include 'hierarchy/snippets/hierarchy_' ~ type ~ '.html' %}

The debugger console tells me my CKAN version is

 from ckan.lib.helpers import ckan_version
>>> ckan_version()
'2.10.0a0'

Related

Possible fixes

Modify ckan.lib.helpers.ckan_version or add new function

Make h.ckan_version() > '2.9' robust against versions '2.10.0a0', e.g. only compare major.minor and drop patch/dev:

@core_helper
def ckan_version() -> str:
    '''Return CKAN version'''
    # Option1: regex
    import re
    return re.match(r"^([\d]\.[\d]+)", str(ckan.__version__)).group()

    # Option 2: join/split/subset
     return ".".join(str(ckan.__version__).split(".", 2)[:2])

    # Default: returns the version as int or string
    return ckan.__version__

This would break anything depending on the patch number/string of the version string. Seeing that ckan.lib.app_lobals.py defines Globals including major version as __ckan_base_version__, and the full version as __version__, maybe an unobtrusive option would be to add the major/minor version as global, e.g. __ckan_majorminor__.

Use template helper check_ckan_version

Use ckan.plugins.toolkit.check_ckan_version() as template helper.

This works with v "2.10.0a0":

t.check_ckan_version(min_version="2.10") == True
t.check_ckan_version(min_version="2.11") == False

Edit: PR sent.