view.py
branchstable
changeset 7938 80c6e2155c3d
parent 7833 f19e3203dff6
child 7959 32349eb5d89d
equal deleted inserted replaced
7936:82d76492e21e 7938:80c6e2155c3d
    21 _ = unicode
    21 _ = unicode
    22 
    22 
    23 import types, new
    23 import types, new
    24 from cStringIO import StringIO
    24 from cStringIO import StringIO
    25 from warnings import warn
    25 from warnings import warn
       
    26 from functools import partial
    26 
    27 
    27 from logilab.common.deprecation import deprecated
    28 from logilab.common.deprecation import deprecated
    28 from logilab.mtconverter import xml_escape
    29 from logilab.mtconverter import xml_escape
    29 
    30 
    30 from rql import nodes
    31 from rql import nodes
   449     __select__ = nonempty_rset()
   450     __select__ = nonempty_rset()
   450 
   451 
   451     category = _('anyrsetview')
   452     category = _('anyrsetview')
   452 
   453 
   453     def columns_labels(self, mainindex=0, tr=True):
   454     def columns_labels(self, mainindex=0, tr=True):
       
   455         """compute the label of the rset colums
       
   456 
       
   457         The logic is based on :meth:`~rql.stmts.Union.get_description`.
       
   458 
       
   459         :param mainindex: The index of the main variable. This is an hint to get
       
   460                           more accurate label for various situation
       
   461         :type mainindex:  int
       
   462 
       
   463         :param tr: Should the label be translated ?
       
   464         :type tr: boolean
       
   465         """
   454         if tr:
   466         if tr:
   455             translate = lambda val, req=self._cw: display_name(req, val)
   467             translate = partial(display_name, self._cw)
   456         else:
   468         else:
   457             translate = lambda val: val
   469             translate = lambda val: val
   458         # XXX [0] because of missing Union support
   470         # XXX [0] because of missing Union support
   459         rqlstdescr = self.cw_rset.syntax_tree().get_description(mainindex,
   471         rql_syntax_tree = self.cw_rset.syntax_tree()
   460                                                                 translate)[0]
   472         rqlstdescr = rql_syntax_tree.get_description(mainindex)[0]
   461         labels = []
   473         labels = []
   462         for colidx, label in enumerate(rqlstdescr):
   474         for colidx, label in enumerate(rqlstdescr):
   463             try:
   475             labels.append(self.column_label(colidx, label, translate))
   464                 label = getattr(self, 'label_column_%s' % colidx)()
       
   465             except AttributeError:
       
   466                 # compute column header
       
   467                 if label == 'Any': # find a better label
       
   468                     label = ','.join(translate(et)
       
   469                                      for et in self.cw_rset.column_types(colidx))
       
   470             labels.append(label)
       
   471         return labels
   476         return labels
       
   477 
       
   478     def column_label(self, colidx, default, translate_func=None):
       
   479         """return the label of a specified columns index
       
   480 
       
   481         Overwrite me if you need to compute specific label.
       
   482 
       
   483         :param colidx: The index of the column the call computes a label for.
       
   484         :type colidx:  int
       
   485 
       
   486         :param default: Default value. If ``"Any"`` the default value will be
       
   487                         recomputed as coma separated list for all possible
       
   488                         etypes name.
       
   489         :type colidx:  string
       
   490 
       
   491         :param translate_func: A function used to translate name.
       
   492         :type colidx:  function
       
   493         """
       
   494         label = default
       
   495         if label == 'Any':
       
   496             etypes = self.cw_rset.column_types(colidx)
       
   497             if translate_func is not None:
       
   498                 etypes = map(translate_func, etypes)
       
   499             label = ','.join(etypes)
       
   500         return label
       
   501 
   472 
   502 
   473 
   503 
   474 # concrete template base classes ##############################################
   504 # concrete template base classes ##############################################
   475 
   505 
   476 class MainTemplate(View):
   506 class MainTemplate(View):