diff options
| author | Xavier Morel <xmo@openerp.com> | 2012-01-10 15:35:18 +0100 |
|---|---|---|
| committer | Xavier Morel <xmo@openerp.com> | 2012-01-10 15:35:18 +0100 |
| commit | ee9a9558c0f7a0e290ebcdfb0ce0b9be9751daf8 (patch) | |
| tree | 87fda8f667603a31fcfe86c02cae9aaca9b6d35f /addons/web/controllers | |
| parent | bf6e1eae289287e5701d2a0f4d8dbc1f6784cd93 (diff) | |
[IMP] add doc to Binary.saveas, and rename a field for clarity
bzr revid: xmo@openerp.com-20120110143518-ircd8x1feyf5rquf
Diffstat (limited to 'addons/web/controllers')
| -rw-r--r-- | addons/web/controllers/main.py | 24 |
1 files changed, 19 insertions, 5 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)]) |
