web/controller.py
branchstable
changeset 4986 c4ef22c85d16
parent 4897 e402e0b32075
child 5040 00782905b720
equal deleted inserted replaced
4982:4247066fd3de 4986:c4ef22c85d16
     5 :copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
     5 :copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
     6 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     7 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     7 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     8 """
     8 """
     9 __docformat__ = "restructuredtext en"
     9 __docformat__ = "restructuredtext en"
       
    10 
       
    11 from logilab.mtconverter import xml_escape
    10 
    12 
    11 from cubicweb.selectors import yes
    13 from cubicweb.selectors import yes
    12 from cubicweb.appobject import AppObject
    14 from cubicweb.appobject import AppObject
    13 from cubicweb.web import LOGGER, Redirect, RequestError
    15 from cubicweb.web import LOGGER, Redirect, RequestError
    14 
    16 
    77             pp = self._cw.vreg['components'].select_or_none('magicsearch', self._cw)
    79             pp = self._cw.vreg['components'].select_or_none('magicsearch', self._cw)
    78             if pp is not None:
    80             if pp is not None:
    79                 self.cw_rset = pp.process_query(rql)
    81                 self.cw_rset = pp.process_query(rql)
    80         return self.cw_rset
    82         return self.cw_rset
    81 
    83 
    82     def check_expected_params(self, params):
       
    83         """check that the given list of parameters are specified in the form
       
    84         dictionary
       
    85         """
       
    86         missing = []
       
    87         for param in params:
       
    88             if not self._cw.form.get(param):
       
    89                 missing.append(param)
       
    90         if missing:
       
    91             raise RequestError('missing required parameter(s): %s'
       
    92                                % ','.join(missing))
       
    93 
       
    94 
       
    95     def notify_edited(self, entity):
    84     def notify_edited(self, entity):
    96         """called by edit_entity() to notify which entity is edited"""
    85         """called by edit_entity() to notify which entity is edited"""
    97         # NOTE: we can't use entity.rest_path() at this point because
    86         # NOTE: we can't use entity.rest_path() at this point because
    98         #       rest_path() could rely on schema constraints (such as a required
    87         #       rest_path() could rely on schema constraints (such as a required
    99         #       relation) that might not be satisfied yet (in case of creations)
    88         #       relation) that might not be satisfied yet (in case of creations)
   100         if not self._edited_entity:
    89         if not self._edited_entity:
   101             self._edited_entity = entity
    90             self._edited_entity = entity
   102 
    91 
   103     # XXX move to EditController (only customer)
       
   104     def delete_entities(self, eidtypes):
       
   105         """delete entities from the repository"""
       
   106         redirect_info = set()
       
   107         eidtypes = tuple(eidtypes)
       
   108         for eid, etype in eidtypes:
       
   109             entity = self._cw.entity_from_eid(eid, etype)
       
   110             path, params = entity.after_deletion_path()
       
   111             redirect_info.add( (path, tuple(params.iteritems())) )
       
   112             entity.delete()
       
   113         if len(redirect_info) > 1:
       
   114             # In the face of ambiguity, refuse the temptation to guess.
       
   115             self._after_deletion_path = 'view', ()
       
   116         else:
       
   117             self._after_deletion_path = iter(redirect_info).next()
       
   118         if len(eidtypes) > 1:
       
   119             self._cw.set_message(self._cw._('entities deleted'))
       
   120         else:
       
   121             self._cw.set_message(self._cw._('entity deleted'))
       
   122 
       
   123     def validate_cache(self, view):
    92     def validate_cache(self, view):
   124         view.set_http_cache_headers()
    93         view.set_http_cache_headers()
   125         self._cw.validate_cache()
    94         self._cw.validate_cache()
   126 
    95 
   127     # XXX is that used AT ALL ?
       
   128     def reset(self):
    96     def reset(self):
   129         """reset form parameters and redirect to a view determinated by given
    97         """reset form parameters and redirect to a view determinated by given
   130         parameters
    98         parameters
   131         """
    99         """
   132         newparams = {}
   100         newparams = {}
   133         # sets message if needed
   101         # sets message if needed
   134         if self._cw.message:
   102         if self._cw.message:
   135             newparams['__message'] = self._cw.message
   103             newparams['_cwmsgid'] = self._cw.set_redirect_message(self._cw.message)
   136         if self._cw.form.has_key('__action_apply'):
   104         if self._cw.form.has_key('__action_apply'):
   137             self._return_to_edition_view(newparams)
   105             self._return_to_edition_view(newparams)
   138         if self._cw.form.has_key('__action_cancel'):
   106         if self._cw.form.has_key('__action_cancel'):
   139             self._return_to_lastpage(newparams)
   107             self._return_to_lastpage(newparams)
   140         else:
   108         else:
   141             self._return_to_original_view(newparams)
   109             self._return_to_original_view(newparams)
   142 
   110 
   143 
       
   144     # XXX is that used AT ALL ?
       
   145     def _return_to_original_view(self, newparams):
   111     def _return_to_original_view(self, newparams):
   146         """validate-button case"""
   112         """validate-button case"""
   147         # transforms __redirect[*] parameters into regular form parameters
   113         # transforms __redirect[*] parameters into regular form parameters
   148         newparams.update(redirect_params(self._cw.form))
   114         newparams.update(redirect_params(self._cw.form))
   149         # find out if we have some explicit `rql` needs
   115         # find out if we have some explicit `rql` needs
   154             path = 'view'
   120             path = 'view'
   155             newparams['rql'] = rql
   121             newparams['rql'] = rql
   156         elif '__redirectpath' in self._cw.form:
   122         elif '__redirectpath' in self._cw.form:
   157             # if redirect path was explicitly specified in the form, use it
   123             # if redirect path was explicitly specified in the form, use it
   158             path = self._cw.form['__redirectpath']
   124             path = self._cw.form['__redirectpath']
   159             if self._edited_entity and path != self._edited_entity.rest_path():
   125             if (self._edited_entity and path != self._edited_entity.rest_path()
   160                 # XXX may be here on modification? if yes the message should be
   126                 and '_cwmsgid' in newparams):
   161                 # modified where __createdpath is detected (cw.web.request)
   127                 # XXX may be here on modification?
   162                 newparams['__createdpath'] = self._edited_entity.rest_path()
   128                 msg = u'(<a href="%s">%s</a>)' % (
       
   129                     xml_escape(self._edited_entity.absolute_url()),
       
   130                     self._cw._('click here to see created entity'))
       
   131                 self._cw.append_to_redirect_message(msg)
   163         elif self._after_deletion_path:
   132         elif self._after_deletion_path:
   164             # else it should have been set during form processing
   133             # else it should have been set during form processing
   165             path, params = self._after_deletion_path
   134             path, params = self._after_deletion_path
   166             params = dict(params) # params given as tuple
   135             params = dict(params) # params given as tuple
   167             params.update(newparams)
   136             params.update(newparams)
   172             path = 'view'
   141             path = 'view'
   173         url = self._cw.build_url(path, **newparams)
   142         url = self._cw.build_url(path, **newparams)
   174         url = append_url_params(url, self._cw.form.get('__redirectparams'))
   143         url = append_url_params(url, self._cw.form.get('__redirectparams'))
   175         raise Redirect(url)
   144         raise Redirect(url)
   176 
   145 
   177     # XXX is that used AT ALL ?
       
   178     def _return_to_edition_view(self, newparams):
   146     def _return_to_edition_view(self, newparams):
   179         """apply-button case"""
   147         """apply-button case"""
   180         form = self._cw.form
   148         form = self._cw.form
   181         if self._edited_entity:
   149         if self._edited_entity:
   182             path = self._edited_entity.rest_path()
   150             path = self._edited_entity.rest_path()
   184         # else, fallback on the old `view?rql=...` url form
   152         # else, fallback on the old `view?rql=...` url form
   185         elif 'rql' in self._cw.form:
   153         elif 'rql' in self._cw.form:
   186             path = 'view'
   154             path = 'view'
   187             newparams['rql'] = form['rql']
   155             newparams['rql'] = form['rql']
   188         else:
   156         else:
   189             self.warning("the edited data seems inconsistent")
   157             self.warning('the edited data seems inconsistent')
   190             path = 'view'
   158             path = 'view'
   191         # pick up the correction edition view
   159         # pick up the correction edition view
   192         if form.get('__form_id'):
   160         if form.get('__form_id'):
   193             newparams['vid'] = form['__form_id']
   161             newparams['vid'] = form['__form_id']
   194         # re-insert copy redirection parameters
   162         # re-insert copy redirection parameters
   196             if redirectparam in form:
   164             if redirectparam in form:
   197                 newparams[redirectparam] = form[redirectparam]
   165                 newparams[redirectparam] = form[redirectparam]
   198         raise Redirect(self._cw.build_url(path, **newparams))
   166         raise Redirect(self._cw.build_url(path, **newparams))
   199 
   167 
   200 
   168 
   201     # XXX is that used AT ALL ?
       
   202     def _return_to_lastpage(self, newparams):
   169     def _return_to_lastpage(self, newparams):
   203         """cancel-button case: in this case we are always expecting to go back
   170         """cancel-button case: in this case we are always expecting to go back
   204         where we came from, and this is not easy. Currently we suppose that
   171         where we came from, and this is not easy. Currently we suppose that
   205         __redirectpath is specifying that place if found, else we look in the
   172         __redirectpath is specifying that place if found, else we look in the
   206         request breadcrumbs for the last visited page.
   173         request breadcrumbs for the last visited page.