Beispiel #1
0
 def extractLink(self, model, item):
     webTranslator = IWebTranslator(item.store, None)
     if webTranslator is not None:
         return unicode(webTranslator.toWebID(item), 'ascii')
     return None
Beispiel #2
0
class QueryList(ThemedElement):
    """
    A widget that displays data tabulated according to a set of columns.

    Actions are supported too.
    """
    jsClass = u'Methanal.Widgets.QueryList'
    fragmentName = 'methanal-table'

    def __init__(self, rows, columns, webTranslator=None, timezone=None, **kw):
        warn('QueryList is deprecated, use methanal.widgets.Table instead')
        super(QueryList, self).__init__(**kw)

        self.rows = list(rows)
        def _adapt(col):
            try:
                return IColumn(col)
            except TypeError:
                col = mantissaIColumn(col)

            warn('use methanal.imethanal.IColumn instead of '
                 'xmantissa.ixmantissa.IColumn', DeprecationWarning, 2)
            return col

        columns = (_adapt(col) for col in columns)
        self.columns = [(col.attributeID.decode('ascii'), col)
                        for col in columns]
        self.webTranslator = webTranslator

        if timezone is None:
            hour, minute = divmod(time.timezone, -3600)
            timezone = FixedOffset(hour, minute)
            warn('not passing in timezone is deprecated', DeprecationWarning, 2)

        self.timezone = timezone


    def dictifyItem(self, item, index):
        def _formatValue(value):
            if isinstance(value, Time):
                return value.asDatetime(self.timezone).strftime(
                    '%a, %d %h %Y %H:%M:%S').decode('ascii')
            return value

        if isinstance(item, tuple):
            link, item = item
        else:
            if self.webTranslator is None:
                self.webTranslator = IWebTranslator(item.store)
            link = unicode(self.webTranslator.toWebID(item), 'ascii')

        d = dict((cid, _formatValue(col.extractValue(self, item)))
                 for (cid, col) in self.columns)
        d[u'__link__'] = link
        d[u'__id__'] = index

        return d


    def getInitialArguments(self):
        return [getArgsDict(self)]


    def getArgs(self):
        IDs = []
        aliases = {}
        for cid, col in self.columns:
            IDs.append(cid)
            if isinstance(col, AttributeColumn):
                alias = col.attribute.doc
            else:
                alias = col
            aliases[cid] = unicode(alias)

        return {u'columnIDs':     IDs,
                u'columnAliases': aliases,
                u'rows':          [self.dictifyItem(row, i)
                                   for i, row in enumerate(self.rows)]}


    @expose
    def performAction(self, name, rowIndex):
        method = getattr(self, 'action_' + name)
        item = self.rows[rowIndex]
        return method(item)