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

Memoize vocabularies #42

Merged
merged 8 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.coverage
.installed.cfg
.mr.developer.cfg
local.cfg
bin
build
develop-eggs
Expand Down
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ Changes
1.4.5 (unreleased)
------------------

- Nothing changed yet.


1.4.4.post1 (2017-06-01)
------------------------

- Add explicit dependency on plone.api >= 1.5 which
the api.portal.get_current_language api was introduced.
[vincentfretin]
Expand Down
23 changes: 2 additions & 21 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,5 @@ include .coveragerc
include buildout.cfg
include travis.cfg
recursive-include images *.gif
recursive-include src *.babelrc
recursive-include src *.cfg
recursive-include src *.css
recursive-include src *.eot
recursive-include src *.eslintignore
recursive-include src *.eslintrc
recursive-include src *.gitkeep
recursive-include src *.html
recursive-include src *.js
recursive-include src *.json
recursive-include src *.map
recursive-include src *.po
recursive-include src *.pot
recursive-include src *.pt
recursive-include src *.sh
recursive-include src *.svg
recursive-include src *.ttf
recursive-include src *.txt
recursive-include src *.woff
recursive-include src *.xml
recursive-include src *.zcml
graft src
global-exclude *.pyc
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def read(*pathnames):
name='collective.taxonomy',
version=version,
description="Create, edit and use hierarchical taxonomies in Plone!",
url='http://pypi.python.org/pypi/collective.taxonomy',
long_description='\n'.join([
read('README.rst'),
read('CHANGES.rst'),
Expand Down
1 change: 0 additions & 1 deletion src/collective/taxonomy/vocabulary.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class TaxonomyVocabulary(object):
implements(IVocabularyFactory)

def __call__(self, adapter):
results = []
sm = getSite().getSiteManager()
utilities = sm.getUtilitiesFor(ITaxonomy)
return SimpleVocabulary([
Expand Down
35 changes: 33 additions & 2 deletions src/collective/taxonomy/widget.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
from time import time
from plone.memoize import ram
import zope.interface
import zope.component
import zope.schema.interfaces

from z3c.form import interfaces
from z3c.form.browser.orderedselect import OrderedSelectWidget
from z3c.form.widget import FieldWidget
from z3c.form.widget import SequenceWidget
from z3c.form.browser import widget

from interfaces import ITaxonomySelectWidget

ONE_DAY = 60 * 60 * 24


def _cache_one_day(fun, self):
key = '{0}{1}'.format(
self.field.__name__,
time() // ONE_DAY
)
return key

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add another component to the key (not sure what) so that the cache is invalidated if you change the taxonomy ttw or by import?


@zope.interface.implementer(ITaxonomySelectWidget,
interfaces.IOrderedSelectWidget)
class TaxonomySelectWidget(OrderedSelectWidget):
zope.interface.implements(ITaxonomySelectWidget,
interfaces.IOrderedSelectWidget)

@ram.cache(_cache_one_day)
def _get_items(self):
return [
self.getItem(term, count)
for count, term in enumerate(self.terms)
]

def update(self):
"""See z3c.form.interfaces.IWidget."""
SequenceWidget.update(self)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't you use super(OrderedSelectWidget, self).update() here? I think widget.HTMLSelectWidget.update is not called right now.

widget.addFieldClass(self)
self.items = self._get_items()
self.selectedItems = [
self.getItem(self.terms.getTermByToken(token), count)
for count, token in enumerate(self.value)]
self.notselectedItems = self.deselect()


@zope.component.adapter(zope.schema.interfaces.ISequence,
Expand Down