Conjunto de mudanças 6497 no repositório publico


Ignorar:
Timestamp:
14/06/2012 17:18:26 (9 anos atrás)
Autor:
mazza
Mensagem:

usando callables para generalizar boxes

Localização:
PortalInterlegis/produtos/il.portalinterlegis/trunk/src/il/portalinterlegis
Arquivos:
1 adicionado
3 editados

Legenda:

Não Modificado
Adicionado
Removido
  • PortalInterlegis/produtos/il.portalinterlegis/trunk/src/il/portalinterlegis/browser/boxes/manager.py

    r6495 r6497  
    11import martian
    22from AccessControl import getSecurityManager
    3 from Products.CMFCore import permissions
    43from Products.CMFCore.interfaces import IFolderish
     4from Products.CMFCore.permissions import ModifyPortalContent
    55from five import grok
    66from five.grok.components import ZopeTwoPageTemplate
     
    8686        dictionary[key] = value = PersistentDict()
    8787    return value
     88from exceptions import ValueError
     89# ROWS
     90class DtRow(object):
    8891
    89 # ROWS
    90 
    91 ROW_TEMPLATE = '''
     92    ROW_TEMPLATE = '''
    9293  <div class="dt-row">%s
    9394  </div>'''
    9495
    95 CELL_TEMPLATE = '''
    96     <div id="%s" class="%s dt-cell dt-position-%s dt-width-%s">%s
     96    CELL_TEMPLATE = '''
     97    <div class="dt-cell dt-position-%s dt-width-%s">%s
    9798    </div>'''
    9899
    99 # TODO: refactor these functions to live inside GridView @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    100 def row_spec_to_cells(context, row_spec):
    101     """Iterates transforming each cell spec from
    102        (width, schema, number) to
    103        (id, additional_classes, position, width, html)
    104     """
    105     position = 0
    106     for (width, schema, number) in row_spec:
    107         boxmanager = BoxManager(schema)
    108         if getSecurityManager().checkPermission(permissions.ModifyPortalContent, context):
    109             additional_classes = 'editable-box'
    110         else:
    111             additional_classes = ''
    112         yield (boxmanager._box_key(number),
    113                additional_classes,
    114                position,
    115                width,
    116                boxmanager.html(context, number))
    117         position += width
     100    def __init__(self, *row_spec):
     101        try:
     102            for (width, template) in row_spec: pass
     103        except ValueError, e:
     104            e.args += row_spec
     105            raise
     106        self.row_spec = row_spec
    118107
    119 def row_html(context, row_spec):
    120     """Renders the html of one row.
    121     `row_spec` is a sequence of cell specs: [(width, schema, number), ...]
    122     """
    123     return ROW_TEMPLATE % ''.join(
    124         [CELL_TEMPLATE % cell for cell in row_spec_to_cells(context, row_spec)])
     108    def _cells(self, context):
     109        """Iterates transforming each cell spec from (width, template) to
     110           (position, width, rendered_html)
     111        """
     112        position = 0
     113        for (width, template) in self.row_spec:
     114            yield (position, width, template(context))
     115            position += width
     116
     117    def render(self, context):
     118        """Renders the html of one row.
     119        `row_spec` is a sequence of cell specs: [(width, schema, number), ...]
     120        """
     121        return self.ROW_TEMPLATE % ''.join(
     122            [self.CELL_TEMPLATE % cell for cell in self._cells(context)])
     123
     124
     125class Box(object):
     126
     127    BOX_TEMPLATE = '''
     128      <div id="%s"%s>%s
     129      </div>'''
     130
     131    def __init__(self, schema, number, permission=ModifyPortalContent):
     132        self.schema = schema
     133        self.number = number
     134        self.permission = permission
     135
     136    def __call__(self, context):
     137        boxmanager = BoxManager(self.schema)
     138        is_editable = getSecurityManager().checkPermission(self.permission, context)
     139        return self.BOX_TEMPLATE % (boxmanager._box_key(self.number),
     140                                    ' class ="editable-box"' if is_editable else '',
     141                                    boxmanager.html(context, self.number))
     142
    125143
    126144class GridView(grok.View):
     
    131149
    132150    def rows(self):
    133         for row_spec in self.grid:
    134             yield row_html(self.context, row_spec)
     151        for row in self.grid:
     152            yield row.render(self.context)
    135153
    136154################################################################
  • PortalInterlegis/produtos/il.portalinterlegis/trunk/src/il/portalinterlegis/browser/homes.py

    r6495 r6497  
     1# -*- coding: utf-8 -*-
    12from Products.CMFPlone.interfaces import IPloneSiteRoot
    23from five import grok
    34
    4 from boxes.manager import GridView
     5from boxes.interfaces import ISimpleBox, ICarousel, ICalendar, IAcompanheOInterlegis, IHighlight
     6from boxes.manager import DtRow, Box, GridView
    57from interfaces import \
    68     IComunidadeLegislativa, IInformacao, ICapacitacao, ITecnologia, IComunicacao
    7 from boxes.interfaces import \
    8      ISimpleBox, ICarousel, ICalendar, ITabsPilares, IAcompanheOInterlegis, IHighlight
    99
     10
     11class Tab(object):
     12
     13    def __init__(self, title, inner_title, *row_spec):
     14        self.title = title
     15        self.inner_title = inner_title
     16        self.row_spec = row_spec
     17
     18    def __call__(self, context):
     19        return 'TODO: TAB %s' % self.title
     20
     21class TabbedPane(object):
     22
     23    def __init__(self, *tabs):
     24        self.tabs = tabs
     25
     26    def __call__(self, context):
     27        return ''.join([tab(context) for tab in self.tabs])
     28
     29# These are meant for more legible grid definitions. Do not overuse.
    1030FULL = 16
     31___, _ = DtRow, Box
    1132
    1233class Home(GridView):
     
    1637
    1738    grid = [
    18         [(10, ICarousel, 1), (6, ICalendar, 1)],
    19         [(FULL, ITabsPilares, 1)],
    20         [(FULL, IAcompanheOInterlegis, 1)],
    21         [(4, IHighlight, 1), (4, ISimpleBox, 1), (4, ISimpleBox, 2), (4, ISimpleBox, 3)]
     39        ___((10, _(ICarousel, 1)), (6, _(ICalendar, 1)),),
     40        ___((FULL, TabbedPane(
     41            Tab(u'Informação',  u'Informação Legislativa',
     42                (4, _(IHighlight, 1)), (4, _(IHighlight, 1)), (4, _(IHighlight, 1)), (4, _(IHighlight, 1)),),
     43            Tab(u'Capacitação', u'Capacitação Legislativa',
     44                (4, _(IHighlight, 1)), (4, _(IHighlight, 1)), (4, _(IHighlight, 1)), (4, _(IHighlight, 1)),),
     45            Tab(u'Tecnologia',  u'Tecnologia Legislativa',
     46                (4, _(IHighlight, 1)), (4, _(IHighlight, 1)), (4, _(IHighlight, 1)), (4, _(IHighlight, 1)),),
     47            Tab(u'Comunicação', u'Comunicação Legislativa',
     48                (4, _(IHighlight, 1)), (4, _(IHighlight, 1)), (4, _(IHighlight, 1)), (4, _(IHighlight, 1)),),
     49            ))),
     50        ___((FULL, _(IAcompanheOInterlegis, 1)), ),
     51        ___((4, _(IHighlight, 1)), (4, _(ISimpleBox, 1)), (4, _(ISimpleBox, 2)), (4, _(ISimpleBox, 3)),),
    2252    ]
    2353
     
    2858
    2959    grid = [
    30         [(10, ICarousel, 1), (6, ICalendar, 1)],
    31         [(10, ICarousel, 2), (6, ICalendar, 2)],
    32         [(10, ICarousel, 3), (6, ICalendar, 3)],
     60        ___((10, _(ICarousel, 1)), (6, _(ICalendar, 1)),),
     61        ___((10, _(ICarousel, 2)), (6, _(ICalendar, 2)),),
     62        ___((10, _(ICarousel, 3)), (6, _(ICalendar, 3)),),
    3363    ]
    3464
     
    6292    grok.context(IComunicacao)
    6393    grok.require('zope2.View')
    64 
  • PortalInterlegis/produtos/il.portalinterlegis/trunk/src/il/portalinterlegis/tests/testunitboxes.py

    r6495 r6497  
    33from mock import patch
    44
    5 from il.portalinterlegis.browser.boxes.manager import BoxManager, row_html
     5from il.portalinterlegis.browser.boxes.manager import BoxManager, DtRow
    66from il.portalinterlegis.browser.boxes.interfaces import ISimpleBox
    77from mockutils import *
    88from itertools import count
    99
    10 _ = None # anything, doesn't really matter
     10_any_ = None # anything, doesn't really matter
    1111
    1212class TestUnitBoxes(unittest.TestCase):
     
    4848        </a>
    4949      </div>
    50         ''', boxmanager.html(_, _))
     50        ''', boxmanager.html(_any_, _any_))
    5151
    5252    def test_row(self):
    5353        context = object()
    54         with patch.object(BoxManager, 'html') as mock_html:
    55             with patch.object(BoxManager, '_box_key') as mock_box_key:
     54        def mock_template(c):
     55            # A template is just a callable. A function will do.
     56            def f(context):
     57                return "\n      %s" % (3*c)
     58            return f
    5659
    57                 @return_values(mock_html, ['\n      %s' % s for s in ['AAA', 'BBB', 'CCC', 'DDD']])
    58                 def side_effect(c, n):
    59                     self.assertIs(context, c)
    60 
    61                 numbers = count(1)
    62                 @return_values(mock_box_key, [1, 2, 3, 4])
    63                 def side_effect(n):
    64                     self.assertIs(numbers.next(), n)
    65 
    66                 self.assertMultiLineEqual('''
     60        self.assertMultiLineEqual('''
    6761  <div class="dt-row">
    68     <div id="1" class=" dt-cell dt-position-0 dt-width-1">
     62    <div class="dt-cell dt-position-0 dt-width-1">
    6963      AAA
    7064    </div>
    71     <div id="2" class=" dt-cell dt-position-1 dt-width-2">
     65    <div class="dt-cell dt-position-1 dt-width-2">
    7266      BBB
    7367    </div>
    74     <div id="3" class=" dt-cell dt-position-3 dt-width-3">
     68    <div class="dt-cell dt-position-3 dt-width-3">
    7569      CCC
    7670    </div>
    77     <div id="4" class=" dt-cell dt-position-6 dt-width-1">
     71    <div class="dt-cell dt-position-6 dt-width-1">
    7872      DDD
    7973    </div>
    8074  </div>
    81 ''', row_html(context, [(1, _, 1), (2, _, 2), (3, _, 3), (1, _, 4)] ))
     75''', DtRow((1, mock_template('A')),
     76           (2, mock_template('B')),
     77           (3, mock_template('C')),
     78           (1, mock_template('D'))).render(context))
    8279
    8380
Note: Veja TracChangeset para ajuda no uso do visualizador de conjunto de mudanças.
 

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