Working "subjects" (matières) field

This commit is contained in:
Frédéric Péters 2011-06-13 21:38:31 +02:00
parent c33b16adf5
commit 661d3aa0d6
11 changed files with 149 additions and 10 deletions

View File

@ -7,8 +7,8 @@ themis.fields.egg-info/entry_points.txt
themis.fields.egg-info/not-zip-safe
themis.fields.egg-info/top_level.txt
themisfields/__init__.py
themisfields/editor.py
themisfields/handler.py
themisfields/editors.py
themisfields/handlers.py
themisfields/interfaces.py
themisfields/value.py
themisfields/widget.py
themisfields/vocabs.py
themisfields/widgets.py

View File

@ -1,4 +1,4 @@
from zope.interface import implements
from zope.interface import implements, implementsOnly
from zope.schema import Choice, Field, List, Date
from zope.schema.interfaces import IFromUnicode
@ -12,6 +12,9 @@ from themisfields.vocabs import AuthorsSource
from themisfields.interfaces import IAuthors
from themisfields.interfaces import IDate
from themisfields.interfaces import ISubjects, IList
class Commission(Field):
implements(ICommission, IFromUnicode)
@ -71,3 +74,22 @@ class Authors(List):
kw['unique'] = False
super(Authors, self).__init__(**kw)
from vocabs import SubjectsVocabulary, SubjectsVocabularyFactory
from vocabs import SubjectsSource
class Subjects(Field):
implements(ISubjects, IList)
_type = list
def __init__(self, **kw):
self.required = False
self.value_type = Choice(source=SubjectsSource())
self.unique = True
self.min_length = None
self.max_length = None
for attr in ('min_length', 'max_length', 'unique', 'value_type'):
if attr in kw: del kw[attr]
super(Subjects, self).__init__(**kw)

View File

@ -18,6 +18,9 @@
file="editors.zcml"
/>
<utility component=".vocabs.SubjectsVocabularyFactory"
name="themisfields.vocabulary.Subjects"/>
<!--
<utility component=".vocabs.CommissionsVocabularyFactory"
name="themisfields.vocabulary.Commissions"/>

View File

@ -1,10 +1,13 @@
from zope import interface, component, schema
from zope.schema import interfaces as schema_ifaces
from plone.schemaeditor import interfaces as editor_ifaces
from themisfields import interfaces
from themisfields import Commission
from themisfields import Author
from themisfields import Authors
from themisfields import Date
from themisfields import Subjects
from plone.schemaeditor.fields import FieldFactory
class ICommission(interfaces.ICommission, schema_ifaces.IFromUnicode):
@ -24,4 +27,17 @@ class IAuthors(interfaces.IAuthors, schema_ifaces.IList):
AuthorsFactory = FieldFactory(Authors, u'Author(s)')
class IDate(interfaces.IDate, schema_ifaces.IDate):
pass
DateFactory = FieldFactory(Date, u'Date')
@interface.implementer(editor_ifaces.IFieldEditFormSchema)
@component.adapter(schema_ifaces.IList)
def getSubjectsFieldSchema(field):
return se_schema.ITextLineMultiChoice
SubjectsFactory = FieldFactory(Subjects, u'Subjects')
#value_type=schema.Choice(vocabulary='themisfields.vocabulary.Subjects'))

View File

@ -22,4 +22,9 @@
component=".editors.DateFactory"
/>
<utility
name="themisfields.Subjects"
component=".editors.SubjectsFactory"
/>
</configure>

View File

@ -15,6 +15,7 @@ if HAVE_SUPERMODEL:
from themisfields import Author
from themisfields import Authors
from themisfields import Date
from themisfields import Subjects
class CommissionHandler_(BaseHandler):
pass
@ -35,3 +36,8 @@ if HAVE_SUPERMODEL:
pass
DateHandler = DateHandler_(Date)
class SubjectsHandler_(BaseHandler):
pass
SubjectsHandler = SubjectsHandler_(Subjects)

View File

@ -24,4 +24,10 @@
name="themisfields.Date"
/>
<utility
component=".handlers.SubjectsHandler"
name="themisfields.Subjects"
/>
</configure>

View File

@ -1,6 +1,6 @@
from zope.interface import Interface
from zope.schema.interfaces import IChoice, IField, IList
from zope.schema.interfaces import IChoice, IField, IList, IDate
from zope import schema
class ICommission(IField):
@ -11,3 +11,6 @@ class IAuthor(IField):
class IAuthors(IList):
'''Field containing a list of authors'''
class ISubjects(IField):
'''Field contained a list of subjects'''

View File

@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-
from zope.browser.interfaces import ITerms
from zope.interface import implements, classProvides
from zope.schema.interfaces import ISource, IContextSourceBinder
@ -12,7 +14,11 @@ class CommissionsSource(object):
classProvides(IContextSourceBinder)
def __init__(self, context=None):
self.terms = ['pomme', 'poire', 'peche', 'abricot']
# XXX get this list from the catalog
self.terms = [u'Commission de Controle des communications gouvernementales',
u'Commission de Cooperation avec d\'autres parlements',
u'Commission de la Sante',
u'Commission des Affaires sociales'] #...
def __contains__(self, value):
if self.get(value) is None:
@ -20,7 +26,7 @@ class CommissionsSource(object):
return True
def search(self, query):
return [SimpleTerm(x, x, x) for x in self.terms if x.startswith(query)]
return [SimpleTerm(x, x, x) for x in self.terms if query.lower() in x.lower()]
def get(self, value):
if value in self.terms:
@ -74,7 +80,15 @@ class AuthorsSource(object):
classProvides(IContextSourceBinder)
def __init__(self, context=None):
self.terms = ['pomme', 'poire', 'peche', 'abricot']
self.terms = [u'Aziz ALBISHARI',
u'Mohamed AZZOUZI',
u'Sfia BOUARFA',
u'Dominique BRAECKMAN',
u'Jacques BROTCHI',
u'Danielle CARON',
u'Mohammadi CHAHID',
u'Philippe CLOSE',
'Michel COLSON'] #...
def __contains__(self, value):
if self.get(value) is None:
@ -82,7 +96,7 @@ class AuthorsSource(object):
return True
def search(self, query):
return [SimpleTerm(x, x, x) for x in self.terms if x.startswith(query)]
return [SimpleTerm(x, x, x) for x in self.terms if query.lower() in x.lower()]
def get(self, value):
if value in self.terms:
@ -119,3 +133,45 @@ class AuthorsSourceQueryView(object):
if query != '':
return self.context.search(query)
class SubjectsVocabulary(object):
implements(IVocabularyFactory)
def __call__(self): #, context):
terms = [SimpleVocabulary.createTerm(x, x, x) for x in ['pomme', 'poire', 'peche', 'abricot']]
return SimpleVocabulary(terms)
SubjectsVocabularyFactory = SubjectsVocabulary()
class SubjectsSource(object):
implements(IContextSourceBinder)
def __init__(self):
# XXX get this list from the catalog
self.terms = [
u"Budget",
u"Enseignement",
u"Tourisme",
u"Relations internationales",
u"Cohésion sociale",
u"Fonction publique / administration",
u"Santé",
u"Formation professionnelle",
u"Politique d'aide aux personnes handicapées",
u"Culture",
u"Transport scolaire",
u"Action sociale",
u"Famille",
u"Sport",
u"Compétences résiduaires",
u"Coordination de la politique du Collège",
]
def __call__(self, context):
terms = [SimpleVocabulary.createTerm(x, x.encode('ascii', 'replace'), x) for x in self.terms]
return SimpleVocabulary(terms)
def __iter__(self):
# hack to let schema editor handle the field
yield u'DO NOT TOUCH'

View File

@ -5,14 +5,20 @@ from zope.component import adapts, adapter
from z3c.form.interfaces import IFormLayer, IFieldWidget
from z3c.form.widget import FieldWidget
from collective.z3cform.datetimewidget import DateWidget
from themisfields.interfaces import ICommission
from themisfields.interfaces import IAuthor
from themisfields.interfaces import IDate
from themisfields.interfaces import ISubjects
from plone.formwidget.autocomplete.interfaces import IAutocompleteWidget
from plone.formwidget.autocomplete.widget import AutocompleteSelectionWidget
from plone.formwidget.autocomplete import AutocompleteFieldWidget
from z3c.form.browser.orderedselect import OrderedSelectWidget
from z3c.form.browser.checkbox import CheckBoxWidget
class ICommissionWidget(IAutocompleteWidget):
pass
@ -42,3 +48,17 @@ def AuthorFieldWidget(field, request):
"""IFieldWidget factory for AuthorWidget."""
return FieldWidget(field, AutocompleteSelectionWidget(request))
#@adapter(IDate, IFormLayer)
#@implementer(IFieldWidget)
#def DateFieldWidget(field, request):
# """IFieldWidget factory for DateWidget."""
# return FieldWidget(field, DateWidget(request))
@adapter(ISubjects, IFormLayer)
@implementer(IFieldWidget)
def SubjectsFieldWidget(field, request):
"""IFieldWidget factory for SelectWidget."""
return FieldWidget(field, CheckBoxWidget(request))

View File

@ -9,5 +9,7 @@
<adapter factory=".widgets.CommissionFieldWidget"/>
<adapter factory=".widgets.AuthorFieldWidget"/>
<!--<adapter factory=".widgets.DateFieldWidget"/>-->
<adapter factory=".widgets.SubjectsFieldWidget"/>
</configure>