[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
This commit is contained in:
Fabien Meghazi 2012-02-08 11:39:35 +01:00
parent 5daa33329e
commit 739b0d4861
2 changed files with 44 additions and 4 deletions

View File

@ -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

View File

@ -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) {