source: publico/il.spdo/trunk/il/spdo/vocabulary.py @ 5377

Última Alteração nesse arquivo desde 5377 foi 5377, incluída por fabianosantos, 8 anos atrás

Implementação nova de vocabulário que também é IQuerySource para
realização de autocomplete nos campos Choice e List de Choice.

File size: 5.0 KB
Linha 
1# -*- coding: utf-8 -*-
2
3from zope.interface import implements
4from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
5from zope.component import provideUtility
6from zope.schema.interfaces import IVocabularyFactory
7
8from zope.schema.interfaces import IContextSourceBinder
9from z3c.formwidget.query.interfaces import IQuerySource
10
11from il.spdo.config import Session
12from il.spdo import db
13from il.spdo.config import MessageFactory as _
14
15
16class BaseQueryVocabulary(object):
17    implements(IVocabularyFactory, IQuerySource)
18
19    vocab = None
20
21    def update(self):
22        raise NotImplementedError
23
24    def __call__(self, context):
25        self.vocab = self.update()
26        return self
27 
28    def __contains__(self, term):
29        return self.vocab.__contains__(term)
30
31    def __iter__(self):
32        return self.vocab.__iter__()
33 
34    def __len__(self):
35        return self.vocab.__len__()
36 
37    def getTerm(self, value):
38        return self.vocab.getTerm(value)
39 
40    def getTermByToken(self, value):
41        return self.vocab.getTermByToken(value)
42 
43    def search(self, query_string):
44        query_string = query_string.lower()
45        return [x for x in self.vocab if x.title.lower().find(query_string) != -1]
46
47class AreasVocabulary(BaseQueryVocabulary):
48
49    def update(self):
50        session = Session()
51        areas = session.query(db.Area).order_by(db.Area.nome).all()
52        return SimpleVocabulary([SimpleTerm(a.id, a.id, a.nome) for a in areas])
53
54AreasVocabularyFactory = AreasVocabulary()
55provideUtility(AreasVocabularyFactory, IVocabularyFactory,
56               name='il.spdo.areas-vocab')
57
58class TipoDocumentoVocabulary(object):
59     implements(IVocabularyFactory)
60
61     def __call__(self, context):
62         session = Session()
63         query = session.query(db.TipoDocumento).order_by(db.TipoDocumento.nome).all()
64         return SimpleVocabulary([SimpleTerm(t.id, t.id, t.nome) for t in query])
65
66TipoDocumentoVocabularyFactory = TipoDocumentoVocabulary()
67provideUtility(TipoDocumentoVocabularyFactory, IVocabularyFactory,
68               name='il.spdo.tipodocumento-vocab')
69
70class PessoaVocabulary(BaseQueryVocabulary):
71
72    def update(self):
73         session = Session()
74         query = session.query(db.Pessoa).order_by(db.Pessoa.nome).all()
75         return SimpleVocabulary([SimpleTerm(p.id, p.id, p.nome) for p in query])
76
77PessoaVocabularyFactory = PessoaVocabulary()
78provideUtility(PessoaVocabularyFactory, IVocabularyFactory,
79               name='il.spdo.pessoa-vocab')
80
81class SituacaoVocabulary(object):
82     implements(IVocabularyFactory)
83
84     def __call__(self, context):
85         session = Session()
86         query = session.query(db.Situacao).filter_by(inicial=True).order_by(db.Situacao.nome).all()
87         return SimpleVocabulary([SimpleTerm(s.id, s.id, s.nome) for s in query])
88
89SituacaoVocabularyFactory = SituacaoVocabulary()
90provideUtility(SituacaoVocabularyFactory, IVocabularyFactory,
91               name='il.spdo.situacao-vocab')
92
93class UFVocabulary(object):
94     implements(IVocabularyFactory)
95
96     def __call__(self, context):
97         session = Session()
98         query = session.query(db.UF).order_by(db.UF.nome).all()
99         return SimpleVocabulary([SimpleTerm(a.id, a.id, a.nome) for a in query])
100
101UFVocabularyFactory = UFVocabulary()
102provideUtility(UFVocabularyFactory, IVocabularyFactory,
103               name='il.spdo.uf-vocab')
104
105def TipoPessoaVocabulary(context):
106    return SimpleVocabulary([
107        SimpleTerm('F','F', _(u'Física')),
108        SimpleTerm('O','O', _(u'Organização')),
109    ])
110
111provideUtility(TipoPessoaVocabulary, IVocabularyFactory,
112               name='il.spdo.tipopessoa-vocab')
113
114def TipoProtocoloVocabulary(context):
115    return SimpleVocabulary([
116        SimpleTerm('R','R', _(u'Recebido')),
117        SimpleTerm('E','E', _(u'Expedido')),
118        SimpleTerm('I','I', _(u'Interno')),
119    ])
120
121provideUtility(TipoProtocoloVocabulary, IVocabularyFactory,
122               name='il.spdo.tipoprotocolo-vocab')
123
124class AreaSource(object):
125    implements(IQuerySource)
126
127    def __init__(self, context):
128         self.session = Session()
129         areas = self.session.query(db.Area).order_by(db.Area.nome).all()
130         self.vocab = SimpleVocabulary([SimpleTerm(a.id, a.id, a.nome) for a in areas])
131         self.keywords = [a.id for a in areas]
132
133    def __contains__(self, term):
134        return self.vocab.__contains__(term)
135
136    def __iter__(self):
137        return self.vocab.__iter__()
138
139    def __len__(self):
140        return self.vocab.__len__()
141
142    def getTerm(self, value):
143        return self.vocab.getTerm(value)
144
145    def getTermByToken(self, value):
146        return self.vocab.getTermByToken(value)
147
148    def search(self, query_string):
149        query_string = '%' + query_string.lower() + '%'
150        query = self.session.query(db.Area)
151        areas = query.filter(db.Area.nome.like(query_string)).all()
152        x = [self.vocab.getTerm(a.id) for a in areas]
153        print x, areas
154        return x
155
156class AreaSourceBinder(object):
157    implements(IContextSourceBinder)
158
159    def __call__(self, context):
160        return AreaSource(context)
Note: Veja TracBrowser para ajuda no uso do navegador do trac.
 

The contents and data of this website are published under license:
Creative Commons 4.0 Brasil - Atribuir Fonte - Compartilhar Igual.