Skip to content

Commit

Permalink
Merge pull request #574 from plone/improve-vocabulary-template
Browse files Browse the repository at this point in the history
add option for StaticCatalogVocabulary instead of dynamic one
  • Loading branch information
MrTango authored Jan 17, 2025
2 parents 9fd4df8 + 32805b4 commit c1e77bb
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 30 deletions.
1 change: 1 addition & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
python-version:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
os:
- ubuntu-latest
Expand Down
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog
6.3.5 (unreleased)
------------------

- Nothing changed yet.
- add static_catalog_vocab option to vocabulary template
[MrTango]


6.3.4 (2024-11-05)
Expand Down
4 changes: 2 additions & 2 deletions bobtemplates/plone/addon/constraints_plone52.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# setuptools==40.2.0
# zc.buildout==2.13.2

isort>=5
isort>=5.12.0
black==22.8.0
tox==4.3.5
tox==4.11.3
flake8==5.0.4
4 changes: 2 additions & 2 deletions bobtemplates/plone/addon/constraints_plone60.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
## SSL Certs on windows, because Python is missing them otherwise:
#certifi ; platform_system == 'Windows'
tox==4.3.5
isort>=5
tox==4.11.3
isort>=5.12.0
black==22.8.0
flake8==5.0.4
29 changes: 14 additions & 15 deletions bobtemplates/plone/addon/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
min_version = 4.11.0

envlist =
py37-lint,
py38-lint,
py39-lint,
py310-lint,
py311-lint,
black-check,
py{38,37}-Plone{52},
py{39,310}-Plone{60},
py{38}-Plone{52},
py{39,310,311}-Plone{60},
# docs,
# coverage-report,

Expand All @@ -18,11 +18,10 @@ skip_missing_interpreters = True

[gh-actions]
python =
3.6: py36
3.7: py37
3.8: py38
3.9: py39
3.10: py310
3.11: py311


[gh-actions:env]
Expand Down Expand Up @@ -62,7 +61,7 @@ deps =
[testenv:coverage-report]
skip_install = true
usedevelop = True
basepython = python3.9
basepython = python3

deps =
coverage
Expand Down Expand Up @@ -130,7 +129,7 @@ commands =


[testenv:black-check]
basepython = python3.9
basepython = python3
skip_install = True
deps =
-cconstraints.txt
Expand All @@ -141,7 +140,7 @@ commands =


[testenv:black-enforce]
basepython = python3.9
basepython = python3
skip_install = True
deps =
-cconstraints.txt
Expand All @@ -151,13 +150,6 @@ commands =
black -v src setup.py


[testenv:py37-lint]
basepython = python3.7
skip_install = true
deps = {[lint]deps}
commands = {[lint]commands}
allowlist_externals = {[lint]allowlist_externals}

[testenv:py38-lint]
basepython = python3.8
skip_install = true
Expand All @@ -179,6 +171,13 @@ deps = {[lint]deps}
commands = {[lint]commands}
allowlist_externals = {[lint]allowlist_externals}

[testenv:py311-lint]
basepython = python3.11
skip_install = true
deps = {[lint]deps}
commands = {[lint]commands}
allowlist_externals = {[lint]allowlist_externals}

[testenv:docs]
skip_install = true

Expand Down
2 changes: 1 addition & 1 deletion bobtemplates/plone/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def run_black(configurator):
print("\nblack-enforce: successful:\n{0}\n".format(safe_unicode(test_result)))
except OSError as e:
print(
"Error on black-enforce: {0}, make sure you have tox an black installed globally!".format(
"Error on black-enforce: {0}, make sure you have tox and black installed globally!".format(
safe_unicode(e)
)
)
Expand Down
8 changes: 8 additions & 0 deletions bobtemplates/plone/vocabulary/.mrbob.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ vocabulary_name.required = True
vocabulary_name.default = AvailableThings
vocabulary_name.post_ask_question = bobtemplates.plone.base:check_klass_name

is_static_catalog_vocab.question = Is the vocabulary a StaticCatalogVocabulary?
is_static_catalog_vocab.help = StaticCatalogVocabulary is often used for Choice fields where the values are resolved by relations.
is_static_catalog_vocab.required = True
is_static_catalog_vocab.default = n
is_static_catalog_vocab.post_ask_question = mrbob.hooks:validate_choices mrbob.hooks:to_boolean
is_static_catalog_vocab.choices = y|n
is_static_catalog_vocab.choices_delimiter = |


[template]
pre_render = bobtemplates.plone.vocabulary:prepare_renderer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
# -*- coding: utf-8 -*-

# from plone import api
from zope.schema.interfaces import IVocabularyFactory
from zope.interface import implementer
{{% if is_static_catalog_vocab == 'y' %}}
from plone.app.vocabularies.catalog import StaticCatalogVocabulary
{{% else %}}
from {{{package.dottedname}}} import _
from plone.dexterity.interfaces import IDexterityContent
from zope.globalrequest import getRequest
from zope.interface import implementer
from zope.schema.interfaces import IVocabularyFactory
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary
{{% endif %}}


{{% if is_static_catalog_vocab != 'y' %}}
class VocabItem(object):
def __init__(self, token, value):
self.token = token
self.value = value
{{% endif %}}


@implementer(IVocabularyFactory)
Expand All @@ -22,6 +28,19 @@ class {{{ vocabulary_name_klass }}}(object):
"""

def __call__(self, context):
{{% if is_static_catalog_vocab == 'y' %}}
{{{ is_static_catalog_vocab }}}
return StaticCatalogVocabulary(
{
# possible portal_types:
"portal_type": [
"Event",
]
},
# customizable title of the Choice items, by default brain.Title:
title_template="{brain.Title}",
)
{{% else %}}
# Just an example list of content for our vocabulary,
# this can be any static or dynamic data, a catalog result for example.
items = [
Expand All @@ -47,6 +66,7 @@ class {{{ vocabulary_name_klass }}}(object):
)
# Create a SimpleVocabulary from the terms list and return it:
return SimpleVocabulary(terms)
{{% endif %}}


{{{ vocabulary_name_klass }}}Factory = {{{ vocabulary_name_klass }}}()
1 change: 1 addition & 0 deletions package_tests/test_vocabulary.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def test_post_renderer(tmpdir):
"plone.version": "5.1",
"vocabulary_name": "AvailableTasks",
"vocabulary_description": "Bla",
"is_static_catalog_vocab": "n",
},
)

Expand Down
1 change: 1 addition & 0 deletions skeleton-tests/test_addon_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ def test_addon_all(tmpdir, capsys, config):
template = """[variables]
subtemplate_warning = Yes
vocabulary_name = AvailableTasks
is_static_catalog_vocab = n
"""
generate_answers_ini(package_dir, template)

Expand Down
14 changes: 7 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ depends =

[testenv:coverage-report]
#usedevelop = True
basepython = python3.9
basepython = python3
deps =
-cconstraints.txt
coverage
Expand All @@ -99,7 +99,7 @@ skip_install = true
commands = coverage erase

[testenv:isort-apply]
basepython = python3.10
basepython = python3
deps =
-cconstraints.txt
isort
Expand All @@ -108,7 +108,7 @@ commands =
isort {toxinidir}/bobtemplates setup.py skeleton-tests package_tests

[testenv:autopep8]
basepython = python3.9
basepython = python3
skip_install = true
deps =
-cconstraints.txt
Expand Down Expand Up @@ -181,7 +181,7 @@ commands = {[lint]commands}
allowlist_externals = {[lint]allowlist_externals}

[testenv:black-check]
basepython = python3.10
basepython = python3
skip_install = True
deps =
-cconstraints.txt
Expand All @@ -191,7 +191,7 @@ commands =
black --check --diff -v bobtemplates setup.py skeleton-tests package_tests

[testenv:black-enforce]
basepython = python3.10
basepython = python3
skip_install = True
deps =
-cconstraints.txt
Expand All @@ -201,7 +201,7 @@ commands =
black -v bobtemplates setup.py skeleton-tests package_tests

[testenv:docs]
basepython = python3.10
basepython = python3
commands =
sphinx-build -b html -d _build/docs/doctrees docs _build/docs/html -W
# sphinx-build -b doctest docs _build/docs/doctrees
Expand All @@ -212,7 +212,7 @@ deps =

[testenv:release]
skip_install = true
basepython = python3.10
basepython = python3

deps =
-cconstraints.txt
Expand Down

0 comments on commit c1e77bb

Please sign in to comment.