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

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

Implementacao do formulário de cadastro do protocolo, novos
vocabularios, IAddProtocolo schema e dependencia da tool
plone.app.drafts.

File size: 4.3 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 AreasVocabulary(object):
17     implements(IVocabularyFactory)
18
19     def __call__(self, context):
20         session = Session()
21         query = session.query(db.Area).order_by(db.Area.nome).all()
22         return SimpleVocabulary([SimpleTerm(a.id, a.id, a.nome) for a in query])
23
24AreasVocabularyFactory = AreasVocabulary()
25provideUtility(AreasVocabularyFactory, IVocabularyFactory,
26               name='il.spdo.areas-vocab')
27
28class TipoDocumentoVocabulary(object):
29     implements(IVocabularyFactory)
30
31     def __call__(self, context):
32         session = Session()
33         query = session.query(db.TipoDocumento).order_by(db.TipoDocumento.nome).all()
34         return SimpleVocabulary([SimpleTerm(t.id, t.id, t.nome) for t in query])
35
36TipoDocumentoVocabularyFactory = TipoDocumentoVocabulary()
37provideUtility(TipoDocumentoVocabularyFactory, IVocabularyFactory,
38               name='il.spdo.tipodocumento-vocab')
39
40class PessoaVocabulary(object):
41     implements(IVocabularyFactory)
42
43     def __call__(self, context):
44         session = Session()
45         query = session.query(db.Pessoa).order_by(db.Pessoa.nome).all()
46         return SimpleVocabulary([SimpleTerm(p.id, p.id, p.nome) for p in query])
47
48PessoaVocabularyFactory = PessoaVocabulary()
49provideUtility(PessoaVocabularyFactory, IVocabularyFactory,
50               name='il.spdo.pessoa-vocab')
51
52class SituacaoVocabulary(object):
53     implements(IVocabularyFactory)
54
55     def __call__(self, context):
56         session = Session()
57         query = session.query(db.Situacao).filter_by(inicial=True).order_by(db.Situacao.nome).all()
58         return SimpleVocabulary([SimpleTerm(s.id, s.id, s.nome) for s in query])
59
60SituacaoVocabularyFactory = SituacaoVocabulary()
61provideUtility(SituacaoVocabularyFactory, IVocabularyFactory,
62               name='il.spdo.situacao-vocab')
63
64class UFVocabulary(object):
65     implements(IVocabularyFactory)
66
67     def __call__(self, context):
68         session = Session()
69         query = session.query(db.UF).order_by(db.UF.nome).all()
70         return SimpleVocabulary([SimpleTerm(a.id, a.id, a.nome) for a in query])
71
72UFVocabularyFactory = UFVocabulary()
73provideUtility(UFVocabularyFactory, IVocabularyFactory,
74               name='il.spdo.uf-vocab')
75
76def TipoPessoaVocabulary(context):
77    return SimpleVocabulary([
78        SimpleTerm('F','F', _(u'Física')),
79        SimpleTerm('O','O', _(u'Organização')),
80    ])
81
82provideUtility(TipoPessoaVocabulary, IVocabularyFactory,
83               name='il.spdo.tipopessoa-vocab')
84
85def TipoProtocoloVocabulary(context):
86    return SimpleVocabulary([
87        SimpleTerm('R','R', _(u'Recebido')),
88        SimpleTerm('E','E', _(u'Expedido')),
89        SimpleTerm('I','I', _(u'Interno')),
90    ])
91
92provideUtility(TipoProtocoloVocabulary, IVocabularyFactory,
93               name='il.spdo.tipoprotocolo-vocab')
94
95class AreaSource(object):
96    implements(IQuerySource)
97
98    def __init__(self, context):
99         self.session = Session()
100         areas = self.session.query(db.Area).order_by(db.Area.nome).all()
101         self.vocab = SimpleVocabulary([SimpleTerm(a.id, a.id, a.nome) for a in areas])
102         self.keywords = [a.id for a in areas]
103
104    def __contains__(self, term):
105        return self.vocab.__contains__(term)
106
107    def __iter__(self):
108        return self.vocab.__iter__()
109
110    def __len__(self):
111        return self.vocab.__len__()
112
113    def getTerm(self, value):
114        return self.vocab.getTerm(value)
115
116    def getTermByToken(self, value):
117        return self.vocab.getTermByToken(value)
118
119    def search(self, query_string):
120        query_string = '%' + query_string.lower() + '%'
121        query = self.session.query(db.Area)
122        areas = query.filter(db.Area.nome.like(query_string)).all()
123        x = [self.vocab.getTerm(a.id) for a in areas]
124        print x, areas
125        return x
126
127class AreaSourceBinder(object):
128    implements(IContextSourceBinder)
129
130    def __call__(self, context):
131        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.