web/views/editcontroller.py
changeset 5556 9ab2b4c74baf
parent 5426 0d4853a6e5ee
child 5557 1a534c596bff
equal deleted inserted replaced
5555:a64f48dd5fe4 5556:9ab2b4c74baf
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    14 # details.
    14 # details.
    15 #
    15 #
    16 # You should have received a copy of the GNU Lesser General Public License along
    16 # You should have received a copy of the GNU Lesser General Public License along
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """The edit controller, handling form submitting.
    18 """The edit controller, automatically handling entity form submitting"""
    19 
    19 
    20 """
       
    21 __docformat__ = "restructuredtext en"
    20 __docformat__ = "restructuredtext en"
    22 
    21 
    23 from warnings import warn
    22 from warnings import warn
    24 
    23 
    25 from rql.utils import rqlvar_maker
    24 from rql.utils import rqlvar_maker
    26 
    25 
    27 from logilab.common.textutils import splitstrip
    26 from logilab.common.textutils import splitstrip
    28 
    27 
    29 from cubicweb import Binary, ValidationError, typed_eid
    28 from cubicweb import Binary, ValidationError, typed_eid
    30 from cubicweb.web import INTERNAL_FIELD_VALUE, RequestError, NothingToEdit, ProcessFormError
    29 from cubicweb.view import EntityAdapter, implements_adapter_compat
       
    30 from cubicweb.selectors import implements
       
    31 from cubicweb.web import (INTERNAL_FIELD_VALUE, RequestError, NothingToEdit,
       
    32                           ProcessFormError)
    31 from cubicweb.web.views import basecontrollers, autoform
    33 from cubicweb.web.views import basecontrollers, autoform
       
    34 
       
    35 
       
    36 class IEditControlAdapter(EntityAdapter):
       
    37     __regid__ = 'IEditControl'
       
    38     __select__ = implements('Any')
       
    39 
       
    40     @implements_adapter_compat('IEditControl')
       
    41     def after_deletion_path(self):
       
    42         """return (path, parameters) which should be used as redirect
       
    43         information when this entity is being deleted
       
    44         """
       
    45         parent = self.cw_adapt_to('IBreadCrumbs').parent_entity()
       
    46         if parent is not None:
       
    47             return parent.rest_path(), {}
       
    48         return str(self.e_schema).lower(), {}
       
    49 
       
    50     @implements_adapter_compat('IEditControl')
       
    51     def pre_web_edit(self):
       
    52         """callback called by the web editcontroller when an entity will be
       
    53         created/modified, to let a chance to do some entity specific stuff.
       
    54 
       
    55         Do nothing by default.
       
    56         """
       
    57         pass
       
    58 
    32 
    59 
    33 def valerror_eid(eid):
    60 def valerror_eid(eid):
    34     try:
    61     try:
    35         return typed_eid(eid)
    62         return typed_eid(eid)
    36     except (ValueError, TypeError):
    63     except (ValueError, TypeError):
   153         etype = formparams['__type']
   180         etype = formparams['__type']
   154         entity = self._cw.vreg['etypes'].etype_class(etype)(self._cw)
   181         entity = self._cw.vreg['etypes'].etype_class(etype)(self._cw)
   155         entity.eid = formparams['eid']
   182         entity.eid = formparams['eid']
   156         is_main_entity = self._cw.form.get('__maineid') == formparams['eid']
   183         is_main_entity = self._cw.form.get('__maineid') == formparams['eid']
   157         # let a chance to do some entity specific stuff
   184         # let a chance to do some entity specific stuff
   158         entity.pre_web_edit()
   185         entity.cw_adapt_to('IEditControl').pre_web_edit()
   159         # create a rql query from parameters
   186         # create a rql query from parameters
   160         rqlquery = RqlQuery()
   187         rqlquery = RqlQuery()
   161         # process inlined relations at the same time as attributes
   188         # process inlined relations at the same time as attributes
   162         # this will generate less rql queries and might be useful in
   189         # this will generate less rql queries and might be useful in
   163         # a few dark corners
   190         # a few dark corners
   274         """delete entities from the repository"""
   301         """delete entities from the repository"""
   275         redirect_info = set()
   302         redirect_info = set()
   276         eidtypes = tuple(eidtypes)
   303         eidtypes = tuple(eidtypes)
   277         for eid, etype in eidtypes:
   304         for eid, etype in eidtypes:
   278             entity = self._cw.entity_from_eid(eid, etype)
   305             entity = self._cw.entity_from_eid(eid, etype)
   279             path, params = entity.after_deletion_path()
   306             path, params = entity.cw_adapt_to('IEditControl').after_deletion_path()
   280             redirect_info.add( (path, tuple(params.iteritems())) )
   307             redirect_info.add( (path, tuple(params.iteritems())) )
   281             entity.delete()
   308             entity.delete()
   282         if len(redirect_info) > 1:
   309         if len(redirect_info) > 1:
   283             # In the face of ambiguity, refuse the temptation to guess.
   310             # In the face of ambiguity, refuse the temptation to guess.
   284             self._after_deletion_path = 'view', ()
   311             self._after_deletion_path = 'view', ()