source: publico/PortalInterlegis/produtos/il.portalinterlegis/src/il/portalinterlegis/browser/boxes.py @ 6473

Última Alteração nesse arquivo desde 6473 foi 6473, incluída por mazza, 9 anos atrás

boxes: row_html

File size: 3.1 KB
Linha 
1from Products.CMFCore.interfaces import IFolderish
2from five import grok
3from persistent.dict import PersistentDict
4from z3c.form import form, field, datamanager
5from z3c.form.interfaces import IDataManager
6from zope.annotation import IAnnotations
7from zope.component import adapts, provideAdapter
8from zope.interface import implements
9from zope.schema.interfaces import IField
10from zope.interface import Interface
11
12class PersistentDictionaryField(datamanager.DictionaryField):
13    adapts(PersistentDict, IField)
14    implements(IDataManager)
15provideAdapter(PersistentDictionaryField)
16
17class BoxManager(object):
18
19    ALL_BOXES_KEY = 'il.portalinterlegis.boxes'
20
21    def __init__(self, schema, label=None):
22        self.schema = schema
23        #TODO: improve this text
24        self.form_label = label or u'Edite os valore desta caixa'
25
26    def build_form(self, number):
27
28        # the combination (form.EditForm, grok.View)
29        # is from https://mail.zope.org/pipermail/grok-dev/2008-July/005999.html
30        # (plone.directives.form.EditForm did not work well)
31        class BoxEditForm(form.EditForm, grok.View):
32            grok.context(IFolderish)
33            grok.name(self._box_name_for_url(number))
34            label = self.form_label
35            fields = field.Fields(self.schema)
36
37            def getContent(form_self):
38                return self.box_content(form_self.context, number)
39
40        globals()['BoxEditForm_%s' % self._box_key(number)] = BoxEditForm
41        return BoxEditForm
42
43    def build_n_forms(self, max_number):
44        for number in range(1, max_number+1):
45            self.build_form(number)
46
47    def box_content(self, context, number): # maybe this method should be private
48        annotations = IAnnotations(context)
49        boxes = get_or_create_persistent_dict(annotations, BoxManager.ALL_BOXES_KEY)
50        return get_or_create_persistent_dict(boxes, self._box_key(number))
51
52    def html(self, context, number):
53        return self._schema_template() % self.box_content(context, number)
54
55    def _box_key(self, number):
56        return '%s_%s' % (self.schema.__name__, number)
57
58    def _box_name_for_url(self, number):
59        return 'box_%s' % self._box_key(number)
60
61    def _schema_template(self):
62        return template_dict[self.schema]
63
64template_dict = {}
65def template(t):
66    def f(cls):
67        template_dict[cls] = t
68        return cls
69    return f
70
71def get_or_create_persistent_dict(dictionary, key):
72    value = dictionary.get(key, None)
73    if not value:
74        dictionary[key] = value = PersistentDict()
75    return value
76
77# ROWS
78
79ROW_TEMPLATE = '''
80  <div class="dt-row">%s
81  </div>'''
82
83CELL_TEMPLATE = '''
84    <div class="dt-cell dt-position-%s dt-width-%s">%s
85    </div>'''
86
87def row_spec_to_cells(context, row_spec):
88    """Iterates transforming each cell spec from
89       (width, schema, number) to (position, width, html)"""
90    position = 0
91    for (width, schema, number) in row_spec:
92        yield (position, width, BoxManager(schema).html(context, number))
93        position += width
94
95def row_html(context, row_spec):
96    return ROW_TEMPLATE % ''.join(
97        [CELL_TEMPLATE % cell for cell in row_spec_to_cells(context, row_spec)])
98
99
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.