From 739b0d4861dff958d80c2c6fa5c86b1c3ed329a7 Mon Sep 17 00:00:00 2001 From: Fabien Meghazi Date: Wed, 8 Feb 2012 11:39:35 +0100 Subject: [PATCH] [FIX] Binary#save_as does not send context. Breaks some accounting downloadable reports. Example of broken report was : Accounting > Reporting > Legal Reports > Belgium Statements > Periodical VAT Declaration bzr revid: fme@openerp.com-20120208103935-cv9nn9kg1vssosgm --- addons/web/controllers/main.py | 31 +++++++++++++++++++++++++++ addons/web/static/src/js/view_form.js | 17 +++++++++++---- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index 5338604e76b..9cb0698be4a 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -1256,6 +1256,37 @@ class Binary(openerpweb.Controller): [('Content-Type', 'application/octet-stream'), ('Content-Disposition', 'attachment; filename="%s"' % filename)]) + @openerpweb.httprequest + def saveas_ajax(self, req, data, token): + jdata = simplejson.loads(data) + model = jdata['model'] + field = jdata['field'] + id = jdata.get('id', None) + filename_field = jdata.get('filename_field', None) + context = jdata.get('context', dict()) + + context = req.session.eval_context(context) + Model = req.session.model(model) + fields = [field] + if filename_field: + fields.append(filename_field) + if id: + res = Model.read([int(id)], fields, context)[0] + else: + res = Model.default_get(fields, context) + filecontent = base64.b64decode(res.get(field, '')) + if not filecontent: + raise ValueError("No content found for field '%s' on '%s:%s'" % + (field, model, id)) + else: + filename = '%s_%s' % (model.replace('.', '_'), id) + if filename_field: + filename = res.get(filename_field, '') or filename + return req.make_response(filecontent, + headers=[('Content-Type', 'application/octet-stream'), + ('Content-Disposition', 'attachment; filename="%s"' % filename)], + cookies={'fileToken': int(token)}) + @openerpweb.httprequest def upload(self, req, callback, ufile): # TODO: might be useful to have a configuration flag for max-length file uploads diff --git a/addons/web/static/src/js/view_form.js b/addons/web/static/src/js/view_form.js index de9bc618048..ca1c2c29e6e 100644 --- a/addons/web/static/src/js/view_form.js +++ b/addons/web/static/src/js/view_form.js @@ -3122,10 +3122,19 @@ openerp.web.form.FieldBinary = openerp.web.form.Field.extend({ on_file_uploaded_and_valid: function(size, name, content_type, file_base64) { }, 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 + - '&filename_field=' + (this.node.attrs.filename || '') + '&t=' + (new Date().getTime()); - window.open(url); + $.blockUI(); + this.session.get_file({ + url: '/web/binary/saveas_ajax', + data: {data: JSON.stringify({ + model: this.view.dataset.model, + id: (this.view.datarecord.id || ''), + field: this.name, + filename_field: (this.node.attrs.filename || ''), + context: this.view.dataset.get_context() + })}, + complete: $.unblockUI, + error: openerp.webclient.crashmanager.on_rpc_error + }); }, on_clear: function() { if (this.value !== false) {