# -*- coding: utf-8 -*- 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 from Products.CMFCore.utils import getToolByName class CommissionsSource(object): implements(IContextSourceBinder) def __init__(self): pass def __call__(self, context): catalog = getToolByName(context, 'portal_catalog') results = catalog(portal_type='themis.datatypes.commission') commissions = sorted([x.getObject().title for x in results]) terms = [SimpleVocabulary.createTerm(x, x.encode('ascii', 'replace'), x) for x in commissions] return SimpleVocabulary(terms) def __iter__(self): # hack to let schema editor handle the field yield u'DO NOT TOUCH' def cmp_person(x, y): t = cmp(x.lastname.lower(), y.lastname.lower()) if t: return t return cmp(x.firstname.lower(), y.lastname.lower()) class DeputiesSource(object): implements(IContextSourceBinder) def __init__(self): pass @classmethod def get_terms(cls, context): catalog = getToolByName(context, 'portal_catalog') results = catalog(portal_type='themis.datatypes.deputy') deputies = [x.getObject() for x in results] deputies.sort(cmp_person) def format_deputy(x): if x.polgroup: return '%s %s (%s)' % (x.lastname, x.firstname, x.polgroup.to_object.title) else: return '%s %s (?)' % (x.lastname, x.firstname) deputies_terms = [SimpleVocabulary.createTerm( 'deputy:'+x.id, 'deputy:'+x.id, format_deputy(x)) for x in deputies] return deputies_terms def __call__(self, context): return SimpleVocabulary(self.get_terms(context)) def __iter__(self): # hack to let schema editor handle the field yield u'DO NOT TOUCH' class MinistriesSource(object): implements(IContextSourceBinder) def __init__(self): pass @classmethod def get_terms(cls, context): catalog = getToolByName(context, 'portal_catalog') results = catalog(portal_type='themis.datatypes.ministry') ministries = [x.getObject() for x in results] ministries.sort(cmp_person) def format_ministry(x): return '%s %s (ministre)' % (x.lastname, x.firstname) ministries_terms = [SimpleVocabulary.createTerm( format_ministry(x), 'ministry:'+x.id, format_ministry(x)) for x in ministries] college_term = SimpleVocabulary.createTerm( 'ministry:college','ministry:college', u'Collège') return [college_term] + ministries_terms def __call__(self, context): return SimpleVocabulary(self.get_terms(context)) def __iter__(self): # hack to let schema editor handle the field yield u'DO NOT TOUCH' class DeputiesAndMinistriesSource(object): implements(IContextSourceBinder) def __init__(self): pass def __call__(self, context): catalog = getToolByName(context, 'portal_catalog') deputies_terms = DeputiesSource.get_terms(context) ministries_terms = MinistriesSource.get_terms(context) return SimpleVocabulary(deputies_terms + ministries_terms) def __iter__(self): # hack to let schema editor handle the field yield u'DO NOT TOUCH' class ContactsVocabulary(SimpleVocabulary): context = None def __init__(self, context, contact_ids, *args, **kwargs): self.context = context self.contact_ids = contact_ids super(ContactsVocabulary, self).__init__(*args, **kwargs) def getTermByToken(self, token): try: return super(ContactsVocabulary, self).getTermByToken(token) except LookupError: if not token: raise if token.startswith('contact:'): raise # a simple string, create a contact object to match portal = getToolByName(self.context, 'portal_url').getPortalObject() plone_tool = getToolByName(self.context, 'plone_utils') new_id = plone_tool.normalizeString(token) if not portal.contacts.has_key(new_id): portal.contacts.invokeFactory( 'themis.datatypes.contact', new_id, title=token) return self.createTerm(token, 'contact:' + new_id, token) def search(self, qs): q = qs.lower() t = [self.getTermByToken(kw) for kw in self.contact_ids if q in kw.lower()] return t class ContactsSource(object): implements(IContextSourceBinder) def __init__(self): pass def __call__(self, context): catalog = getToolByName(context, 'portal_catalog') deputies_terms = DeputiesSource.get_terms(context) ministries_terms = MinistriesSource.get_terms(context) results = catalog(portal_type='themis.datatypes.contact') contacts = [x.getObject() for x in results] def cmp_contact(x, y): return cmp(x.title, y.title) contacts.sort(cmp_contact) contact_ids = [x.id for x in contacts] contacts_terms = [SimpleVocabulary.createTerm( 'contact:'+x.id, 'contact:'+x.id, x.title) for x in contacts] return ContactsVocabulary(context, contact_ids, deputies_terms + ministries_terms + contacts_terms) def __iter__(self): # hack to let schema editor handle the field yield u'DO NOT TOUCH' class SubjectsSource(object): implements(IContextSourceBinder) def __init__(self): # XXX get this list from the catalog self.terms = [ u"Action sociale", u"Budget", u"Cohésion sociale", u"Compétences résiduaires", u"Coordination de la politique du Collège", u"Culture", u"Enseignement", u"Famille", u"Fonction publique / administration", u"Formation professionnelle", u"Politique d'aide aux personnes handicapées", u"Relations internationales", u"Santé", u"Sport", u"Tourisme", u"Transport scolaire", ] 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' class LegislativeSessionsSource(object): # Legislative sessions implements(IContextSourceBinder) def __init__(self): # XXX get this list from somewhere editable self.terms = [ u"2010 - 2011", u"2009 - 2010", u"Session Ordinaire 2009", u"2008 - 2009", u"2007 - 2008", u"2006 - 2007", u"2005 - 2006", # ... ] 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'