Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: iso-8859-1 -*-
- """
- MoinMoin - DataConverterWidget
- Convert MoinMoin.dataset dataset to DOM tree
- @copyright: 2002 Juergen Hermann <[email protected]>
- 2010 MoinMoin:DmitryAndreev
- @license: GNU GPL, see COPYING for details.
- """
- from MoinMoin.util.tree import moin_page
- from MoinMoin.widget import base
- from MoinMoin import wikiutil
- class DataConverterWidget(base.Widget):
- """
- MoinMoin.util.dataset to DOM tree table converter
- """
- def __init__(self, request, show_header=True, **kw):
- _ = request.getText
- base.Widget.__init__(self, request, **kw)
- self.data = None
- self.unqual_data_id = 'dbw.'
- self.data_id = request.formatter.qualify_id(self.unqual_data_id)
- # prefixed with __ are untranslated and to be used in the JS
- self._all = _('[all]')
- self.__all = '[all]'
- self._notempty = _('[not empty]')
- self.__notempty = '[notempty]'
- self._empty = _('[empty]')
- self.__empty = '[empty]'
- self._filter = _('filter')
- self.__filter = 'filter'
- self._show_header = show_header
- def setData(self, dataset):
- """ Sets the data for the browser (see MoinMoin.util.dataset).
- @param dataset: dataset containing either ascii, unicode or tuples.
- If a dataset entry contains a tuple then the first
- item in the tuple is displayed and the second item
- is used for autofilters.
- """
- self.data = dataset
- if dataset.data_id:
- self.unqual_data_id = 'dbw.%s.' % dataset.data_id
- self.data_id = self.request.formatter.qualify_id(self.unqual_data_id)
- def _name(self, elem):
- """ return name tag for a HTML element
- @param elem: element name, will be prefixed by data id
- """
- return 'name="%s%s"' % (self.data_id, elem)
- def _makeoption(self, item, selected, ntitem=None):
- """ create an option for a <select> form element
- @param item: string containing the item name to show
- @param selected: indicates whether the item should be default or not
- """
- if selected:
- selected = ' selected'
- else:
- selected = ''
- assert(isinstance(item, basestring))
- if ntitem is None:
- ntitem = item
- return '<option value="%s"%s>%s</option>' % (
- wikiutil.escape(ntitem, True),
- selected,
- wikiutil.escape(item))
- def _filteroptions(self, idx):
- """
- create options for all elements in the column
- given by idx
- """
- self.data.reset()
- row = self.data.next()
- # [empty] is a special already
- unique = ['']
- value = None
- name = '%sfilter%d' % (self.data_id, idx)
- if name in self.request.values:
- value = self.request.values.getlist(name)
- while row:
- option = row[idx]
- if isinstance(option, tuple):
- option = option[1]
- if not option in unique:
- unique.append(option)
- row = self.data.next()
- # fill in the empty field we left blank
- del unique[0]
- unique.sort()
- result = [self._makeoption(item, item == value) for item in unique]
- common = []
- common.append(self._makeoption(self._all, value == self.__all, self.__all))
- if '' in unique:
- common.extend([
- self._makeoption(self._empty, value == self.__empty, self.__empty),
- self._makeoption(self._notempty, value == self.__notempty, self.__notempty),
- ])
- return '\n'.join(common + result)
- def _format(self, formatter=None, method=None):
- """
- convert table from dataset to DOM tree
- @param formatter: formatter
- @param method: None is the default and does not create a form
- while "GET" or "POST" will create the form using the given method
- """
- fmt = formatter or self.request.formatter
- haveinput = False
- for col in self.data.columns:
- if col.autofilter:
- haveinput = True
- break
- if self._show_header:
- for idx in range(len(self.data.columns)):
- col = self.data.columns[idx]
- if col.hidden:
- continue
- cell_text = col.label or col.name
- selecttag = ''
- if col.autofilter:
- html_select = '<select %s onchange="dbw_update_search(\'%s\');">%s</select>' % \
- (self._name('filter%d' % idx), \
- self.data_id, \
- self._filteroptions(idx))
- selecttag = fmt.rawHTML(html_select)
- cells.append(moin_page.table_cell(\
- attrib={'class':'hcolumn%d' % idx},\
- children=(cell_text+select, )))
- moin_page.table_row(children=cells)
- self.data.reset()
- row = self.data.next()
- if row is not None:
- filters = [None] * len(row)
- if havefilters:
- for idx in range(len(row)):
- name = '%sfilter%d' % (self.data_id, idx)
- if name in self.request.values:
- filters[idx] = self.request.getlist(name)
- if filters[idx] == self._all:
- filters[idx] = None
- rows = []
- while row:
- hidden = False
- if havefilters:
- # check if row needs to be hidden due to filtering
- for idx in range(len(row)):
- if filters[idx]:
- if isinstance(row[idx], tuple):
- data = unicode(row[idx][1])
- else:
- data = unicode(row[idx])
- if data != '' and filters[idx] == self._notempty:
- continue
- if data == '' and filters[idx] == self._empty:
- continue
- if data != filters[idx]:
- hidden = True
- break
- if not hidden:
- #result.append(fmt.table_row(1))
- cells = []
- for idx in range(len(row)):
- if self.data.columns[idx].hidden:
- continue
- if isinstance(row[idx], tuple):
- cell = moin_page.abbr(attrib={'title':unicode(row[idx][1])},\
- children=(unicode(row[idx])[0], ))
- cells.append(moin_page.table_cell(\
- attrib={'class': 'column%d' % idx},\
- children=(cell, ))
- else:
- cells.append(moin_page.table_cell(\
- attrib={'class': 'column%d' % idx},\
- children=(unicode(row[idx]), ))
- rows.append(moin_page.table_row(children=cells)
- row = self.data.next()
- body = moin_page.table(children=rows)
- if haveinput:
- body = moin_page.input(attrib={'type':'submit','value':self._filter,\
- 'name':"%ssubmit" % data_id)}, chidren=(body, ))
- body = moin_page.div(children=(body, ))
- if method:
- body = moin_page.form(attrib={'action':"%s/%s" % \
- (self.request.script_root,\
- wikiutil.quoteWikinameURL(\
- self.request.page.page_name)),\
- 'method':method, 'name':"%sform" % data_id},\
- children=(body, ))
- return body
- convert = _format
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement