web/views/__init__.py
changeset 0 b97547f5f1fa
child 631 99f5852f8604
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 """Views/forms and actions for the CubicWeb web client
       
     2 
       
     3 :organization: Logilab
       
     4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     6 """
       
     7 __docformat__ = "restructuredtext en"
       
     8     
       
     9 from rql import nodes
       
    10 
       
    11 def need_table_view(rset, schema):
       
    12     """return True if we think that a table view is more appropriate than a
       
    13     list or primary view to display the given result set
       
    14     """
       
    15     rqlst = rset.syntax_tree()
       
    16     if len(rqlst.children) > 1:
       
    17         # UNION query, use a table
       
    18         return True
       
    19     selected = rqlst.children[0].selection
       
    20     try:
       
    21         mainvar = selected[0]
       
    22     except AttributeError:
       
    23         # not a variable ref, using table view is probably a good option
       
    24         return True
       
    25     if not (isinstance(mainvar, nodes.VariableRef) or
       
    26             (isinstance(mainvar, nodes.Constant) and mainvar.uid)):
       
    27         return True
       
    28     for i, etype in enumerate(rset.description[0][1:]):
       
    29         # etype may be None on outer join
       
    30         if etype is None:
       
    31             return True
       
    32         # check the selected index node is a VariableRef (else we
       
    33         # won't detect aggregate function
       
    34         if not isinstance(selected[i+1], nodes.VariableRef):
       
    35             return True
       
    36         # if this is not a final entity
       
    37         if not schema.eschema(etype).is_final():
       
    38             return True
       
    39         # if this is a final entity not linked to the main variable
       
    40         var = selected[i+1].variable
       
    41         for vref in var.references():
       
    42             rel = vref.relation()
       
    43             if rel is None:
       
    44                 continue
       
    45             if mainvar.is_equivalent(rel.children[0]):
       
    46                 break
       
    47         else:
       
    48             return True
       
    49     return False
       
    50 
       
    51 
       
    52 def vid_from_rset(req, rset, schema):
       
    53     """given a result set, return a view id"""
       
    54     if rset is None:
       
    55         return 'index'
       
    56     nb_rows = len(rset)
       
    57     # empty resultset
       
    58     if nb_rows == 0 :
       
    59         return 'noresult'
       
    60     # entity result set
       
    61     if not schema.eschema(rset.description[0][0]).is_final():
       
    62         if need_table_view(rset, schema):
       
    63             return 'table'
       
    64         if nb_rows == 1:
       
    65             if req.search_state[0] == 'normal':
       
    66                 return 'primary'
       
    67             return 'outofcontext-search'
       
    68         return 'list'
       
    69     return 'table'
       
    70 
       
    71 def linksearch_match(req, rset):
       
    72     """when searching an entity to create a relation, return True if entities in
       
    73     the given rset may be used as relation end
       
    74     """
       
    75     try:
       
    76         searchedtype = req.search_state[1][-1]
       
    77     except IndexError:
       
    78         return 0 # no searching for association
       
    79     for etype in rset.column_types(0):
       
    80         if etype != searchedtype:
       
    81             return 0
       
    82     return 1
       
    83     
       
    84 def linksearch_select_url(req, rset):
       
    85     """when searching an entity to create a relation, return an url to select
       
    86     entities in the given rset
       
    87     """
       
    88     req.add_js( ('cubicweb.ajax.js', 'cubicweb.edition.js') )
       
    89     target, link_eid, r_type, searchedtype = req.search_state[1]
       
    90     if target == 'subject':
       
    91         id_fmt = '%s:%s:%%s' % (link_eid, r_type)
       
    92     else:
       
    93         id_fmt = '%%s:%s:%s' % (r_type, link_eid)
       
    94     triplets = '-'.join(id_fmt % row[0] for row in rset.rows)
       
    95     return "javascript: selectForAssociation('%s', '%s');" % (triplets,
       
    96                                                               link_eid)