pass default values along the whole call chain, fix hidden field update bug tls-sprint
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 13 May 2009 19:51:30 +0200
branchtls-sprint
changeset 1798 cc86fe8efaaa
parent 1797 c2a80130b06d
child 1799 183acfaa3cde
pass default values along the whole call chain, fix hidden field update bug
web/data/cubicweb.edition.js
web/views/basecontrollers.py
web/views/editcontroller.py
web/views/editforms.py
--- a/web/data/cubicweb.edition.js	Wed May 13 17:21:35 2009 +0200
+++ b/web/data/cubicweb.edition.js	Wed May 13 19:51:30 2009 +0200
@@ -449,7 +449,7 @@
  * @param eid : the eid of the entity being edited
  * @param reload: boolean to reload page if true (when changing URL dependant data)
  */
-function inlineValidateAttributeForm(formid, rtype, eid, divid, reload) {
+function inlineValidateAttributeForm(formid, rtype, eid, divid, reload, default_value) {
     try {
 	var form = getNode(formid);
 	if (typeof FCKeditorAPI != "undefined") {
@@ -461,7 +461,8 @@
 	    }
 	}
 	var zipped = formContents(form);
-	var d = asyncRemoteExec('edit_field', 'apply', zipped[0], zipped[1], rtype, eid);
+	var d = asyncRemoteExec('edit_field', 'apply', zipped[0], zipped[1],
+                                rtype, eid, default_value);
     } catch (ex) {
 	log('got exception', ex);
 	return false;
@@ -488,11 +489,14 @@
     return false;
 }
 
-function inlineValidateRelationForm(formid, rtype, eid, divid, vid) {
+function inlineValidateRelationForm(formid, rtype, role, eid, divid, vid, default_value) {
     try {
 	var form = getNode(formid);
+        var relname = rtype + ':' + eid;
+        var newtarget = jQuery('[name=' + relname + ']').val();
 	var zipped = formContents(form);
-	var d = asyncRemoteExec('edit_relation', 'apply', zipped[0], zipped[1], rtype, eid, vid);
+	var d = asyncRemoteExec('edit_relation', 'apply', zipped[0], zipped[1], rtype, role,
+                                eid, vid, default_value);
     } catch (ex) {
 	log('got exception', ex);
 	return false;
@@ -506,6 +510,8 @@
           // hide global error messages
 	  jQuery('div.errorMessage').remove();
 	  jQuery('#appMsg').hide();
+          var inputname = 'edit' + role[0] + '-' + relname;
+          jQuery('input[name=' + inputname + ']').val(newtarget);
 	  cancelInlineEdit(eid, rtype, divid);
 	}
         return false;
--- a/web/views/basecontrollers.py	Wed May 13 17:21:35 2009 +0200
+++ b/web/views/basecontrollers.py	Wed May 13 19:51:30 2009 +0200
@@ -382,8 +382,8 @@
             else:
                 eid = err.entity.eid
             return (False, (eid, err.errors))
-        except Redirect, err:
-            return (True, err.location)
+        except Redirect, redir:
+            return (True, redir.location)
         except Exception, err:
             self.req.cnx.rollback()
             self.exception('unexpected error in js_validateform')
@@ -391,7 +391,7 @@
         return (False, '???')
 
     @jsonize
-    def js_edit_field(self, action, names, values, rtype, eid):
+    def js_edit_field(self, action, names, values, rtype, eid, default):
         success, args = self.validate_form(action, names, values)
         if success:
             # Any X,N where we don't seem to use N is an optimisation
@@ -400,18 +400,22 @@
                                     {'x': eid}, 'x')
             entity = rset.get_entity(0, 0)
             value = entity.printable_value(rtype)
-            return (success, args, value or self.req._('not specified'))
+            return (success, args, value or default)
         else:
             return (success, args, None)
 
     @jsonize
     def js_edit_relation(self, action, names, values,
-                         rtype, eid, vid, role='subject'):
+                         rtype, role, eid, vid, default):
         success, args = self.validate_form(action, names, values)
         if success:
             entity = self.req.eid_rset(eid).get_entity(0, 0)
             rset = entity.related(rtype, role)
-            return (success, args, self.view(vid, rset))
+            if rset:
+                output = self.view(vid, rset)
+            else:
+                output = default
+            return (success, args, output)
         else:
             return (success, args, None)
 
--- a/web/views/editcontroller.py	Wed May 13 17:21:35 2009 +0200
+++ b/web/views/editcontroller.py	Wed May 13 19:51:30 2009 +0200
@@ -308,14 +308,16 @@
         if x == 'object' or not rschema.inlined or not values:
             # this is not an inlined relation or no values specified,
             # explicty remove relations
+            rql = 'DELETE %s %s %s WHERE X eid %%(x)s, Y eid %%(y)s' % (
+                subjvar, rschema, objvar)
             for reid in origvalues.difference(values):
-                rql = 'DELETE %s %s %s WHERE X eid %%(x)s, Y eid %%(y)s' % (
-                    subjvar, rschema, objvar)
                 self.req.execute(rql, {'x': eid, 'y': reid}, ('x', 'y'))
-        rql = 'SET %s %s %s WHERE X eid %%(x)s, Y eid %%(y)s' % (
-            subjvar, rschema, objvar)
-        for reid in values.difference(origvalues):
-            self.req.execute(rql, {'x': eid, 'y': reid}, ('x', 'y'))
+        seteids = values.difference(origvalues)
+        if seteids:
+            rql = 'SET %s %s %s WHERE X eid %%(x)s, Y eid %%(y)s' % (
+                subjvar, rschema, objvar)
+            for reid in seteids:
+                self.req.execute(rql, {'x': eid, 'y': reid}, ('x', 'y'))
 
     def _get_eid(self, eid):
         # should be either an int (existant entity) or a variable (to be
--- a/web/views/editforms.py	Wed May 13 17:21:35 2009 +0200
+++ b/web/views/editforms.py	Wed May 13 19:51:30 2009 +0200
@@ -90,17 +90,19 @@
     # FIXME editableField class could be toggleable from userprefs
 
     onsubmit = ("return inlineValidateAttributeForm('%(divid)s-form', '%(rtype)s', "
-                "'%(eid)s', '%(divid)s', %(reload)s);")
+                "'%(eid)s', '%(divid)s', %(reload)s, '%(default)s');")
     ondblclick = "showInlineEditionForm(%(eid)s, '%(rtype)s', '%(divid)s')"
 
     def cell_call(self, row, col, rtype=None, role='subject', reload=False,
-                  vid='textoutofcontext', default=u''):
+                  vid='textoutofcontext', default=None):
         """display field to edit entity's `rtype` relation on double-click"""
         rschema = self.schema.rschema(rtype)
         entity = self.entity(row, col)
+        if not default:
+            default = self.req._('not specified')
         if rschema.is_final():
             if getattr(entity, rtype) is None:
-                value = default or self.req._('not specified')
+                value = default
             else:
                 value = entity.printable_value(rtype)
         else:
@@ -111,9 +113,9 @@
             self.w(value)
             return
         if rschema.is_final():
-            form = self._build_attribute_form(entity, value, rtype, role, reload, row, col)
+            form = self._build_attribute_form(entity, value, rtype, role, reload, row, col, default)
         else:
-            form = self._build_relation_form(entity, value, rtype, role, row, col, vid)
+            form = self._build_relation_form(entity, value, rtype, role, row, col, vid, default)
         form.form_add_hidden(u'__maineid', entity.eid)
         renderer = FormRenderer(display_label=False, display_help=False,
                                 display_fields=[(rtype, role)],
@@ -121,15 +123,17 @@
                                 display_progress_div=False)
         self.w(form.form_render(renderer=renderer))
 
-    def _build_relation_form(self, entity, value, rtype, role, row, col, vid):
+    def _build_relation_form(self, entity, value, rtype, role, row, col, vid, default):
         entity = self.entity(row, col)
         divid = 'd%s' % make_uid('%s-%s' % (rtype, entity.eid))
-        event_data = {'divid' : divid, 'eid' : entity.eid, 'rtype' : rtype, 'vid' : vid}
+        event_data = {'divid' : divid, 'eid' : entity.eid, 'rtype' : rtype, 'vid' : vid,
+                      'default' : default, 'role' : role}
         form = EntityFieldsForm(self.req, None, entity=entity, action='#',
                                 domid='%s-form' % divid,
                                 cssstyle='display: none',
                                 onsubmit=("return inlineValidateRelationForm('%(divid)s-form', '%(rtype)s', "
-                                          "'%(eid)s', '%(divid)s', '%(vid)s');" % event_data),
+                                          "'%(role)s', '%(eid)s', '%(divid)s', '%(vid)s', '%(default)s');" %
+                                          event_data),
                                 form_buttons=[SubmitButton(),
                                               Button(stdmsgs.BUTTON_CANCEL,
                                                      onclick="cancelInlineEdit(%s,\'%s\',\'%s\')" %\
@@ -141,11 +145,11 @@
                         ondblclick=self.ondblclick % event_data))
         return form
 
-    def _build_attribute_form(self, entity, value, rtype, role, reload, row, col):
+    def _build_attribute_form(self, entity, value, rtype, role, reload, row, col, default):
         eid = entity.eid
         divid = 'd%s' % make_uid('%s-%s' % (rtype, eid))
         event_data = {'divid' : divid, 'eid' : eid, 'rtype' : rtype,
-                      'reload' : dumps(reload)}
+                      'reload' : dumps(reload), 'default' : default}
         buttons = [SubmitButton(stdmsgs.BUTTON_OK),
                    Button(stdmsgs.BUTTON_CANCEL,
                           onclick="cancelInlineEdit(%s,\'%s\',\'%s\')" % (