[IMP] Removed fields_view_get from controllers. Add a helper client side.
authorFabien Meghazi <fme@openerp.com>
Tue, 4 Dec 2012 16:39:48 +0000 (17:39 +0100)
committerFabien Meghazi <fme@openerp.com>
Tue, 4 Dec 2012 16:39:48 +0000 (17:39 +0100)
bzr revid: fme@openerp.com-20121204163948-pctxvy55w34e1pg9

addons/web/controllers/main.py
addons/web/static/src/js/search.js
addons/web/static/src/js/view_tree.js
addons/web/static/src/js/views.js

index 3f35b3f..f5710e8 100644 (file)
@@ -1121,41 +1121,6 @@ class DataSet(openerpweb.Controller):
 class View(openerpweb.Controller):
     _cp_path = "/web/view"
 
-    def fields_view_get(self, req, model, view_id, view_type,
-                        transform=True, toolbar=False, submenu=False):
-        Model = req.session.model(model)
-        fvg = Model.fields_view_get(view_id, view_type, req.context, toolbar, submenu)
-        # todo fme?: check that we should pass the evaluated context here
-        self.process_view(req.session, fvg, req.context, transform, (view_type == 'kanban'))
-        return fvg
-
-    def process_view(self, session, fvg, context, transform, preserve_whitespaces=False):
-        # depending on how it feels, xmlrpclib.ServerProxy can translate
-        # XML-RPC strings to ``str`` or ``unicode``. ElementTree does not
-        # enjoy unicode strings which can not be trivially converted to
-        # strings, and it blows up during parsing.
-
-        # So ensure we fix this retardation by converting view xml back to
-        # bit strings.
-        if isinstance(fvg['arch'], unicode):
-            arch = fvg['arch'].encode('utf-8')
-        else:
-            arch = fvg['arch']
-        fvg['arch_string'] = arch
-
-        fvg['arch'] = xml2json_from_elementtree(
-            ElementTree.fromstring(arch), preserve_whitespaces)
-
-        if 'id' in fvg['fields']:
-            # Special case for id's
-            id_field = fvg['fields']['id']
-            id_field['original_type'] = id_field['type']
-            id_field['type'] = 'id'
-
-        for field in fvg['fields'].itervalues():
-            for view in field.get("views", {}).itervalues():
-                self.process_view(session, view, None, transform)
-
     @openerpweb.jsonrequest
     def add_custom(self, req, view_id, arch):
         CustomView = req.session.model('ir.ui.view.custom')
@@ -1179,10 +1144,6 @@ class View(openerpweb.Controller):
             return {'result': True}
         return {'result': False}
 
-    @openerpweb.jsonrequest
-    def load(self, req, model, view_id, view_type, toolbar=False):
-        return self.fields_view_get(req, model, view_id, view_type, toolbar=toolbar)
-
 class TreeView(View):
     _cp_path = "/web/treeview"
 
index 3917f34..eea8bba 100644 (file)
@@ -341,12 +341,10 @@ instance.web.SearchView = instance.web.Widget.extend(/** @lends instance.web.Sea
         if (this.headless) {
             this.ready.resolve();
         } else {
-            var load_view = this.rpc("/web/view/load", {
-                model: this.model,
+            var load_view = instance.web.fields_view_get({
+                model: this.dataset._model,
                 view_id: this.view_id,
                 view_type: 'search',
-                context: instance.web.pyeval.eval(
-                    'context', this.dataset.get_context())
             });
 
             $.when(load_view).then(function (r) {
index 4d08da3..a68b92b 100644 (file)
@@ -9,6 +9,7 @@ var QWeb = instance.web.qweb,
 instance.web.views.add('tree', 'instance.web.TreeView');
 instance.web.TreeView = instance.web.View.extend(/** @lends instance.web.TreeView# */{
     display_name: _lt('Tree'),
+    view_type: 'tree',
     /**
      * Indicates that this view is not searchable, and thus that no search
      * view should be displayed (if there is one active).
@@ -36,18 +37,9 @@ instance.web.TreeView = instance.web.View.extend(/** @lends instance.web.TreeVie
         this.options = _.extend({}, this.defaults, options || {});
 
         _.bindAll(this, 'color_for');
+        this.on('view_loaded', self, self.load_tree);
     },
 
-    start: function () {
-        return this.rpc("/web/treeview/load", {
-            model: this.model,
-            view_id: this.view_id,
-            view_type: "tree",
-            toolbar: this.view_manager ? !!this.view_manager.sidebar : false,
-            context: instance.web.pyeval.eval(
-                    'context', this.dataset.get_context())
-        }).done(this.on_loaded);
-    },
     /**
      * Returns the list of fields needed to correctly read objects.
      *
@@ -64,7 +56,7 @@ instance.web.TreeView = instance.web.View.extend(/** @lends instance.web.TreeVie
         }
         return fields;
     },
-    on_loaded: function (fields_view) {
+    load_tree: function (fields_view) {
         var self = this;
         var has_toolbar = !!fields_view.arch.attrs.toolbar;
         // field name in OpenERP is kinda stupid: this is the name of the field
index 939c980..607e8fc 100644 (file)
@@ -1204,13 +1204,11 @@ instance.web.View = instance.web.Widget.extend({
         } else {
             if (! this.view_type)
                 console.warn("view_type is not defined", this);
-            view_loaded = this.rpc("/web/view/load", {
-                "model": this.dataset.model,
+            view_loaded = instance.web.fields_view_get({
+                "model": this.dataset._model,
                 "view_id": this.view_id,
                 "view_type": this.view_type,
-                toolbar: !!this.options.$sidebar,
-                context: instance.web.pyeval.eval(
-                    'context', this.dataset.get_context(context))
+                "toolbar": !!this.options.$sidebar,
             });
         }
         return view_loaded.then(function(r) {
@@ -1385,8 +1383,51 @@ instance.web.View = instance.web.Widget.extend({
     }
 });
 
+/**
+ * Performs a fields_view_get and apply postprocessing.
+ * return a {$.Deferred} resolved with the fvg
+ *
+ * @param {Object} [args]
+ * @param {String|Object} args.model instance.web.Model instance or string repr of the model
+ * @param {null|Object} args.context context if args.model is a string
+ * @param {null|Number} args.view_id id of the view to be loaded, default view if null
+ * @param {null|String} args.view_type type of view to be loaded if view_id is null
+ * @param {Boolean} [args.toolbar=false] get the toolbar definition
+ */
+instance.web.fields_view_get = function(args) {
+    function postprocess(fvg) {
+        fvg.arch_string = fvg.arch;
+        fvg.arch_doc = $.parseXML(fvg.arch);
+        fvg.arch = instance.web.xml_to_json(fvg.arch_doc);
+        if ('id' in fvg.fields) {
+            // Special case for id's
+            var id_field = fvg.fields['id'];
+            id_field.original_type = id_field.type;
+            id_field.type = 'id';
+        }
+        _.each(fvg.fields, function(field) {
+            _.each(field.views || {}, function(view) {
+                postprocess(view);
+            });
+        });
+        return fvg;
+    }
+    args = _.defaults(args, {
+        toolbar: false,
+    });
+    var model = args.model;
+    if (typeof(model) === 'string') {
+        model = new instance.web.Model(args.model, args.context);
+    }
+    return args.model.call('fields_view_get', [args.view_id, args.view_type, model.context(), args.toolbar]).then(function(fvg) {
+        return postprocess(fvg);
+    });
+};
+
 instance.web.xml_to_json = function(node) {
     switch (node.nodeType) {
+        case 9:
+            return instance.web.xml_to_json(node.documentElement);
         case 3:
         case 4:
             return node.data;
@@ -1455,26 +1496,6 @@ instance.web.xml_to_str = function(node) {
         throw new Error(_t("Could not serialize XML"));
     }
 };
-instance.web.str_to_xml = function(s) {
-    if (window.DOMParser) {
-        var dp = new DOMParser();
-        var r = dp.parseFromString(s, "text/xml");
-        if (r.body && r.body.firstChild && r.body.firstChild.nodeName == 'parsererror') {
-            throw new Error(_t("Could not parse string to xml"));
-        }
-        return r;
-    }
-    var xDoc;
-    try {
-        xDoc = new ActiveXObject("MSXML2.DOMDocument");
-    } catch (e) {
-        throw new Error(_.str.sprintf( _t("Could not find a DOM Parser: %s"), e.message));
-    }
-    xDoc.async = false;
-    xDoc.preserveWhiteSpace = true;
-    xDoc.loadXML(s);
-    return xDoc;
-}
 
 /**
  * Registry for all the main views