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

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

Import inicial.

File size: 1.9 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
13
14class AreasVocabulary(object):
15     implements(IVocabularyFactory)
16
17     def __call__(self, context):
18         session = Session()
19         query = session.query(db.Area).order_by(db.Area.nome).all()
20         return SimpleVocabulary([SimpleTerm(a.id, a.id, a.nome) for a in query])
21
22AreasVocabularyFactory = AreasVocabulary()
23provideUtility(AreasVocabularyFactory, IVocabularyFactory,
24               name='il.spdo.areas-vocab')
25
26
27class AreaSource(object):
28    implements(IQuerySource)
29
30    def __init__(self, context):
31         self.session = Session()
32         areas = self.session.query(db.Area).order_by(db.Area.nome).all()
33         self.vocab = SimpleVocabulary([SimpleTerm(a.id, a.id, a.nome) for a in areas])
34         self.keywords = [a.id for a in areas]
35
36    def __contains__(self, term):
37        return self.vocab.__contains__(term)
38
39    def __iter__(self):
40        return self.vocab.__iter__()
41
42    def __len__(self):
43        return self.vocab.__len__()
44
45    def getTerm(self, value):
46        return self.vocab.getTerm(value)
47
48    def getTermByToken(self, value):
49        return self.vocab.getTermByToken(value)
50
51    def search(self, query_string):
52        query_string = '%' + query_string.lower() + '%'
53        query = self.session.query(db.Area)
54        areas = query.filter(db.Area.nome.like(query_string)).all()
55        x = [self.vocab.getTerm(a.id) for a in areas]
56        print x, areas
57        return x
58
59class AreaSourceBinder(object):
60    implements(IContextSourceBinder)
61
62    def __call__(self, context):
63        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.