web/views/editcontroller.py
branchtls-sprint
changeset 1753 ba01605cdd9a
parent 1361 c558a88bb85d
child 1765 a25c7c73c8f6
equal deleted inserted replaced
1752:4b0b912ff5b7 1753:ba01605cdd9a
    63                 raise ValidationError(None, {None: req._('nothing to edit')})
    63                 raise ValidationError(None, {None: req._('nothing to edit')})
    64         # handle relations in newly created entities
    64         # handle relations in newly created entities
    65         if self._pending_relations:
    65         if self._pending_relations:
    66             for rschema, formparams, x, entity in self._pending_relations:
    66             for rschema, formparams, x, entity in self._pending_relations:
    67                 self.handle_relation(rschema, formparams, x, entity, True)
    67                 self.handle_relation(rschema, formparams, x, entity, True)
    68             
    68 
    69         # XXX this processes *all* pending operations of *all* entities
    69         # XXX this processes *all* pending operations of *all* entities
    70         if form.has_key('__delete'):
    70         if form.has_key('__delete'):
    71             todelete += req.list_form_param('__delete', form, pop=True)
    71             todelete += req.list_form_param('__delete', form, pop=True)
    72         if todelete:
    72         if todelete:
    73             self.delete_relations(parse_relations_descr(todelete))
    73             self.delete_relations(parse_relations_descr(todelete))
    74         if form.has_key('__insert'):
    74         if form.has_key('__insert'):
    75             toinsert = req.list_form_param('__insert', form, pop=True)
    75             toinsert = req.list_form_param('__insert', form, pop=True)
    76         if toinsert:
    76         if toinsert:
    77             self.insert_relations(parse_relations_descr(toinsert))
    77             self.insert_relations(parse_relations_descr(toinsert))
    78         self.req.remove_pending_operations()
    78         self.req.remove_pending_operations()
    79         
    79 
    80     def edit_entity(self, formparams, multiple=False):
    80     def edit_entity(self, formparams, multiple=False):
    81         """edit / create / copy an entity and return its eid"""
    81         """edit / create / copy an entity and return its eid"""
    82         etype = formparams['__type']
    82         etype = formparams['__type']
    83         entity = self.vreg.etype_class(etype)(self.req, None, None)
    83         entity = self.vreg.etype_class(etype)(self.req, None, None)
    84         entity.eid = eid = self._get_eid(formparams['eid'])
    84         entity.eid = eid = self._get_eid(formparams['eid'])
    85         edited = self.req.form.get('__maineid') == formparams['eid']
    85         edited = self.req.form.get('__maineid') == formparams['eid']
    86         # let a chance to do some entity specific stuff.
    86         # let a chance to do some entity specific stuff.
    87         entity.pre_web_edit() 
    87         entity.pre_web_edit()
    88         # create a rql query from parameters
    88         # create a rql query from parameters
    89         self.relations = []
    89         self.relations = []
    90         self.restrictions = []
    90         self.restrictions = []
    91         # process inlined relations at the same time as attributes
    91         # process inlined relations at the same time as attributes
    92         # this is required by some external source such as the svn source which
    92         # this is required by some external source such as the svn source which
    97                 self.handle_attribute(entity, rschema, formparams)
    97                 self.handle_attribute(entity, rschema, formparams)
    98             elif rschema.inlined:
    98             elif rschema.inlined:
    99                 self.handle_inlined_relation(rschema, formparams, entity)
    99                 self.handle_inlined_relation(rschema, formparams, entity)
   100         execute = self.req.execute
   100         execute = self.req.execute
   101         if eid is None: # creation or copy
   101         if eid is None: # creation or copy
   102             if self.relations: 
   102             if self.relations:
   103                 rql = 'INSERT %s X: %s' % (etype, ','.join(self.relations))
   103                 rql = 'INSERT %s X: %s' % (etype, ','.join(self.relations))
   104             else:
   104             else:
   105                 rql = 'INSERT %s X' % etype
   105                 rql = 'INSERT %s X' % etype
   106             if self.restrictions:
   106             if self.restrictions:
   107                 rql += ' WHERE %s' % ','.join(self.restrictions)
   107                 rql += ' WHERE %s' % ','.join(self.restrictions)
   108             try:
   108             try:
   109                 # get the new entity (in some cases, the type might have 
   109                 # get the new entity (in some cases, the type might have
   110                 # changed as for the File --> Image mutation)
   110                 # changed as for the File --> Image mutation)
   111                 entity = execute(rql, formparams).get_entity(0, 0)
   111                 entity = execute(rql, formparams).get_entity(0, 0)
   112                 eid = entity.eid
   112                 eid = entity.eid
   113             except ValidationError, ex:
   113             except ValidationError, ex:
   114                 # ex.entity may be an int or an entity instance
   114                 # ex.entity may be an int or an entity instance
   150         return eid
   150         return eid
   151 
   151 
   152     def _action_apply(self):
   152     def _action_apply(self):
   153         self._default_publish()
   153         self._default_publish()
   154         self.reset()
   154         self.reset()
   155             
   155 
   156     def _action_cancel(self):
   156     def _action_cancel(self):
   157         errorurl = self.req.form.get('__errorurl')
   157         errorurl = self.req.form.get('__errorurl')
   158         if errorurl:
   158         if errorurl:
   159             self.req.cancel_edition(errorurl)
   159             self.req.cancel_edition(errorurl)
   160         return self.reset()
   160         return self.reset()
   170             return False, None # not edited
   170             return False, None # not edited
   171         value = formparams.get(rtype) or None
   171         value = formparams.get(rtype) or None
   172         if entity.has_eid() and (formparams.get(editkey) or None) == value:
   172         if entity.has_eid() and (formparams.get(editkey) or None) == value:
   173             return False, None # not modified
   173             return False, None # not modified
   174         if value == INTERNAL_FIELD_VALUE:
   174         if value == INTERNAL_FIELD_VALUE:
   175             value = None        
   175             value = None
   176         return True, value
   176         return True, value
   177 
   177 
   178     def handle_attribute(self, entity, rschema, formparams):
   178     def handle_attribute(self, entity, rschema, formparams):
   179         """append to `relations` part of the rql query to edit the
   179         """append to `relations` part of the rql query to edit the
   180         attribute described by the given schema if necessary
   180         attribute described by the given schema if necessary
   282             formparams[attr] = iter(values).next()
   282             formparams[attr] = iter(values).next()
   283             self.relations.append('X %s %s' % (attr, attr.upper()))
   283             self.relations.append('X %s %s' % (attr, attr.upper()))
   284             self.restrictions.append('%s eid %%(%s)s' % (attr.upper(), attr))
   284             self.restrictions.append('%s eid %%(%s)s' % (attr.upper(), attr))
   285         elif entity.has_eid():
   285         elif entity.has_eid():
   286             self.handle_relation(rschema, formparams, 'subject', entity, late)
   286             self.handle_relation(rschema, formparams, 'subject', entity, late)
   287         
   287 
   288     def handle_relation(self, rschema, formparams, x, entity, late=False):
   288     def handle_relation(self, rschema, formparams, x, entity, late=False):
   289         """handle edition for the (rschema, x) relation of the given entity
   289         """handle edition for the (rschema, x) relation of the given entity
   290         """
   290         """
   291         try:
   291         try:
   292             values, origvalues = self._relation_values(rschema, formparams, x,
   292             values, origvalues = self._relation_values(rschema, formparams, x,
   314                 self.req.execute(rql, {'x': eid, 'y': reid}, ('x', 'y'))
   314                 self.req.execute(rql, {'x': eid, 'y': reid}, ('x', 'y'))
   315         rql = 'SET %s %s %s WHERE X eid %%(x)s, Y eid %%(y)s' % (
   315         rql = 'SET %s %s %s WHERE X eid %%(x)s, Y eid %%(y)s' % (
   316             subjvar, rschema, objvar)
   316             subjvar, rschema, objvar)
   317         for reid in values.difference(origvalues):
   317         for reid in values.difference(origvalues):
   318             self.req.execute(rql, {'x': eid, 'y': reid}, ('x', 'y'))
   318             self.req.execute(rql, {'x': eid, 'y': reid}, ('x', 'y'))
   319     
   319 
   320     def _get_eid(self, eid):
   320     def _get_eid(self, eid):
   321         # should be either an int (existant entity) or a variable (to be
   321         # should be either an int (existant entity) or a variable (to be
   322         # created entity)
   322         # created entity)
   323         assert eid or eid == 0, repr(eid) # 0 is a valid eid
   323         assert eid or eid == 0, repr(eid) # 0 is a valid eid
   324         try:
   324         try:
   344                 # eid is still None while it's already a late call
   344                 # eid is still None while it's already a late call
   345                 # this mean that the associated entity has not been created
   345                 # this mean that the associated entity has not been created
   346                 raise Exception('duh')
   346                 raise Exception('duh')
   347             result.add(eid)
   347             result.add(eid)
   348         return result
   348         return result
   349         
   349 
   350 
   350