[IMP] add doc to Binary.saveas, and rename a field for clarity

bzr revid: xmo@openerp.com-20120110143518-ircd8x1feyf5rquf
This commit is contained in:
Xavier Morel 2012-01-10 15:35:18 +01:00
parent bf6e1eae28
commit ee9a9558c0
3 changed files with 21 additions and 7 deletions

View File

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

View File

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

View File

@ -748,7 +748,7 @@
<li t-foreach="attachments" t-as="attachment">
<t t-if="attachment.type == 'binary'" t-set="attachment.url" t-value="_s + '/web/binary/saveas?session_id='
+ session.session_id + '&amp;model=ir.attachment&amp;id=' + attachment.id
+ '&amp;field=datas&amp;fieldname=name&amp;t=' + (new Date().getTime())"/>
+ '&amp;field=datas&amp;filename_field=name&amp;t=' + (new Date().getTime())"/>
<a class="oe-sidebar-attachments-link" t-att-href="attachment.url" target="_blank">
<t t-esc="attachment.name"/>
</a>