Advertisement
Guest User

DBW with DOM support

a guest
May 20th, 2010
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.21 KB | None | 0 0
  1. # -*- coding: iso-8859-1 -*-
  2. """
  3.    MoinMoin - DataConverterWidget
  4.  
  5.    Convert MoinMoin.dataset dataset to DOM tree
  6.    
  7.    @copyright: 2002 Juergen Hermann <[email protected]>
  8.                2010 MoinMoin:DmitryAndreev
  9.    @license: GNU GPL, see COPYING for details.
  10. """
  11.  
  12. from MoinMoin.util.tree import moin_page
  13. from MoinMoin.widget import base
  14. from MoinMoin import wikiutil
  15.  
  16. class DataConverterWidget(base.Widget):
  17.     """
  18.    MoinMoin.util.dataset to DOM tree table converter
  19.    """
  20.  
  21.     def __init__(self, request, show_header=True, **kw):
  22.         _ = request.getText
  23.         base.Widget.__init__(self, request, **kw)
  24.         self.data = None
  25.         self.unqual_data_id = 'dbw.'
  26.         self.data_id = request.formatter.qualify_id(self.unqual_data_id)
  27.         # prefixed with __ are untranslated and to be used in the JS
  28.         self._all = _('[all]')
  29.         self.__all = '[all]'
  30.         self._notempty = _('[not empty]')
  31.         self.__notempty = '[notempty]'
  32.         self._empty = _('[empty]')
  33.         self.__empty = '[empty]'
  34.         self._filter = _('filter')
  35.         self.__filter = 'filter'
  36.         self._show_header = show_header
  37.  
  38.     def setData(self, dataset):
  39.         """ Sets the data for the browser (see MoinMoin.util.dataset).
  40.  
  41.        @param dataset: dataset containing either ascii, unicode or tuples.
  42.                        If a dataset entry contains a tuple then the first
  43.                        item in the tuple is displayed and the second item
  44.                        is used for autofilters.
  45.        """
  46.         self.data = dataset
  47.         if dataset.data_id:
  48.             self.unqual_data_id = 'dbw.%s.' % dataset.data_id
  49.             self.data_id = self.request.formatter.qualify_id(self.unqual_data_id)
  50.  
  51.     def _name(self, elem):
  52.         """ return name tag for a HTML element
  53.        @param elem: element name, will be prefixed by data id
  54.        """
  55.         return 'name="%s%s"' % (self.data_id, elem)
  56.  
  57.     def _makeoption(self, item, selected, ntitem=None):
  58.         """ create an option for a <select> form element
  59.        @param item: string containing the item name to show
  60.        @param selected: indicates whether the item should be default or not
  61.        """
  62.         if selected:
  63.             selected = ' selected'
  64.         else:
  65.             selected = ''
  66.         assert(isinstance(item, basestring))
  67.         if ntitem is None:
  68.             ntitem = item
  69.         return '<option value="%s"%s>%s</option>' % (
  70.             wikiutil.escape(ntitem, True),
  71.             selected,
  72.             wikiutil.escape(item))
  73.  
  74.     def _filteroptions(self, idx):
  75.         """
  76.        create options for all elements in the column
  77.        given by idx
  78.        """
  79.         self.data.reset()
  80.         row = self.data.next()
  81.         # [empty] is a special already
  82.         unique = ['']
  83.  
  84.         value = None
  85.         name = '%sfilter%d' % (self.data_id, idx)
  86.         if name in self.request.values:
  87.             value = self.request.values.getlist(name)
  88.         while row:
  89.             option = row[idx]
  90.             if isinstance(option, tuple):
  91.                 option = option[1]
  92.             if not option in unique:
  93.                 unique.append(option)
  94.             row = self.data.next()
  95.  
  96.         # fill in the empty field we left blank
  97.         del unique[0]
  98.         unique.sort()
  99.         result = [self._makeoption(item, item == value) for item in unique]
  100.         common = []
  101.         common.append(self._makeoption(self._all, value == self.__all, self.__all))
  102.         if '' in unique:
  103.             common.extend([
  104.                 self._makeoption(self._empty, value == self.__empty, self.__empty),
  105.                 self._makeoption(self._notempty, value == self.__notempty, self.__notempty),
  106.             ])
  107.         return '\n'.join(common + result)
  108.  
  109.     def _format(self, formatter=None, method=None):
  110.         """
  111.        convert table from dataset to DOM tree
  112.  
  113.        @param formatter: formatter
  114.        @param method: None is the default and does not create a form
  115.                       while "GET" or "POST" will create the form using the given method
  116.        """
  117.         fmt = formatter or self.request.formatter
  118.  
  119.         haveinput = False
  120.         for col in self.data.columns:
  121.             if col.autofilter:
  122.                 haveinput = True
  123.                 break
  124.  
  125.         if self._show_header:
  126.             for idx in range(len(self.data.columns)):
  127.                 col = self.data.columns[idx]
  128.                 if col.hidden:
  129.                     continue
  130.                 cell_text = col.label or col.name  
  131.                 selecttag = ''
  132.                 if col.autofilter:
  133.                     html_select = '<select %s onchange="dbw_update_search(\'%s\');">%s</select>' % \
  134.                                     (self._name('filter%d' % idx), \
  135.                                      self.data_id, \
  136.                                      self._filteroptions(idx))
  137.                     selecttag = fmt.rawHTML(html_select)
  138.                
  139.                 cells.append(moin_page.table_cell(\
  140.                                         attrib={'class':'hcolumn%d' % idx},\
  141.                                         children=(cell_text+select, )))
  142.             moin_page.table_row(children=cells)
  143.  
  144.         self.data.reset()
  145.         row = self.data.next()
  146.         if row is not None:
  147.             filters = [None] * len(row)
  148.  
  149.             if havefilters:
  150.                 for idx in range(len(row)):
  151.                     name = '%sfilter%d' % (self.data_id, idx)
  152.                     if name in self.request.values:
  153.                         filters[idx] = self.request.getlist(name)
  154.                         if filters[idx] == self._all:
  155.                             filters[idx] = None
  156.         rows = []
  157.         while row:
  158.             hidden = False
  159.             if havefilters:
  160.                 # check if row needs to be hidden due to filtering
  161.                 for idx in range(len(row)):
  162.                     if filters[idx]:
  163.                         if isinstance(row[idx], tuple):
  164.                             data = unicode(row[idx][1])
  165.                         else:
  166.                             data = unicode(row[idx])
  167.                         if data != '' and filters[idx] == self._notempty:
  168.                             continue
  169.                         if data == '' and filters[idx] == self._empty:
  170.                             continue
  171.                         if data != filters[idx]:
  172.                             hidden = True
  173.                             break
  174.  
  175.             if not hidden:
  176.                 #result.append(fmt.table_row(1))
  177.                 cells = []
  178.                 for idx in range(len(row)):
  179.                    
  180.                     if self.data.columns[idx].hidden:
  181.                         continue
  182.                     if isinstance(row[idx], tuple):
  183.                         cell = moin_page.abbr(attrib={'title':unicode(row[idx][1])},\
  184.                                         children=(unicode(row[idx])[0], ))
  185.                         cells.append(moin_page.table_cell(\
  186.                                         attrib={'class': 'column%d' % idx},\
  187.                                         children=(cell, ))
  188.                     else:
  189.                         cells.append(moin_page.table_cell(\
  190.                                         attrib={'class': 'column%d' % idx},\
  191.                                         children=(unicode(row[idx]), ))
  192.                 rows.append(moin_page.table_row(children=cells)
  193.             row = self.data.next()
  194.  
  195.         body = moin_page.table(children=rows)
  196.         if haveinput:
  197.             body = moin_page.input(attrib={'type':'submit','value':self._filter,\
  198.                                 'name':"%ssubmit" % data_id)}, chidren=(body, ))
  199.         body = moin_page.div(children=(body, ))
  200.  
  201.         if method:
  202.             body = moin_page.form(attrib={'action':"%s/%s" % \
  203.                                 (self.request.script_root,\
  204.                                     wikiutil.quoteWikinameURL(\
  205.                                         self.request.page.page_name)),\
  206.                                     'method':method, 'name':"%sform" % data_id},\
  207.                                 children=(body, ))
  208.         return body
  209.  
  210.     convert = _format
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement