backport stable
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Mon, 14 Feb 2011 17:47:15 +0100
changeset 6991 eb2ba251f093
parent 6988 2f4a7d1e15b3 (current diff)
parent 6990 353ad06867a8 (diff)
child 6994 4b2c7e401065
backport stable
server/test/unittest_ldapuser.py
--- a/dataimport.py	Mon Feb 14 09:38:03 2011 +0100
+++ b/dataimport.py	Mon Feb 14 17:47:15 2011 +0100
@@ -306,11 +306,18 @@
         self.items.append(item)
         return len(self.items) - 1
 
-    def add(self, type, item):
+    def create_entity(self, etype, **data):
+        data['eid'] =  eid = self._put(etype, data)
+        self.eids[eid] = data
+        self.types.setdefault(etype, []).append(eid)
+        return data
+
+    @deprecated("[3.11] add is deprecated, use create_entity instead")
+    def add(self, etype, item):
         assert isinstance(item, dict), 'item is not a dict but a %s' % type(item)
-        eid = item['eid'] = self._put(type, item)
-        self.eids[eid] = item
-        self.types.setdefault(type, []).append(eid)
+        data = self.create_entity(etype, **item)
+        item['eid'] = data['eid']
+        return item
 
     def relate(self, eid_from, rtype, eid_to, inlined=False):
         """Add new relation"""
@@ -453,8 +460,10 @@
         return entity
 
     def _put(self, type, item):
-        query = ('INSERT %s X: ' % type) + ', '.join('X %s %%(%s)s' % (k, k)
-                                                     for k in item)
+        query = 'INSERT %s X' % type
+        if item:
+            query += ': ' + ', '.join('X %s %%(%s)s' % (k, k)
+                                      for k in item)
         return self.rql(query, item)[0][0]
 
     def relate(self, eid_from, rtype, eid_to, inlined=False):
@@ -686,7 +695,7 @@
         del entity.cw_extra_kwargs
         entity.cw_edited = EditedEntity(entity)
         for attr in self.etype_attrs:
-            entity.cw_edited.attribute_edited(attr, self.generate(entity, attr))
+            entity.cw_edited.edited_attribute(attr, self.generate(entity, attr))
         rels = {}
         for rel in self.etype_rels:
             rels[rel] = self.generate(entity, rel)
@@ -695,7 +704,7 @@
     def init_entity(self, entity):
         entity.eid = self.source.create_eid(self.session)
         for attr in self.entity_attrs:
-            entity.cw_edited.attribute_edited(attr, self.generate(entity, attr))
+            entity.cw_edited.edited_attribute(attr, self.generate(entity, attr))
 
     def generate(self, entity, rtype):
         return getattr(self, 'gen_%s' % rtype)(entity)
--- a/web/component.py	Mon Feb 14 09:38:03 2011 +0100
+++ b/web/component.py	Mon Feb 14 17:47:15 2011 +0100
@@ -577,6 +577,7 @@
         if mayadd or maydel:
             req.add_js(('jquery.ui.js', 'cubicweb.widgets.js'))
             req.add_js(('cubicweb.ajax.js', 'cubicweb.ajax.box.js'))
+            req.add_css('jquery.ui.css')
         _ = req._
         if related:
             w(u'<table class="ajaxEditRelationTable">')
@@ -597,8 +598,6 @@
         else:
             w(_('no related entity'))
         if mayadd:
-            req.add_js(('jquery.ui.js', 'cubicweb.widgets.js'))
-            req.add_css('jquery.ui.css')
             multiple = self.rdef.role_cardinality(self.role) in '*+'
             w(u'<table><tr><td>')
             jscall = unicode(js.ajaxBoxShowSelector(
--- a/web/data/cubicweb.ajax.box.js	Mon Feb 14 09:38:03 2011 +0100
+++ b/web/data/cubicweb.ajax.box.js	Mon Feb 14 17:47:15 2011 +0100
@@ -37,6 +37,26 @@
     });
 }
 
+/**
+ * .. function:: ajaxBoxShowSelector(boxid, eid, unrelfname,
+ *                                  addfname, msg,
+ *                                  oklabel, cancellabel,
+ *                                  separator=None)
+ *
+ * Display an ajax selector within a box of regid `boxid`, for entity with eid
+ * `eid`.
+ *
+ * Other parameters are:
+ *
+ * * `addfname`, name of the json controller method to call to add a relation
+ *
+ * * `msg`, message to display to the user when a relation has been added
+ *
+ * * `oklabel`/`cancellabel`, OK/cancel buttons label
+ *
+ * * `separator`, items separator if the field is multi-valued (will be
+ *   considered mono-valued when not specified)
+ */
 function ajaxBoxShowSelector(boxid, eid,
                              unrelfname,
                              addfname, msg,
@@ -59,11 +79,11 @@
                     ajaxBoxValidateSelectorInput(boxid, eid, separator, addfname, msg);
                 }
             });
-            $input.cwautocomplete(unrelated, {multiple: true});
+            $input.cwautocomplete(unrelated, {multiple: Boolean(separator)});
             var buttons = DIV({'class' : "sgformbuttons"},
                               A({href : "javascript: noop();",
                                  onclick : cw.utils.strFuncCall('ajaxBoxValidateSelectorInput',
-                                                                  boxid, eid, separator, addfname, msg)},
+                                                                boxid, eid, separator, addfname, msg)},
                                 oklabel),
                               ' / ',
                               A({'href' : "javascript: noop();",