cubicweb/web/views/__init__.py
changeset 11057 0b59724cb3f2
parent 10688 fa29f3628a1b
child 11767 432f87a63057
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """Views, forms, actions... for the CubicWeb web client"""
       
    19 
       
    20 __docformat__ = "restructuredtext en"
       
    21 
       
    22 import os
       
    23 import sys
       
    24 import tempfile
       
    25 
       
    26 from six import add_metaclass
       
    27 
       
    28 from rql import nodes
       
    29 from logilab.mtconverter import xml_escape
       
    30 from logilab.common.deprecation import class_deprecated
       
    31 
       
    32 
       
    33 def need_table_view(rset, schema):
       
    34     """return True if we think that a table view is more appropriate than a
       
    35     list or primary view to display the given result set
       
    36     """
       
    37     rqlst = rset.syntax_tree()
       
    38     if len(rqlst.children) > 1:
       
    39         # UNION query, use a table
       
    40         return True
       
    41     selected = rqlst.children[0].selection
       
    42     try:
       
    43         mainvar = selected[0]
       
    44     except AttributeError:
       
    45         # not a variable ref, using table view is probably a good option
       
    46         return True
       
    47     if not (isinstance(mainvar, nodes.VariableRef) or
       
    48             (isinstance(mainvar, nodes.Constant) and mainvar.uid)):
       
    49         return True
       
    50     for i, etype in enumerate(rset.description[0][1:]):
       
    51         # etype may be None on outer join
       
    52         if etype is None:
       
    53             return True
       
    54         # check the selected index node is a VariableRef (else we
       
    55         # won't detect aggregate function
       
    56         if not isinstance(selected[i+1], nodes.VariableRef):
       
    57             return True
       
    58         # if this is not a final entity
       
    59         if not schema.eschema(etype).final:
       
    60             return True
       
    61         # if this is a final entity not linked to the main variable
       
    62         var = selected[i+1].variable
       
    63         for vref in var.references():
       
    64             rel = vref.relation()
       
    65             if rel is None:
       
    66                 continue
       
    67             if mainvar.is_equivalent(rel.children[0]):
       
    68                 break
       
    69         else:
       
    70             return True
       
    71     return False
       
    72 
       
    73 # FIXME: VID_BY_MIMETYPE is unfortunately a bit too naive since
       
    74 #        some browsers (e.g. FF2) send a bunch of mimetypes in
       
    75 #        the Accept header, for instance:
       
    76 #          text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,
       
    77 #          text/plain;q=0.8,image/png,*/*;q=0.5
       
    78 VID_BY_MIMETYPE = {
       
    79     #'text/xml': 'xml',
       
    80     # XXX rss, owl...
       
    81 }
       
    82 def vid_from_rset(req, rset, schema, check_table=True):
       
    83     """given a result set, return a view id"""
       
    84     if rset is None:
       
    85         return 'index'
       
    86     for mimetype in req.parse_accept_header('Accept'):
       
    87         if mimetype in VID_BY_MIMETYPE:
       
    88             return VID_BY_MIMETYPE[mimetype]
       
    89     nb_rows = len(rset)
       
    90     # empty resultset
       
    91     if nb_rows == 0:
       
    92         return 'noresult'
       
    93     # entity result set
       
    94     if not schema.eschema(rset.description[0][0]).final:
       
    95         if check_table and need_table_view(rset, schema):
       
    96             return 'table'
       
    97         if nb_rows == 1:
       
    98             if req.search_state[0] == 'normal':
       
    99                 return 'primary'
       
   100             return 'outofcontext-search'
       
   101         if len(rset.column_types(0)) == 1:
       
   102             return 'sameetypelist'
       
   103         return 'list'
       
   104     return 'table'
       
   105 
       
   106 
       
   107 def linksearch_select_url(req, rset):
       
   108     """when searching an entity to create a relation, return a URL to select
       
   109     entities in the given rset
       
   110     """
       
   111     req.add_js( ('cubicweb.ajax.js', 'cubicweb.edition.js') )
       
   112     target, eid, r_type, searchedtype = req.search_state[1]
       
   113     if target == 'subject':
       
   114         id_fmt = '%s:%s:%%s' % (eid, r_type)
       
   115     else:
       
   116         id_fmt = '%%s:%s:%s' % (r_type, eid)
       
   117     triplets = '-'.join(id_fmt % row[0] for row in rset.rows)
       
   118     return "javascript: selectForAssociation('%s', '%s');" % (triplets, eid)
       
   119 
       
   120 
       
   121 def add_etype_button(req, etype, csscls='addButton right', **urlkwargs):
       
   122     vreg = req.vreg
       
   123     eschema = vreg.schema.eschema(etype)
       
   124     if eschema.has_perm(req, 'add'):
       
   125         url = vreg['etypes'].etype_class(etype).cw_create_url(req, **urlkwargs)
       
   126         return u'<a href="%s" class="%s">%s</a>' % (
       
   127             xml_escape(url), csscls, req.__('New %s' % etype))
       
   128     return u''
       
   129 
       
   130 
       
   131 
       
   132 @add_metaclass(class_deprecated)
       
   133 class TmpFileViewMixin(object):
       
   134     __deprecation_warning__ = '[3.18] %(cls)s is deprecated'
       
   135     binary = True
       
   136     content_type = 'application/octet-stream'
       
   137     cache_max_age = 60*60*2 # stay in http cache for 2 hours by default
       
   138 
       
   139     def call(self):
       
   140         self.cell_call()
       
   141 
       
   142     def cell_call(self, row=0, col=0):
       
   143         self.cw_row, self.cw_col = row, col # in case one needs it
       
   144         fd, tmpfile = tempfile.mkstemp('.png')
       
   145         os.close(fd)
       
   146         self._generate(tmpfile)
       
   147         self.w(open(tmpfile, 'rb').read())
       
   148         os.unlink(tmpfile)