diff -r 058bb3dc685f -r 0b59724cb3f2 web/views/baseviews.py --- a/web/views/baseviews.py Mon Jan 04 18:40:30 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,644 +0,0 @@ -# copyright 2003-2012 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 . -""" -HTML views -~~~~~~~~~~ - -Special views -````````````` - -.. autoclass:: NullView -.. autoclass:: NoResultView -.. autoclass:: FinalView - - -Base entity views -````````````````` - -.. autoclass:: InContextView -.. autoclass:: OutOfContextView -.. autoclass:: OneLineView - -Those are used to display a link to an entity, whose label depends on the entity -having to be displayed in or out of context (of another entity): some entities -make sense in the context of another entity. For instance, the `Version` of a -`Project` in forge. So one may expect that 'incontext' will be called when -display a version from within the context of a project, while 'outofcontext"' -will be called in other cases. In our example, the 'incontext' view of the -version would be something like '0.1.2', while the 'outofcontext' view would -include the project name, e.g. 'baz 0.1.2' (since only a version number without -the associated project doesn't make sense if you don't know yet that you're -talking about the famous 'baz' project. |cubicweb| tries to make guess and call -'incontext'/'outofcontext' nicely. When it can't know, the 'oneline' view should -be used. - - -List entity views -````````````````` - -.. autoclass:: ListView -.. autoclass:: SimpleListView -.. autoclass:: SameETypeListView -.. autoclass:: CSVView - -Those list views can be given a 'subvid' arguments, telling the view to use of -each item in the list. When not specified, the value of the 'redirect_vid' -attribute of :class:`ListItemView` (for 'listview') or of -:class:`SimpleListView` will be used. This default to 'outofcontext' for 'list' -/ 'incontext' for 'simplelist' - - -Text entity views -~~~~~~~~~~~~~~~~~ - -Basic HTML view have some variants to be used when generating raw text, not HTML -(for notifications for instance). Also, as explained above, some of the HTML -views use those text views as a basis. - -.. autoclass:: TextView -.. autoclass:: InContextTextView -.. autoclass:: OutOfContextView -""" - -__docformat__ = "restructuredtext en" -from cubicweb import _ - -from datetime import timedelta -from warnings import warn - -from six.moves import range - -from rql import nodes - -from logilab.mtconverter import TransformError, xml_escape -from logilab.common.registry import yes - -from cubicweb import NoSelectableObject, tags -from cubicweb.predicates import empty_rset, one_etype_rset, match_kwargs -from cubicweb.schema import display_name -from cubicweb.view import EntityView, AnyRsetView, View -from cubicweb.uilib import cut -from cubicweb.web.views import calendar - - -class NullView(AnyRsetView): - """:__regid__: *null* - - This view is the default view used when nothing needs to be rendered. It is - always applicable and is usually used as fallback view when calling - :meth:`_cw.view` to display nothing if the result set is empty. - """ - __regid__ = 'null' - __select__ = yes() - def call(self, **kwargs): - pass - cell_call = call - - -class NoResultView(View): - """:__regid__: *noresult* - - This view is the default view to be used when no result has been found - (i.e. empty result set). - - It's usually used as fallback view when calling :meth:`_cw.view` to display - "no results" if the result set is empty. - """ - __regid__ = 'noresult' - __select__ = empty_rset() - - def call(self, **kwargs): - self.w(u'
%s
\n' - % self._cw._('No result matching query')) - - -class FinalView(AnyRsetView): - """:__regid__: *final* - - Display the value of a result set cell with minimal transformations - (i.e. you'll get a number for entities). It is applicable on any result set, - though usually dedicated for cells containing an attribute's value. - """ - __regid__ = 'final' - - def cell_call(self, row, col, props=None, format='text/html'): - value = self.cw_rset.rows[row][col] - if value is None: - self.w(u'') - return - etype = self.cw_rset.description[row][col] - if etype == 'String': - entity, rtype = self.cw_rset.related_entity(row, col) - if entity is not None: - # call entity's printable_value which may have more information - # about string format & all - self.w(entity.printable_value(rtype, value, format=format)) - return - value = self._cw.printable_value(etype, value, props) - if etype in ('Time', 'Interval'): - self.w(value.replace(' ', ' ')) - else: - self.wdata(value) - - -class InContextView(EntityView): - """:__regid__: *incontext* - - This view is used when the entity should be considered as displayed in its - context. By default it produces the result of ``entity.dc_title()`` wrapped in a - link leading to the primary view of the entity. - """ - __regid__ = 'incontext' - - def cell_call(self, row, col): - entity = self.cw_rset.get_entity(row, col) - desc = cut(entity.dc_description(), 50) - self.w(u'%s' % ( - xml_escape(entity.absolute_url()), xml_escape(desc), - xml_escape(entity.dc_title()))) - -class OutOfContextView(EntityView): - """:__regid__: *outofcontext* - - This view is used when the entity should be considered as displayed out of - its context. By default it produces the result of ``entity.dc_long_title()`` - wrapped in a link leading to the primary view of the entity. - """ - __regid__ = 'outofcontext' - - def cell_call(self, row, col): - entity = self.cw_rset.get_entity(row, col) - desc = cut(entity.dc_description(), 50) - self.w(u'%s' % ( - xml_escape(entity.absolute_url()), xml_escape(desc), - xml_escape(entity.dc_long_title()))) - - -class OneLineView(EntityView): - """:__regid__: *oneline* - - This view is used when we can't tell if the entity should be considered as - displayed in or out of context. By default it produces the result of the - `text` view in a link leading to the primary view of the entity. - """ - __regid__ = 'oneline' - title = _('oneline') - - def cell_call(self, row, col, **kwargs): - """the one line view for an entity: linked text view - """ - entity = self.cw_rset.get_entity(row, col) - desc = cut(entity.dc_description(), 50) - title = cut(entity.dc_title(), - self._cw.property_value('navigation.short-line-size')) - self.w(u'%s' % ( - xml_escape(entity.absolute_url()), xml_escape(desc), - xml_escape(title))) - - -# text views ################################################################### - -class TextView(EntityView): - """:__regid__: *text* - - This is the simplest text view for an entity. By default it returns the - result of the entity's `dc_title()` method, which is cut to fit the - `navigation.short-line-size` property if necessary. - """ - __regid__ = 'text' - title = _('text') - content_type = 'text/plain' - - def call(self, **kwargs): - """The view is called for an entire result set, by default loop other - rows of the result set and call the same view on the particular row. - - Subclasses views that are applicable on None result sets will have to - override this method. - """ - rset = self.cw_rset - if rset is None: - raise NotImplementedError(self) - for i in range(len(rset)): - self.wview(self.__regid__, rset, row=i, **kwargs) - if len(rset) > 1: - self.w(u"\n") - - def cell_call(self, row, col=0, **kwargs): - entity = self.cw_rset.get_entity(row, col) - self.w(cut(entity.dc_title(), - self._cw.property_value('navigation.short-line-size'))) - - -class InContextTextView(TextView): - """:__regid__: *textincontext* - - Similar to the `text` view, but called when an entity is considered in - context (see description of incontext HTML view for more information on - this). By default it displays what's returned by the `dc_title()` method of - the entity. - """ - __regid__ = 'textincontext' - title = None # not listed as a possible view - def cell_call(self, row, col): - entity = self.cw_rset.get_entity(row, col) - self.w(entity.dc_title()) - - -class OutOfContextTextView(InContextTextView): - """:__regid__: *textoutofcontext* - - Similar to the `text` view, but called when an entity is considered out of - context (see description of outofcontext HTML view for more information on - this). By default it displays what's returned by the `dc_long_title()` - method of the entity. - """ - __regid__ = 'textoutofcontext' - - def cell_call(self, row, col): - entity = self.cw_rset.get_entity(row, col) - self.w(entity.dc_long_title()) - - -# list views ################################################################## - -class ListView(EntityView): - """:__regid__: *list* - - This view displays a list of entities by creating a HTML list (`