[IMP] add doc to Binary.saveas, and rename a field for clarity
authorXavier Morel <xmo@openerp.com>
Tue, 10 Jan 2012 14:35:18 +0000 (15:35 +0100)
committerXavier Morel <xmo@openerp.com>
Tue, 10 Jan 2012 14:35:18 +0000 (15:35 +0100)
bzr revid: xmo@openerp.com-20120110143518-ircd8x1feyf5rquf

addons/web/controllers/main.py
addons/web/static/src/js/view_form.js
addons/web/static/src/xml/base.xml

index 852d513..7db8782 100644 (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)])
index 4f08e90..159fedb 100644 (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() {
index d4bb512..642f1f8 100644 (file)
         <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>