[facet] closes #1806932: test and fix facet bug w/ having query (need rql update)
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr## This file is part of CubicWeb.## CubicWeb is free software: you can redistribute it and/or modify it under the# terms of the GNU Lesser General Public License as published by the Free# Software Foundation, either version 2.1 of the License, or (at your option)# any later version.## CubicWeb is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more# details.## You should have received a copy of the GNU Lesser General Public License along# with CubicWeb. If not, see <http://www.gnu.org/licenses/>."""Views, forms, actions... for the CubicWeb web client"""__docformat__="restructuredtext en"importosimportsysimporttempfilefromrqlimportnodesdefneed_table_view(rset,schema):"""return True if we think that a table view is more appropriate than a list or primary view to display the given result set """rqlst=rset.syntax_tree()iflen(rqlst.children)>1:# UNION query, use a tablereturnTrueselected=rqlst.children[0].selectiontry:mainvar=selected[0]exceptAttributeError:# not a variable ref, using table view is probably a good optionreturnTrueifnot(isinstance(mainvar,nodes.VariableRef)or(isinstance(mainvar,nodes.Constant)andmainvar.uid)):returnTruefori,etypeinenumerate(rset.description[0][1:]):# etype may be None on outer joinifetypeisNone:returnTrue# check the selected index node is a VariableRef (else we# won't detect aggregate functionifnotisinstance(selected[i+1],nodes.VariableRef):returnTrue# if this is not a final entityifnotschema.eschema(etype).final:returnTrue# if this is a final entity not linked to the main variablevar=selected[i+1].variableforvrefinvar.references():rel=vref.relation()ifrelisNone:continueifmainvar.is_equivalent(rel.children[0]):breakelse:returnTruereturnFalse# FIXME: VID_BY_MIMETYPE is unfortunately a bit too naive since# some browsers (e.g. FF2) send a bunch of mimetypes in# the Accept header, for instance:# text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,# text/plain;q=0.8,image/png,*/*;q=0.5VID_BY_MIMETYPE={#'text/xml': 'xml',# XXX rss, owl...}defvid_from_rset(req,rset,schema):"""given a result set, return a view id"""ifrsetisNone:return'index'formimetypeinreq.parse_accept_header('Accept'):ifmimetypeinVID_BY_MIMETYPE:returnVID_BY_MIMETYPE[mimetype]nb_rows=len(rset)# empty resultsetifnb_rows==0:return'noresult'# entity result setifnotschema.eschema(rset.description[0][0]).final:ifneed_table_view(rset,schema):return'table'ifnb_rows==1:ifreq.search_state[0]=='normal':return'primary'return'outofcontext-search'iflen(rset.column_types(0))==1:return'sameetypelist'return'list'return'table'deflinksearch_select_url(req,rset):"""when searching an entity to create a relation, return an url to select entities in the given rset """req.add_js(('cubicweb.ajax.js','cubicweb.edition.js'))target,eid,r_type,searchedtype=req.search_state[1]iftarget=='subject':id_fmt='%s:%s:%%s'%(eid,r_type)else:id_fmt='%%s:%s:%s'%(r_type,eid)triplets='-'.join(id_fmt%row[0]forrowinrset.rows)return"javascript: selectForAssociation('%s', '%s');"%(triplets,eid)classTmpFileViewMixin(object):binary=Truecontent_type='application/octet-stream'cache_max_age=60*60*2# stay in http cache for 2 hours by defaultdefcall(self):self.cell_call()defcell_call(self,row=0,col=0):self.cw_row,self.cw_col=row,col# in case one needs itfd,tmpfile=tempfile.mkstemp('.png')os.close(fd)self._generate(tmpfile)self.w(open(tmpfile,'rb').read())os.unlink(tmpfile)