Conjunto de mudanças 6471 no repositório publico


Ignorar:
Timestamp:
09/06/2012 16:43:29 (9 anos atrás)
Autor:
mazza
Mensagem:

boxes: refatoracao para uma classe

Localização:
PortalInterlegis/produtos/il.portalinterlegis/src/il/portalinterlegis
Arquivos:
1 editado
2 movidos

Legenda:

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

    r6467 r6471  
    11from Products.CMFCore.interfaces import IFolderish
    22from five import grok
    3 from persistent.dict import PersistentDict 
     3from persistent.dict import PersistentDict
    44from z3c.form import form, field, datamanager
    55from z3c.form.interfaces import IDataManager
     
    88from zope.interface import implements
    99from zope.schema.interfaces import IField
    10 
     10from zope.interface import Interface
    1111
    1212class PersistentDictionaryField(datamanager.DictionaryField):
     
    1515provideAdapter(PersistentDictionaryField)
    1616
    17 ALL_BOXES_KEY = 'il.portalinterlegis.boxes'
    18 def _build_box(box_schema, number):
     17class BoxManager(object):
    1918
    20     form_label = getattr(box_schema, 'form_label', None) \
    21       or u'Edite os valore desta caixa'
    22    
    23     # the combination (form.EditForm, grok.View)
    24     # is from https://mail.zope.org/pipermail/grok-dev/2008-July/005999.html
    25     # (plone.directives.form.EditForm did not work well)
    26     class BoxEditForm(form.EditForm, grok.View):
    27         grok.context(IFolderish)
    28         grok.name(_box_name_for_url(box_schema, number))
    29         label = form_label
    30         fields = field.Fields(box_schema)
    31      
    32         def getContent(self):
    33             return box_content(self.context, box_schema, number)
    34        
    35     globals()['BoxEditForm_%s' % _box_key(box_schema, number)] = BoxEditForm
    36     return BoxEditForm
     19    ALL_BOXES_KEY = 'il.portalinterlegis.boxes'
    3720
    38 def box_content(context, box_schema, number):
    39     annotations = IAnnotations(context)
    40     boxes = _get_persistent_dict(annotations, ALL_BOXES_KEY)
    41     return _get_persistent_dict(boxes, _box_key(box_schema, number))
     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'
    4225
    43 def _get_persistent_dict(dictionary, key):
     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 _box_key(self, number):
     53        return '%s_%s' % (self.schema.__name__, number)
     54
     55    def _box_name_for_url(self, number):
     56        return 'box_%s' % self._box_key(number)
     57
     58
     59def get_or_create_persistent_dict(dictionary, key):
    4460    value = dictionary.get(key, None)
    4561    if not value:
     
    4763    return value
    4864
    49 def _box_key(box_schema, number):
    50     return '%s_%s' % (box_schema.__name__, number)
     65################################################################
     66from interfaces import ISimpleBox
    5167
    52 def _box_name_for_url(box_schema, number):
    53     return 'box_%s' % _box_key(box_schema, number)
    54 
    55 def build_n_boxes(box_schema, max_number):
    56     for number in range(1, max_number+1):
    57         _build_box(box_schema, number)
    58 
    59 ################################################################       
    60 from interfaces import ISimpleBox
    61    
    62 for box_schema in [ISimpleBox]:
    63     build_n_boxes(box_schema, 2)
     68# initialize all the boxes
     69for s in [ISimpleBox,]:
     70    BoxManager(s).build_n_forms(10)
  • PortalInterlegis/produtos/il.portalinterlegis/src/il/portalinterlegis/tests/testintegration.py

    r6470 r6471  
    33import unittest2 as unittest
    44from Products.CMFCore.utils import getToolByName
     5from fixtures import IL_PORTALINTERLEGIS_INTEGRATION_TESTING
    56from plone.testing.z2 import Browser
     7from zExceptions import NotFound
    68
    7 from fixtures import IL_PORTALINTERLEGIS_INTEGRATION_TESTING
    8 from il.portalinterlegis.browser.boxes import _box_name_for_url, box_content
     9from il.portalinterlegis.browser.boxes import BoxManager
    910from il.portalinterlegis.browser.interfaces import \
    1011     IComunidadeLegislativa, IInformacao, ICapacitacao, ITecnologia, IComunicacao, \
    1112     ISimpleBox
    12 import zExceptions
     13
    1314
    1415class TestIntegracao(unittest.TestCase):
     
    1920        self.portal = self.layer['portal']
    2021        self.qi_tool = getToolByName(self.portal, 'portal_quickinstaller')
    21    
     22
    2223    def browser(self, path=None):
    2324        browser = Browser(self.app)
    2425        browser.handleErrors = False
    2526        browser.open(self.url(path))
    26         return browser 
     27        return browser
    2728
    2829    def dom(self, browser):
    2930        return lxml.html.fromstring(browser.contents)
    30        
     31
    3132    def url(self, path=None):
    3233        base = self.portal.absolute_url()
     
    3738
    3839    def test_product_is_installed(self):
    39         """ Validate that our products GS profile has been run and the product 
     40        """ Validate that our products GS profile has been run and the product
    4041            installed
    4142        """
     
    5758
    5859    def test_homes_marcadas_e_com_layout_home(self):
    59         for id, marker in [('comunidade-legislativa', IComunidadeLegislativa), 
    60                            ('informacao',          IInformacao), 
    61                            ('capacitacao',         ICapacitacao), 
     60        for id, marker in [('comunidade-legislativa', IComunidadeLegislativa),
     61                           ('informacao',          IInformacao),
     62                           ('capacitacao',         ICapacitacao),
    6263                           ('tecnologia',          ITecnologia),
    63                            ('comunicacao',         IComunicacao), 
     64                           ('comunicacao',         IComunicacao),
    6465                           ]:
    6566            obj = self.portal[id]
     
    6869
    6970    def test_box_content_is_empty_before_visiting_form(self):
    70         self.assertEqual({}, box_content(self.portal, ISimpleBox, 1))
     71        self.assertEqual({}, BoxManager(ISimpleBox).box_content(self.portal, 1))
    7172
    7273    def test_using_box_form_creates_box_content(self):
    73         box_schema = ISimpleBox
    7474        context = self.portal
     75        boxmanager = BoxManager(ISimpleBox)
    7576
    7677        def use_box_form(title, subtitle, text, num):
    77             browser = self.browser(_box_name_for_url(box_schema, num))
     78            browser = self.browser(boxmanager._box_name_for_url(num))
    7879            browser.getControl(name='form.widgets.title').value = title
    7980            browser.getControl(name='form.widgets.subtitle').value = subtitle
     
    8485        use_box_form('TIT_2', 'SUBTIT_2', 'TEXT_2', 2)
    8586        self.assertEqual({'title': 'TIT_1', 'subtitle': 'SUBTIT_1', 'text': 'TEXT_1'},
    86                          box_content(context, box_schema, 1))
     87                         boxmanager.box_content(context, 1))
    8788        self.assertEqual({'title': 'TIT_2', 'subtitle': 'SUBTIT_2', 'text': 'TEXT_2'},
    88                          box_content(context, box_schema, 2))
    89        
     89                         boxmanager.box_content(context, 2))
     90
    9091    def test_box_forms_numbers_begin_from_1_not_zero(self):
    91         with self.assertRaises(zExceptions.NotFound):
    92             browser = self.browser(_box_name_for_url(ISimpleBox, 0))
     92        with self.assertRaises(NotFound):
     93            browser = self.browser(BoxManager(ISimpleBox)._box_name_for_url(0))
    9394
    9495    def test_box_forms_are_limited(self):
    95         with self.assertRaises(zExceptions.NotFound):
    96             browser = self.browser(_box_name_for_url(ISimpleBox, 1000000))
     96        with self.assertRaises(NotFound):
     97            browser = self.browser(BoxManager(ISimpleBox)._box_name_for_url(1000000))
    9798
    98            
     99    def test_box_form_cannot_be_created_after_initialization(self):
     100        BoxManager(ISimpleBox).build_form(99) # try to build a box form in an arbitrary moment
     101        with self.assertRaises(NotFound):
     102            browser = self.browser(BoxManager(ISimpleBox)._box_name_for_url(99))
     103
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.