[Cubicweb] struggling with table view

Alexandre Fayolle alexandre.fayolle at logilab.fr
Fri Sep 30 20:26:21 CEST 2011


Working on a personnal project, I'm struggling with the 2 view classes for 
displaying tables, TableView and EntityAttributeTableView. 

Example at hand: I have a Compte entity which contains Transactions, and I 
want to display all the Transactions of given Compte in a table (in a tab). 
Some of the columns are plain attributes, and other are one-to-many relations 
to other entities (namely Destinataires and Achat), which I want to display as 
a list in the table cell, and one optional relation. 

If I want to use TableView, here's what I write:

class CompteTransactionTab(EntityView):
    __select__ = is_instance('Compte') & \
                 rql_condition('T compte X, T is Transaction')
    __regid__ = _('transactions_tab')
    title = _('Transactions')
    def cell_call(self, row, col):
        entity = self.cw_rset.complete_entity(row, col)
        rql = ('Any T, T, T, PE, D, PAG, DO, DR ORDERBY PAG '
               'WHERE T is Transaction, T compte C, C eid %(x)s, '
               'T prix_ensemble PE?, T pagination PAG, '
               'T date D, T date_ordre DO, T date_recette DR')
        rset = self._cw.execute(rql, {'x': entity.eid})
        self.wview('table', rset,
                   cellvids={1: 'transaction_achats', 
                             2: 'transaction_destinataires',
                             3: 'textincontext'},
                   headers=('transactions', 'achats', 'destinataires',
                            'prix_ensemble', 'date', 'pagination',
                            'date_ordre', 'date_recette'))

class TransactionAchatsView(EntityView):
    __select__ = one_line_rset & EntityView.__select__ & \
                 is_instance('Transaction')
    __regid__ = 'transaction_achats'
    def entity_call(self, entity):
        return self.wview('list', entity.related('achat'), 'null')

class TransactionDestinatairesView(EntityView):
    __select__ = one_line_rset & EntityView.__select__ & \
                 is_instance('Transaction')
    __regid__ = 'transaction_destinataires'
    def entity_call(self, entity):
        return self.wview('list', entity.related('destinataires'), 'null')


If I choose to use EntityAttributesTableView, here's what I get:

class CompteTransactionTab2(EntityView):
    __select__ = is_instance('Compte') & \
                 rql_condition('T compte X, T is Transaction')
    __regid__ = _('transactions_tab2')
    title = _('Transactions')
    def cell_call(self, row, col):
        entity = self.cw_rset.complete_entity(row, col)
        rql = entity.cw_related_rql('compte', 'object', ('Transaction',),)
        rset = self._cw.execute(rql, {'x': entity.eid})
        self.wview('myosotis.transaction.attributestableview', rset)

class TransactionTableView(EntityAttributesTableView):
    __select__ = EntityAttributesTableView.__select__ &\
                 is_instance('Transaction')
    __regid__ = 'myosotis.transaction.attributestableview'
    columns = ('transaction', 'achats', 'destinataires', 
               'prix', 'date', 'pagination', 'date_ordre', 
               'date_recette', )
    title = 'transaction table'
    def build_transaction_cell(self, entity):
        return entity.view('incontext')

    def build_achats_cell(self, entity):
        return self._cw.view('list', entity.related('achat'), 'null')

    def build_prix_cell(self, entity):
        return self._cw.view('textincontext', 
                             entity.related('prix_ensemble'), 
                             'null')

    def build_destinataires_cell(self, entity):
        return self._cw.view('list', entity.related('destinataires'), 'null')

The speed of both versions is similar (and quite slow: about 13s for 200 rows 
in the tables), the rendering is very similar, the total size of the code is 
similar

Main differences: 

* I get a reusable view with EntityAttributesTableView which can be displayed 
in the possible views for any query returning Transactions
* I tend to find the EntityAttributesTableView version easier to understand
* I get sortable cols with TableView
* The query I need to write in the TableView version feels awkward, with T 
being selected 3 times
* with TableView, I need to keep in sync the query, the header tuple and the 
cellvids dictionary, which can be a burden when maintaining the application

So, all in all I'm tempted to use the EntityAttributesTableView. Yet this view 
is almost never used. Am I missing something, such as a better way of using 
TableView? 

Thanks in advance

-- 
Alexandre Fayolle                              LOGILAB, Paris (France)
Formations Python, CubicWeb, Debian :  http://www.logilab.fr/formations
Développement logiciel sur mesure :      http://www.logilab.fr/services
Informatique scientifique:               http://www.logilab.fr/science
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubicweb.org/pipermail/cubicweb/attachments/20110930/7adaa333/attachment-0185.html>


More information about the Cubicweb mailing list