This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
themis.fields/themisfields/vocabs.py

70 lines
2.1 KiB
Python

from zope.browser.interfaces import ITerms
from zope.interface import implements, classProvides
from zope.schema.interfaces import ISource, IContextSourceBinder
from zope.app.form.browser.interfaces import ISourceQueryView
from zope.schema.interfaces import IVocabularyFactory
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
class CommissionsSource(object):
implements(ISource)
classProvides(IContextSourceBinder)
def __init__(self, context=None):
self.terms = ['pomme', 'poire', 'peche', 'abricot']
def __contains__(self, value):
if self.get(value) is None:
return False
return True
def search(self, query):
return [SimpleTerm(x, x, x) for x in self.terms if x.startswith(query)]
def get(self, value):
if value in self.terms:
return value
return None
class CommissionsSourceQueryView(object):
implements(ITerms, ISourceQueryView)
def __init__(self, context, request):
self.context = context
self.request = request
def getTerm(self, value):
commission = self.context.get(value)
token = value
title = value
return SimpleTerm(value, token=user, title=user)
def getValue(self, token):
if token not in self.context:
raise LookupError(token)
return token
def render(self, name):
return name
def results(self, name):
# check whether the normal search button was pressed
if name+".search" in self.request.form:
query_fieldname = name+".query"
if query_fieldname in self.request.form:
query = self.request.form[query_fieldname]
if query != '':
return self.context.search(query)
#class CommissionsVocabulary(object):
# implements(IVocabularyFactory)
#
# def __call__(self, context):
# terms = [SimpleVocabulary.createTerm(x, x, x) for x in ['pomme', 'poire', 'peche', 'abricot']]
# return SimpleVocabulary(terms)
#
#CommissionsVocabularyFactory = CommissionsVocabulary()