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

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

boxes: GridView? iniciada e funcionando com forms das caixas integrados

File size: 4.0 KB
Linha 
1import martian
2from Products.CMFCore.interfaces import IFolderish
3from five import grok
4from five.grok.components import ZopeTwoPageTemplate
5from persistent.dict import PersistentDict
6from z3c.form import form, field, datamanager
7from z3c.form.interfaces import IDataManager
8from zope.annotation import IAnnotations
9from zope.component import adapts, provideAdapter
10from zope.interface import implements
11from zope.schema.interfaces import IField
12
13from interfaces import ISimpleBox, template_dict
14
15
16class PersistentDictionaryField(datamanager.DictionaryField):
17    adapts(PersistentDict, IField)
18    implements(IDataManager)
19provideAdapter(PersistentDictionaryField)
20
21class BoxManager(object):
22
23    ALL_BOXES_KEY = 'il.portalinterlegis.boxes'
24
25    def __init__(self, schema, label=None):
26        self.schema = schema
27        #TODO: improve this text
28        self.form_label = label or u'Edite os valore desta caixa'
29
30    def build_form(self, number):
31
32        # the combination (form.EditForm, grok.View)
33        # is from https://mail.zope.org/pipermail/grok-dev/2008-July/005999.html
34        # (plone.directives.form.EditForm did not work well)
35        class BoxEditForm(form.EditForm, grok.View):
36            grok.context(IFolderish)
37            grok.name(self._box_name_for_url(number))
38            label = self.form_label
39            fields = field.Fields(self.schema)
40
41            def getContent(form_self):
42                return self.box_content(form_self.context, number)
43
44        globals()['BoxEditForm_%s' % self._box_key(number)] = BoxEditForm
45        return BoxEditForm
46
47    def build_n_forms(self, max_number):
48        for number in range(1, max_number+1):
49            self.build_form(number)
50
51    def box_content(self, context, number): # maybe this method should be private
52        annotations = IAnnotations(context)
53        boxes = get_or_create_persistent_dict(annotations, BoxManager.ALL_BOXES_KEY)
54        return get_or_create_persistent_dict(boxes, self._box_key(number))
55
56    def html(self, context, number):
57        return self._schema_template() % NiceDictGetter(
58            self.box_content(context, number))
59
60    def _box_key(self, number):
61        return '%s_%s' % (self.schema.__name__, number)
62
63    def _box_name_for_url(self, number):
64        return 'box_%s' % self._box_key(number)
65
66    def _schema_template(self):
67        return template_dict[self.schema]
68
69def get_or_create_persistent_dict(dictionary, key):
70    value = dictionary.get(key, None)
71    if not value:
72        dictionary[key] = value = PersistentDict()
73    return value
74
75class NiceDictGetter(object):
76    """Dictionary getter with a default empty string for unknown keys.
77    """
78    def __init__(self, wrapped):
79        self.wrapped = wrapped
80
81    def __getitem__(self, key):
82        value = self.wrapped.get(key, None)
83        return (value is None) and '---' or value
84
85# ROWS
86
87ROW_TEMPLATE = '''
88  <div class="dt-row">%s
89  </div>'''
90
91CELL_TEMPLATE = '''
92    <div class="dt-cell dt-position-%s dt-width-%s">%s
93    </div>'''
94
95def row_spec_to_cells(context, row_spec):
96    """Iterates transforming each cell spec from
97       (width, schema, number) to (position, width, html)"""
98    position = 0
99    for (width, schema, number) in row_spec:
100        yield (position, width, BoxManager(schema).html(context, number))
101        position += width
102
103def row_html(context, row_spec):
104    """Renders the html of one row.
105    `row_spec` is a sequence of cell specs: [(width, schema, number), ...]
106    """
107    return ROW_TEMPLATE % ''.join(
108        [CELL_TEMPLATE % cell for cell in row_spec_to_cells(context, row_spec)])
109
110class GridView(grok.View):
111    "Base class for all grid-like views"
112    martian.baseclass()
113
114    template = ZopeTwoPageTemplate(filename="gridview.pt")
115
116    def rows(self):
117        for row_spec in self.grid:
118            yield row_html(self.context, row_spec)
119
120################################################################
121# TODO: o unico lugar em que isto funcionou foi aqui. Entender porque e decidir lugar definitivo.
122
123# initialize all the box managers
124for s in [ISimpleBox,]:
125    BoxManager(s).build_n_forms(10)
126
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.