class ContainerIndex(uvclight.Page): uvclight.name('index') uvclight.layer(IAdminLayer) uvclight.context(IModelContainer) require('manage.vouchers') template = uvclight.get_template('container.cpt', __file__) batching = uvclight.get_template('batch.pt', __file__) def listing(self, item): details = inspect(item) relations = details.mapper.relationships.keys() itemurl = self.url(item) for col in self.context.listing_attrs: value = getattr(item, col.identifier, col.defaultValue) if col.identifier in relations: relation = self.relation(col.identifier, value) yield col.identifier, [{ 'value': rel.title, 'link': link} for rel, link in relation] else: yield col.identifier, [{ 'value': value, 'link': col.identifier == 'oid' and itemurl or ''}] def batch_elements(self): return list(self.context) def set_batch(self): self.batcher = Batcher(self.context, self.request, size=50) elements = self.batch_elements() if elements: self.batcher.update(elements) self.dichotomy = get_dichotomy_batches( self.batcher.batch.batches, self.batcher.batch.total, self.batcher.batch.number) self.batch = self.batching.render( self, **self.namespace()) else: self.batch = u'' def update(self): ukhvouchers.need() ukhcss.need() self.columns = [field.title for field in self.context.listing_attrs] self.set_batch() def relation(self, id, value): site = getSite() if id == 'vouchers': baseurl = self.url(site) + '/vouchers/' for voucher in value: yield voucher, baseurl + str(voucher.oid)
def test_batch_multi_param(): sequence = range(10) class Publishable(Location): implements(IPublicationRoot) root = Publishable() request = TestRequest( form={'batch.size': 2, 'batch.start': 4, 'x':[u'a', u'b']}) batcher = Batcher(root, request) batcher.update(sequence) assert ('http://localhost?x=a&x=b&batch.start=0&batch.size=2' in batcher.render())
def test_batch_unicode_param(): sequence = range(10) @implementer(IPublicationRoot) class Publishable(Location): pass root = Publishable() request = Request(form={'batch.size': 2, 'batch.start': 4, 'x': u'héhô'}) batcher = Batcher(root, request) batcher.update(sequence) content = batcher.render() # must not raise # assert page param is url encoded assert 'http://localhost?x=h%C3%A9h%C3%B4' in content
def test_batch_unicode_param(): sequence = range(10) class Publishable(Location): implements(IPublicationRoot) root = Publishable() request = TestRequest( form={'batch.size': 2, 'batch.start': 4, 'x':u'héhô'}) batcher = Batcher(root, request) batcher.update(sequence) content = batcher.render() # must not raise # assert page param is url encoded assert 'http://localhost?x=h%C3%A9h%C3%B4' in content
def test_batch_multi_param(): sequence = range(10) @implementer(IPublicationRoot) class Publishable(Location): pass root = Publishable() request = Request(form={ 'batch.size': 2, 'batch.start': 4, 'x': [u'a', u'b'] }) batcher = Batcher(root, request) batcher.update(sequence) assert ('http://localhost?x=a&x=b&batch.start=0&batch.size=2' in batcher.render())
def update_batch(self): if self.createBatch: content = self.getContentData().getContent() if not ILocation.providedBy(content): content = LocationProxy(content) content.__parent__ = self content.__name__ = '' self.batcher = Batcher( content, self.request, self.prefix, size=self.batchSize) self.batcher.update(self.lines)
def set_batch(self): self.batcher = Batcher(self.context, self.request, size=50) elements = self.batch_elements() if elements: self.batcher.update(elements) self.dichotomy = get_dichotomy_batches( self.batcher.batch.batches, self.batcher.batch.total, self.batcher.batch.number) self.batch = self.batching.render( self, **self.namespace()) else: self.batch = u''
def test_batch(): sequence = [1, 3, 5, 7, 9, 11, 13, 15] class Publishable(Location): implements(IPublicationRoot) root = Publishable() request = TestRequest() batcher = Batcher(root, request) batcher.update(sequence) assert not XMLDiff(batcher.render(), NO_BATCH) request = TestRequest(form={'batch.size': 2}) batcher = Batcher(root, request) batcher.update(sequence) assert not XMLDiff(batcher.render(), BATCHED) request = TestRequest(form={'batch.size': 2, 'batch.start': 4}) batcher = Batcher(root, request) batcher.update(sequence) assert not XMLDiff(batcher.render(), BATCHED_ADV)
def test_batch(): sequence = [1, 3, 5, 7, 9, 11, 13, 15] @implementer(IPublicationRoot) class Publishable(Location): pass root = Publishable() request = Request() batcher = Batcher(root, request) batcher.update(sequence) assert not XMLDiff(batcher.render(), NO_BATCH) request = Request(form={'batch.size': 2}) batcher = Batcher(root, request) batcher.update(sequence) assert not XMLDiff(batcher.render(), BATCHED) request = Request(form={'batch.size': 2, 'batch.start': 4}) batcher = Batcher(root, request) batcher.update(sequence) assert not XMLDiff(batcher.render(), BATCHED_ADV)
class BaseTable(FormData): """Action-less table component (mainly for display) """ mode = DISPLAY fields = Fields() template = get_template('table.pt') label = u"" description = u"" css_id = None css_class = None batchSize = 10 createBatch = False tableFields = Fields() ignoreContent = False ignoreRequest = True _updated = False emptyDescription = _(u"There are no items.") def __init__(self, context, request): super(BaseTable, self).__init__(context, request) self.fieldWidgets = Widgets(form=self, request=self.request) self.lines = [] self.lineWidgets = [] self.batcher = None @property def tableDataManager(self): """By default tableDataManager is dataManager but you may override it""" return self.dataManager @property def target_language(self): return ILanguage(self.request, None) @property def title(self): return (u"<h1>%s</h1>" % self.label) or '' def getItems(self): return self.context.values() def items(self): if self.createBatch: self.batcher.update(self.getItems()) return self.batcher.batch return self.getItems() def namespace(self): namespace = {} namespace['view'] = namespace['table'] = self namespace['table_fields'] = self.tableFields namespace['lines'] = self.lineWidgets namespace['fields'] = self.fieldWidgets return namespace def generate_line_form(self, content, prefix): """ Generate form for a specific line, using content. """ form = cloneFormData(self, content=content, prefix=prefix) # TODO : if cloneFormData would copy dataValidators # and accept kwargs to override eg. dataManager # this would not happen, fix it in forms.base! form.dataManager = self.tableDataManager form.dataValidators = self.dataValidators form.setContentData(content) # context is content so that vocabularies work ! if not ILocation.providedBy(content): content = LocationProxy(content) form.context = content if getattr(content, '__parent__', None) is None: setattr(content, '__parent__', form) form.__parent__ = self return form def updateLines(self): self.lines = [] self.lineWidgets = [] for position, item in enumerate(self.items()): prefix = u'%s.line-%d' % (self.prefix, position) form = self.generate_line_form(content=item, prefix=prefix) lineWidget = Widgets(form=form, request=self.request) self.lines.append(form) self.lineWidgets.append(lineWidget) def updateWidgets(self): for widgets in self.lineWidgets: widgets.extend(self.tableFields) self.fieldWidgets.extend(self.fields) for widgets in self.lineWidgets: widgets.update() self.fieldWidgets.update() def update(self, *args, **kwargs): pass def update_batch(self): if self.createBatch: content = self.getContentData().getContent() if not ILocation.providedBy(content): content = LocationProxy(content) content.__parent__ = self content.__name__ = '' self.batcher = Batcher( content, self.request, self.prefix, size=self.batchSize) self.batcher.update(self.lines) def updateForm(self, *args, **kwargs): if self._updated is False: self.updateLines() self.updateWidgets() self.update_batch() self._updated = True def render(self, *args, **kwargs): html = HTMLWrapper() res = self.template.render( self, target_language=self.target_language, **self.namespace()) return html(res.encode('utf-8'))