[FIX] report action management: correctly download reports
authorXavier Morel <xmo@openerp.com>
Wed, 7 Sep 2011 07:23:58 +0000 (09:23 +0200)
committerXavier Morel <xmo@openerp.com>
Wed, 7 Sep 2011 07:23:58 +0000 (09:23 +0200)
bzr revid: xmo@openerp.com-20110907072358-94qnrzx1ww951shv

addons/web/controllers/main.py
addons/web/static/src/js/views.js

index 5a907fb..a5a7ddd 100644 (file)
@@ -23,8 +23,6 @@ openerpweb.nonliterals = web.common.nonliterals
 
 from babel.messages.pofile import read_po
 
-_REPORT_POLLER_DELAY = 0.05
-
 # Should move to openerpweb.Xml2Json
 class Xml2Json:
     # xml2json-direct
@@ -1325,22 +1323,45 @@ class ExcelExport(Export):
 
 class Reports(View):
     _cp_path = "/web/report"
+    POLLING_DELAY = 0.25
+    TYPES_MAPPING = {
+        'doc': 'application/vnd.ms-word',
+        'html': 'text/html',
+        'odt': 'application/vnd.oasis.opendocument.text',
+        'pdf': 'application/pdf',
+        'sxw': 'application/vnd.sun.xml.writer',
+        'xls': 'application/vnd.ms-excel',
+    }
 
-    @openerpweb.jsonrequest
-    def get_report(self, req, action):
-        report_srv = req.session.proxy("report")
-        context = req.session.eval_context(openerpweb.nonliterals.CompoundContext(req.context, \
-                                                                                  action["context"]))
+    @openerpweb.httprequest
+    def index(self, req, action, token):
+        action = simplejson.loads(action)
 
-        args = [req.session._db, req.session._uid, req.session._password, action["report_name"], context["active_ids"], {"id": context["active_id"], "model": context["active_model"], "report_type": action["report_type"]}, context]
-        report_id = report_srv.report(*args)
-        report = None
+        report_srv = req.session.proxy("report")
+        context = req.session.eval_context(
+            openerpweb.nonliterals.CompoundContext(
+                req.context or {}, action[ "context"]))
+        report_id = report_srv.report(
+            req.session._db, req.session._uid, req.session._password,
+            action["report_name"], context["active_ids"],
+            {"id": context["active_id"],
+             "model": context["active_model"],
+             "report_type": action["report_type"]},
+            context)
+        report_struct = None
         while True:
-            args2 = [req.session._db, req.session._uid, req.session._password, report_id]
-            report = report_srv.report_get(*args2)
-            if report["state"]:
+            report_struct = report_srv.report_get(
+                req.session._db, req.session._uid, req.session._password, report_id)
+            if report_struct["state"]:
                 break
-            time.sleep(_REPORT_POLLER_DELAY)
-
-        #TODO: ok now we've got the report, and so what?
-        return False
+            time.sleep(self.POLLING_DELAY)
+
+        report = base64.b64decode(report_struct['result'])
+        report_mimetype = self.TYPES_MAPPING.get(
+            report_struct['format'], 'octet-stream')
+        return req.make_response(report,
+             headers=[
+                 ('Content-Disposition', 'attachment; filename="%s.%s"' % (action['report_name'], action['report_type'])),
+                 ('Content-Type', report_mimetype),
+                 ('Content-Length', len(report))],
+             cookies={'fileToken': int(token)})
index 936106a..e351d49 100644 (file)
@@ -125,11 +125,11 @@ openerp.web.ActionManager = openerp.web.Widget.extend({
         (this.client_widget = new ClientWidget(this, action.params)).appendTo(this);
     },
     ir_actions_report_xml: function(action) {
-        this.rpc('/web/report/get_report', {
-            action: action,
-            context: {}
-        }).then(function(result) {
-            debugger;
+        $.blockUI();
+        this.session.get_file({
+            url: '/web/report',
+            data: {action: JSON.stringify(action)},
+            complete: $.unblockUI
         });
     }
 });