web/controller.py
changeset 3655 af86ab65a282
parent 3460 e4843535db25
child 4252 6c4f109c2b03
equal deleted inserted replaced
3654:e4bed64e0ad7 3655:af86ab65a282
    87     def process_rql(self, rql):
    87     def process_rql(self, rql):
    88         """execute rql if specified"""
    88         """execute rql if specified"""
    89         # XXX assigning to self really necessary?
    89         # XXX assigning to self really necessary?
    90         self.cw_rset = None
    90         self.cw_rset = None
    91         if rql:
    91         if rql:
    92             self.req.ensure_ro_rql(rql)
    92             self._cw.ensure_ro_rql(rql)
    93             if not isinstance(rql, unicode):
    93             if not isinstance(rql, unicode):
    94                 rql = unicode(rql, self.req.encoding)
    94                 rql = unicode(rql, self._cw.encoding)
    95             pp = self.vreg['components'].select_or_none('magicsearch', self.req)
    95             pp = self._cw.vreg['components'].select_or_none('magicsearch', self._cw)
    96             if pp is not None:
    96             if pp is not None:
    97                 self.cw_rset = pp.process_query(rql, self.req)
    97                 self.cw_rset = pp.process_query(rql)
    98         return self.cw_rset
    98         return self.cw_rset
    99 
    99 
   100     def check_expected_params(self, params):
   100     def check_expected_params(self, params):
   101         """check that the given list of parameters are specified in the form
   101         """check that the given list of parameters are specified in the form
   102         dictionary
   102         dictionary
   103         """
   103         """
   104         missing = []
   104         missing = []
   105         for param in params:
   105         for param in params:
   106             if not self.req.form.get(param):
   106             if not self._cw.form.get(param):
   107                 missing.append(param)
   107                 missing.append(param)
   108         if missing:
   108         if missing:
   109             raise RequestError('missing required parameter(s): %s'
   109             raise RequestError('missing required parameter(s): %s'
   110                                % ','.join(missing))
   110                                % ','.join(missing))
   111 
   111 
   121     def delete_entities(self, eidtypes):
   121     def delete_entities(self, eidtypes):
   122         """delete entities from the repository"""
   122         """delete entities from the repository"""
   123         redirect_info = set()
   123         redirect_info = set()
   124         eidtypes = tuple(eidtypes)
   124         eidtypes = tuple(eidtypes)
   125         for eid, etype in eidtypes:
   125         for eid, etype in eidtypes:
   126             entity = self.req.entity_from_eid(eid, etype)
   126             entity = self._cw.entity_from_eid(eid, etype)
   127             path, params = entity.after_deletion_path()
   127             path, params = entity.after_deletion_path()
   128             redirect_info.add( (path, tuple(params.iteritems())) )
   128             redirect_info.add( (path, tuple(params.iteritems())) )
   129             entity.delete()
   129             entity.delete()
   130         if len(redirect_info) > 1:
   130         if len(redirect_info) > 1:
   131             # In the face of ambiguity, refuse the temptation to guess.
   131             # In the face of ambiguity, refuse the temptation to guess.
   132             self._after_deletion_path = 'view', ()
   132             self._after_deletion_path = 'view', ()
   133         else:
   133         else:
   134             self._after_deletion_path = iter(redirect_info).next()
   134             self._after_deletion_path = iter(redirect_info).next()
   135         if len(eidtypes) > 1:
   135         if len(eidtypes) > 1:
   136             self.req.set_message(self.req._('entities deleted'))
   136             self._cw.set_message(self._cw._('entities deleted'))
   137         else:
   137         else:
   138             self.req.set_message(self.req._('entity deleted'))
   138             self._cw.set_message(self._cw._('entity deleted'))
   139 
   139 
   140     def delete_relations(self, rdefs):
   140     def delete_relations(self, rdefs):
   141         """delete relations from the repository"""
   141         """delete relations from the repository"""
   142         # FIXME convert to using the syntax subject:relation:eids
   142         # FIXME convert to using the syntax subject:relation:eids
   143         execute = self.req.execute
   143         execute = self._cw.execute
   144         for subj, rtype, obj in rdefs:
   144         for subj, rtype, obj in rdefs:
   145             rql = 'DELETE X %s Y where X eid %%(x)s, Y eid %%(y)s' % rtype
   145             rql = 'DELETE X %s Y where X eid %%(x)s, Y eid %%(y)s' % rtype
   146             execute(rql, {'x': subj, 'y': obj}, ('x', 'y'))
   146             execute(rql, {'x': subj, 'y': obj}, ('x', 'y'))
   147         self.req.set_message(self.req._('relations deleted'))
   147         self._cw.set_message(self._cw._('relations deleted'))
   148 
   148 
   149     def insert_relations(self, rdefs):
   149     def insert_relations(self, rdefs):
   150         """insert relations into the repository"""
   150         """insert relations into the repository"""
   151         execute = self.req.execute
   151         execute = self._cw.execute
   152         for subj, rtype, obj in rdefs:
   152         for subj, rtype, obj in rdefs:
   153             rql = 'SET X %s Y where X eid %%(x)s, Y eid %%(y)s' % rtype
   153             rql = 'SET X %s Y where X eid %%(x)s, Y eid %%(y)s' % rtype
   154             execute(rql, {'x': subj, 'y': obj}, ('x', 'y'))
   154             execute(rql, {'x': subj, 'y': obj}, ('x', 'y'))
   155 
   155 
   156 
   156 
   158         """reset form parameters and redirect to a view determinated by given
   158         """reset form parameters and redirect to a view determinated by given
   159         parameters
   159         parameters
   160         """
   160         """
   161         newparams = {}
   161         newparams = {}
   162         # sets message if needed
   162         # sets message if needed
   163         if self.req.message:
   163         if self._cw.message:
   164             newparams['__message'] = self.req.message
   164             newparams['__message'] = self._cw.message
   165         if self.req.form.has_key('__action_apply'):
   165         if self._cw.form.has_key('__action_apply'):
   166             self._return_to_edition_view(newparams)
   166             self._return_to_edition_view(newparams)
   167         if self.req.form.has_key('__action_cancel'):
   167         if self._cw.form.has_key('__action_cancel'):
   168             self._return_to_lastpage(newparams)
   168             self._return_to_lastpage(newparams)
   169         else:
   169         else:
   170             self._return_to_original_view(newparams)
   170             self._return_to_original_view(newparams)
   171 
   171 
   172 
   172 
   173     def _return_to_original_view(self, newparams):
   173     def _return_to_original_view(self, newparams):
   174         """validate-button case"""
   174         """validate-button case"""
   175         # transforms __redirect[*] parameters into regular form parameters
   175         # transforms __redirect[*] parameters into regular form parameters
   176         newparams.update(redirect_params(self.req.form))
   176         newparams.update(redirect_params(self._cw.form))
   177         # find out if we have some explicit `rql` needs
   177         # find out if we have some explicit `rql` needs
   178         rql = newparams.pop('rql', None)
   178         rql = newparams.pop('rql', None)
   179         # if rql is needed (explicit __redirectrql or multiple deletions for
   179         # if rql is needed (explicit __redirectrql or multiple deletions for
   180         # instance), we have to use the old `view?rql=...` form
   180         # instance), we have to use the old `view?rql=...` form
   181         if rql:
   181         if rql:
   182             path = 'view'
   182             path = 'view'
   183             newparams['rql'] = rql
   183             newparams['rql'] = rql
   184         elif '__redirectpath' in self.req.form:
   184         elif '__redirectpath' in self._cw.form:
   185             # if redirect path was explicitly specified in the form, use it
   185             # if redirect path was explicitly specified in the form, use it
   186             path = self.req.form['__redirectpath']
   186             path = self._cw.form['__redirectpath']
   187             if self._edited_entity and path != self._edited_entity.rest_path():
   187             if self._edited_entity and path != self._edited_entity.rest_path():
   188                 # XXX may be here on modification? if yes the message should be
   188                 # XXX may be here on modification? if yes the message should be
   189                 # modified where __createdpath is detected (cw.web.request)
   189                 # modified where __createdpath is detected (cw.web.request)
   190                 newparams['__createdpath'] = self._edited_entity.rest_path()
   190                 newparams['__createdpath'] = self._edited_entity.rest_path()
   191         elif self._after_deletion_path:
   191         elif self._after_deletion_path:
   197         elif self._edited_entity:
   197         elif self._edited_entity:
   198             path = self._edited_entity.rest_path()
   198             path = self._edited_entity.rest_path()
   199         else:
   199         else:
   200             path = 'view'
   200             path = 'view'
   201         url = self._cw.build_url(path, **newparams)
   201         url = self._cw.build_url(path, **newparams)
   202         url = append_url_params(url, self.req.form.get('__redirectparams'))
   202         url = append_url_params(url, self._cw.form.get('__redirectparams'))
   203         raise Redirect(url)
   203         raise Redirect(url)
   204 
   204 
   205 
   205 
   206     def _return_to_edition_view(self, newparams):
   206     def _return_to_edition_view(self, newparams):
   207         """apply-button case"""
   207         """apply-button case"""
   208         form = self.req.form
   208         form = self._cw.form
   209         if self._edited_entity:
   209         if self._edited_entity:
   210             path = self._edited_entity.rest_path()
   210             path = self._edited_entity.rest_path()
   211             newparams.pop('rql', None)
   211             newparams.pop('rql', None)
   212         # else, fallback on the old `view?rql=...` url form
   212         # else, fallback on the old `view?rql=...` url form
   213         elif 'rql' in self.req.form:
   213         elif 'rql' in self._cw.form:
   214             path = 'view'
   214             path = 'view'
   215             newparams['rql'] = form['rql']
   215             newparams['rql'] = form['rql']
   216         else:
   216         else:
   217             self.warning("the edited data seems inconsistent")
   217             self.warning("the edited data seems inconsistent")
   218             path = 'view'
   218             path = 'view'
   230         """cancel-button case: in this case we are always expecting to go back
   230         """cancel-button case: in this case we are always expecting to go back
   231         where we came from, and this is not easy. Currently we suppose that
   231         where we came from, and this is not easy. Currently we suppose that
   232         __redirectpath is specifying that place if found, else we look in the
   232         __redirectpath is specifying that place if found, else we look in the
   233         request breadcrumbs for the last visited page.
   233         request breadcrumbs for the last visited page.
   234         """
   234         """
   235         if '__redirectpath' in self.req.form:
   235         if '__redirectpath' in self._cw.form:
   236             # if redirect path was explicitly specified in the form, use it
   236             # if redirect path was explicitly specified in the form, use it
   237             path = self.req.form['__redirectpath']
   237             path = self._cw.form['__redirectpath']
   238             url = self._cw.build_url(path, **newparams)
   238             url = self._cw.build_url(path, **newparams)
   239             url = append_url_params(url, self.req.form.get('__redirectparams'))
   239             url = append_url_params(url, self._cw.form.get('__redirectparams'))
   240         else:
   240         else:
   241             url = self.req.last_visited_page()
   241             url = self._cw.last_visited_page()
   242         raise Redirect(url)
   242         raise Redirect(url)
   243 
   243 
   244 
   244 
   245 from cubicweb import set_log_methods
   245 from cubicweb import set_log_methods
   246 set_log_methods(Controller, LOGGER)
   246 set_log_methods(Controller, LOGGER)