def _select_view_and_rset(self, rset): req = self._cw if rset is None and not hasattr(req, '_rql_processed'): req._rql_processed = True if req.cnx: rset = self.process_rql() else: rset = None vid = req.form.get('vid') or vid_from_rset(req, rset, self._cw.vreg.schema) try: view = self._cw.vreg['views'].select(vid, req, rset=rset) except ObjectNotFound: self.warning("the view %s could not be found", vid) req.set_message(req._("The view %s could not be found") % vid) vid = vid_from_rset(req, rset, self._cw.vreg.schema) view = self._cw.vreg['views'].select(vid, req, rset=rset) except NoSelectableObject: if rset: req.set_message( req._("The view %s can not be applied to this query") % vid) else: req.set_message( req._("You have no access to this view or it can not " "be used to display the current data.")) vid = req.form.get('fallbackvid') or vid_from_rset( req, rset, req.vreg.schema) view = req.vreg['views'].select(vid, req, rset=rset) return view, rset
def test_more_than_one_entity_same_type(self): with self.admin_access.web_request() as req: rset = req.execute('Any X WHERE X is CWUser') self.assertEqual(vid_from_rset(req, rset, self.schema), 'sameetypelist') rset = req.execute('Any X, L WHERE X login L') self.assertEqual(vid_from_rset(req, rset, self.schema), 'sameetypelist')
def test_one_entity(self): with self.admin_access.web_request() as req: rset = req.execute('Any X WHERE X login "admin"') self.assertEqual(vid_from_rset(req, rset, self.schema), 'primary') rset = req.execute('Any X, L WHERE X login "admin", X login L') self.assertEqual(vid_from_rset(req, rset, self.schema), 'primary') req.search_state = ('pasnormal', ) rset = req.execute('Any X WHERE X login "admin"') self.assertEqual(vid_from_rset(req, rset, self.schema), 'outofcontext-search')
def rql_role(role, rawtext, text, lineno, inliner, options={}, content=[]): """``:rql:`<rql-expr>``` or ``:rql:`<rql-expr>:<vid>``` Example: ``:rql:`Any X,Y WHERE X is CWUser, X login Y:table``` Replace the directive with the output of applying the view to the resultset returned by the query. "X eid %(userid)s" can be used in the RQL query for this query will be executed with the argument {'userid': _cw.user.eid}. """ _cw = inliner.document.settings.context._cw text = text.strip() if ':' in text: rql, vid = text.rsplit(u':', 1) rql = rql.strip() else: rql, vid = text, None _cw.ensure_ro_rql(rql) try: rset = _cw.execute(rql, {'userid': _cw.user.eid}) if rset: if vid is None: vid = vid_from_rset(_cw, rset, _cw.vreg.schema) else: vid = 'noresult' view = _cw.vreg['views'].select(vid, _cw, rset=rset) content = view.render() except Exception as exc: content = 'an error occurred while interpreting this rql directive: %r' % exc set_classes(options) return [nodes.raw('', content, format='html')], []
def test_subquery(self): with self.admin_access.web_request() as req: rset = req.execute( 'DISTINCT Any X,N ORDERBY N ' 'WITH X,N BEING (' ' (DISTINCT Any P,N WHERE P is CWUser, P login N)' ' UNION' ' (DISTINCT Any W,N WHERE W is CWGroup, W name N))') self.assertEqual(vid_from_rset(req, rset, self.schema), 'table')
def set_vid_for_rset(self, req, cls, rset): # cls is there to ease overriding if rset.rowcount == 0: raise NotFound() if 'vid' not in req.form: # check_table=False tells vid_from_rset not to try to use a table view if fetch_rql # include some non final relation req.form['vid'] = views.vid_from_rset(req, rset, req.vreg.schema, check_table=False)
def view(self): # XXX try to use the page-content template req = self._cw rql = req.form.get('rql') if rql: rset = self._exec(rql) elif 'eid' in req.form: rset = self._cw.eid_rset(req.form['eid']) else: rset = None vid = req.form.get('vid') or vid_from_rset(req, rset, self._cw.vreg.schema) try: viewobj = self._cw.vreg['views'].select(vid, req, rset=rset) except NoSelectableObject: vid = req.form.get('fallbackvid', 'noresult') viewobj = self._cw.vreg['views'].select(vid, req, rset=rset) viewobj.set_http_cache_headers() if req.is_client_cache_valid(): return '' return self._call_view(viewobj, paginate=req.form.pop('paginate', False))
def view_is_not_default_view(cls, req, rset=None, **kwargs): # interesting if it propose another view than the current one vid = req.form.get('vid') if vid and vid != vid_from_rset(req, rset, req.vreg.schema): return 1 return 0
def test_aggregat(self): with self.admin_access.web_request() as req: rset = req.execute('Any X, COUNT(T) GROUPBY X WHERE X is T') self.assertEqual(vid_from_rset(req, rset, self.schema), 'table') rset = req.execute('Any MAX(X) WHERE X is CWUser') self.assertEqual(vid_from_rset(req, rset, self.schema), 'table')
def test_more_than_one_entity_by_row_2(self): with self.admin_access.web_request() as req: rset = req.execute('Any X, GN WHERE X in_group G, G name GN') self.assertEqual(vid_from_rset(req, rset, self.schema), 'table')
def test_more_than_one_entity_diff_type(self): with self.admin_access.web_request() as req: rset = req.execute('Any X WHERE X is IN (CWUser, CWGroup)') self.assertEqual(vid_from_rset(req, rset, self.schema), 'list')
def test_one_entity_eid(self): with self.admin_access.web_request() as req: rset = req.execute('Any X WHERE X eid 1') self.assertEqual(vid_from_rset(req, rset, self.schema), 'primary')
def test_no_entity(self): with self.admin_access.web_request() as req: rset = req.execute('Any X WHERE X login "blabla"') self.assertEqual(vid_from_rset(req, rset, self.schema), 'noresult')
def test_no_rset(self): with self.admin_access.web_request() as req: self.assertEqual(vid_from_rset(req, None, self.schema), 'index')
def bookmark_role(role, rawtext, text, lineno, inliner, options={}, content=[]): """``:bookmark:`<bookmark-eid>``` or ``:bookmark:`<eid>:<vid>``` Example: ``:bookmark:`1234:table``` Replace the directive with the output of applying the view to the resultset returned by the query stored in the bookmark. By default, the view is the one stored in the bookmark, but it can be overridden by the directive as in the example above. "X eid %(userid)s" can be used in the RQL query stored in the Bookmark, for this query will be executed with the argument {'userid': _cw.user.eid}. """ _cw = inliner.document.settings.context._cw text = text.strip() try: if ':' in text: eid, vid = text.rsplit(u':', 1) eid = int(eid) else: eid, vid = int(text), None except ValueError: msg = inliner.reporter.error( 'EID number must be a positive number; "%s" is invalid.' % text, line=lineno) prb = inliner.problematic(rawtext, rawtext, msg) return [prb], [msg] try: bookmark = _cw.entity_from_eid(eid) except UnknownEid: msg = inliner.reporter.error('Unknown EID %s.' % text, line=lineno) prb = inliner.problematic(rawtext, rawtext, msg) return [prb], [msg] try: params = dict(_cw.url_parse_qsl(urlsplit(bookmark.path).query)) rql = params['rql'] if vid is None: vid = params.get('vid') except (ValueError, KeyError) as exc: msg = inliner.reporter.error('Could not parse bookmark path %s [%s].' % (bookmark.path, exc), line=lineno) prb = inliner.problematic(rawtext, rawtext, msg) return [prb], [msg] try: rset = _cw.execute(rql, {'userid': _cw.user.eid}) if rset: if vid is None: vid = vid_from_rset(_cw, rset, _cw.vreg.schema) else: vid = 'noresult' view = _cw.vreg['views'].select(vid, _cw, rset=rset) content = view.render() except Exception as exc: content = 'An error occurred while interpreting directive bookmark: %r' % exc set_classes(options) return [nodes.raw('', content, format='html')], []