From ee9a9558c0f7a0e290ebcdfb0ce0b9be9751daf8 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 10 Jan 2012 15:35:18 +0100 Subject: [PATCH 1/4] [IMP] add doc to Binary.saveas, and rename a field for clarity bzr revid: xmo@openerp.com-20120110143518-ircd8x1feyf5rquf --- addons/web/controllers/main.py | 24 +++++++++++++++++++----- addons/web/static/src/js/view_form.js | 2 +- addons/web/static/src/xml/base.xml | 2 +- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index 852d5138177..7db8782b52e 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -1184,20 +1184,34 @@ class Binary(openerpweb.Controller): return open(os.path.join(addons_path, 'web', 'static', 'src', 'img', 'placeholder.png'), 'rb').read() @openerpweb.httprequest - def saveas(self, req, model, id, field, fieldname, **kw): + def saveas(self, req, model, field, id=None, filename_field=None, **kw): + """ Download link for files stored as binary fields. + + If the ``id`` parameter is omitted, fetches the default value for the + binary field (via ``default_get``), otherwise fetches the field for + that precise record. + + :param req: OpenERP request + :type req: :class:`web.common.http.HttpRequest` + :param str model: name of the model to fetch the binary from + :param str field: binary field + :param str id: id of the record from which to fetch the binary + :param str filename_field: field holding the file's name, if any + :returns: :class:`werkzeug.wrappers.Response` + """ Model = req.session.model(model) context = req.session.eval_context(req.context) if id: - res = Model.read([int(id)], [field, fieldname], context)[0] + res = Model.read([int(id)], [field, filename_field], context)[0] else: - res = Model.default_get([field, fieldname], context) + res = Model.default_get([field, filename_field], context) filecontent = base64.b64decode(res.get(field, '')) if not filecontent: return req.not_found() else: filename = '%s_%s' % (model.replace('.', '_'), id) - if fieldname: - filename = res.get(fieldname, '') or filename + if filename_field: + filename = res.get(filename_field, '') or filename return req.make_response(filecontent, [('Content-Type', 'application/octet-stream'), ('Content-Disposition', 'attachment; filename=' + filename)]) diff --git a/addons/web/static/src/js/view_form.js b/addons/web/static/src/js/view_form.js index 4f08e909f3e..159fedbc71e 100644 --- a/addons/web/static/src/js/view_form.js +++ b/addons/web/static/src/js/view_form.js @@ -3023,7 +3023,7 @@ openerp.web.form.FieldBinary = openerp.web.form.Field.extend({ on_save_as: function() { var url = '/web/binary/saveas?session_id=' + this.session.session_id + '&model=' + this.view.dataset.model +'&id=' + (this.view.datarecord.id || '') + '&field=' + this.name + - '&fieldname=' + (this.node.attrs.filename || '') + '&t=' + (new Date().getTime()); + '&filename_field=' + (this.node.attrs.filename || '') + '&t=' + (new Date().getTime()); window.open(url); }, on_clear: function() { diff --git a/addons/web/static/src/xml/base.xml b/addons/web/static/src/xml/base.xml index d4bb5122500..642f1f89709 100644 --- a/addons/web/static/src/xml/base.xml +++ b/addons/web/static/src/xml/base.xml @@ -748,7 +748,7 @@
  • + + '&field=datas&filename_field=name&t=' + (new Date().getTime())"/> From 4cc9cb83cede980383709dfebe6ce48d436aaa7f Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 10 Jan 2012 15:35:56 +0100 Subject: [PATCH 2/4] [IMP] add bin_size flag to listview reads in order not to fetch binary file contents bzr revid: xmo@openerp.com-20120110143556-ijsmmhvenw93vzfb --- addons/web/static/src/js/view_list.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/web/static/src/js/view_list.js b/addons/web/static/src/js/view_list.js index 1c4911974fe..6b1621af74c 100644 --- a/addons/web/static/src/js/view_list.js +++ b/addons/web/static/src/js/view_list.js @@ -1289,7 +1289,7 @@ openerp.web.ListView.Groups = openerp.web.Class.extend( /** @lends openerp.web.L page = this.datagroup.openable ? this.page : view.page; var fields = _.pluck(_.select(this.columns, function(x) {return x.tag == "field"}), 'name'); - var options = { offset: page * limit, limit: limit }; + var options = { offset: page * limit, limit: limit, context: {bin_size: true} }; //TODO xmo: investigate why we need to put the setTimeout $.async_when().then(function() {dataset.read_slice(fields, options , function (records) { // FIXME: ignominious hacks, parents (aka form view) should not send two ListView#reload_content concurrently From d98b8b8be0145700667c0f4cc0e7619dec790c5b Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 10 Jan 2012 16:39:05 +0100 Subject: [PATCH 3/4] [ADD] handling of binary fields to the listview bzr revid: xmo@openerp.com-20120110153905-zxqkze9c4zrkmv2a --- addons/web/controllers/main.py | 7 ++-- addons/web/static/src/js/formats.js | 49 ++++++++++++++++++++------- addons/web/static/src/js/view_list.js | 20 +++++++++-- 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index 7db8782b52e..c775927a8cc 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -1201,10 +1201,13 @@ class Binary(openerpweb.Controller): """ Model = req.session.model(model) context = req.session.eval_context(req.context) + fields = [field] + if filename_field: + fields.append(filename_field) if id: - res = Model.read([int(id)], [field, filename_field], context)[0] + res = Model.read([int(id)], fields, context)[0] else: - res = Model.default_get([field, filename_field], context) + res = Model.default_get(fields, context) filecontent = base64.b64decode(res.get(field, '')) if not filecontent: return req.not_found() diff --git a/addons/web/static/src/js/formats.js b/addons/web/static/src/js/formats.js index 2418439ac46..4076c190a74 100644 --- a/addons/web/static/src/js/formats.js +++ b/addons/web/static/src/js/formats.js @@ -237,7 +237,15 @@ openerp.web.auto_date_to_str = function(value, type) { }; /** - * Formats a provided cell based on its field type + * Formats a provided cell based on its field type. Most of the field types + * return a correctly formatted value, but some tags and fields are + * special-cased in their handling: + * + * * buttons will return an actual ``