diff --git a/addons/web_graph/__init__.py b/addons/web_graph/__init__.py index e69de29bb2d..ee5959455ad 100644 --- a/addons/web_graph/__init__.py +++ b/addons/web_graph/__init__.py @@ -0,0 +1 @@ +import controllers diff --git a/addons/web_graph/controllers/__init__.py b/addons/web_graph/controllers/__init__.py new file mode 100644 index 00000000000..039d9715fab --- /dev/null +++ b/addons/web_graph/controllers/__init__.py @@ -0,0 +1 @@ +import main \ No newline at end of file diff --git a/addons/web_graph/controllers/main.py b/addons/web_graph/controllers/main.py new file mode 100644 index 00000000000..1df85d08f59 --- /dev/null +++ b/addons/web_graph/controllers/main.py @@ -0,0 +1,57 @@ +from openerp import http +import simplejson +from openerp.http import request, serialize_exception as _serialize_exception + +try: + import xlwt +except ImportError: + xlwt = None + + +class TableExporter(http.Controller): + + @http.route('/web_graph/check_xlwt', type='json', auth='none') + def check_xlwt(self): + return xlwt is not None + + + @http.route('/web_graph/export_xls', type='http', auth="user") + def export_xls(self, data, token): + jdata = simplejson.loads(data) + model = jdata['test'] + # field = jdata['field'] + # data = jdata['data'] + # id = jdata.get('id', None) + # filename_field = jdata.get('filename_field', None) + # context = jdata.get('context', {}) + filecontent='argh' + print model + print xlwt + + return request.make_response(filecontent, + headers=[('Content-Type', 'application/vnd.ms-excel'), + ('Content-Disposition', 'attachment; filename=table.xls;')], + cookies={'fileToken': token}) + + # Model = request.session.model(model) + # fields = [field] + # if filename_field: + # fields.append(filename_field) + # if data: + # res = { field: data } + # elif 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 request.make_response(filecontent, + # headers=[('Content-Type', 'application/octet-stream'), + # ('Content-Disposition', content_disposition(filename))], + # cookies={'fileToken': token}) diff --git a/addons/web_graph/static/src/js/graph_widget.js b/addons/web_graph/static/src/js/graph_widget.js index ef547e22632..bd367f29ed1 100644 --- a/addons/web_graph/static/src/js/graph_widget.js +++ b/addons/web_graph/static/src/js/graph_widget.js @@ -39,6 +39,10 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({ this.$('.graph_heatmap label').addClass('disabled'); } + openerp.session.rpc('/web_graph/check_xlwt').then(function (result) { + if (result) { self.$('.graph_options_selection label').toggle(true); } + }); + return this.model.call('fields_get', []).then(function (f) { self.fields = f; self.fields.__count = {field:'__count', type: 'integer', string:_t('Quantity')}; @@ -264,6 +268,9 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({ case 'update_values': this.pivot.update_data().then(this.proxy('display_data')); break; + case 'export_data': + this.export_xls(); + break; } }, @@ -362,7 +369,7 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({ this.$('.graph_main_content svg').remove(); this.$('.graph_main_content div').remove(); this.table.empty(); - this.table.toggleClass('heatmap', this.heatmap_mode !== 'none') + this.table.toggleClass('heatmap', this.heatmap_mode !== 'none'); this.width = this.$el.width(); this.height = Math.min(Math.max(document.documentElement.clientHeight - 116 - 60, 250), Math.round(0.8*this.$el.width())); @@ -693,6 +700,91 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({ }); }, + // ---------------------------------------------------------------------- + // Convert Pivot data structure into table structure + // table = {headers: [header_rows], + // measure_row: [_], + // rows: [row]} + // header_rows = [header_cells], + // header_cells = {width:_, height:_, title:_, id:_} + // rows = {id:_, indent:_, title:_, cells: [cell]} + // cell = {row_id:_, col_id:_, value:_} + // ---------------------------------------------------------------------- + build_table: function() { + return { + headers: this.build_headers(), + measure_row: this.build_measure_row(), + rows: this.build_rows() + }; + }, + + build_headers: function () { + var pivot = this.pivot, + height = _.max(_.map(pivot.cols.headers, function(g) {return g.path.length;})), + rows = []; + + _.each(pivot.cols.headers, function (col) { + if (col.path.length === 0) { return;} + var cell_width = col.expanded ? pivot.get_ancestors(col).length : 1; + var cell_height = height - col.path.length + 1; + var cell = {width: cell_width, height: cell_height, title: col.title, id: col.id}; + debugger; + if (rows[col.path.length - 1]) { + rows[col.path.length - 1].push(cell); + } else { + rows[col.path.length - 1] = [cell]; + } + }); + + return rows; + }, + + build_measure_row: function () { + var nbr_leaves = this.pivot.get_cols_leaves().length, + nbr_cols = nbr_leaves + ((nbr_leaves > 1) ? 1 : 0), + result = [], + i, m; + for (i = 0; i < nbr_cols; i++) { + for (m = 0; m < this.pivot.measures.length; m++) { + result.push(this.pivot.measures[m].string); + } + } + return result; + }, + + build_rows: function () { + return []; + }, + + // ---------------------------------------------------------------------- + // Controller stuff... + // ---------------------------------------------------------------------- + export_xls: function() { + var c = openerp.webclient.crashmanager; + + var table = this.build_table(); + console.log(table); + return; + openerp.web.blockUI(); + this.session.get_file({ + url: '/web_graph/export_xls', + data: {data: JSON.stringify({ + test: table + // model: this.view.dataset.model, + // id: (this.view.datarecord.id || ''), + // field: this.name, + // filename_field: (this.node.attrs.filename || ''), + // data: instance.web.form.is_bin_size(value) ? null : value, + // context: this.view.dataset.get_context() + })}, + complete: openerp.web.unblockUI, + error: c.rpc_error.bind(c) + }); + }, + + + + }); // Utility function: returns true if the beginning of array2 is array1 and diff --git a/addons/web_graph/static/src/js/pivot_table.js b/addons/web_graph/static/src/js/pivot_table.js index 770519e52c2..f77d49f9a1a 100644 --- a/addons/web_graph/static/src/js/pivot_table.js +++ b/addons/web_graph/static/src/js/pivot_table.js @@ -127,6 +127,10 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({ return this._get_headers_with_depth(this.rows.headers, depth); }, + get_ancestor_leaves: function (header) { + return _.where(this.get_ancestors_and_self(header), {expanded:false}); + }, + // return all non expanded rows get_rows_leaves: function () { return _.where(this.rows.headers, {expanded:false}); diff --git a/addons/web_graph/static/src/xml/web_graph.xml b/addons/web_graph/static/src/xml/web_graph.xml index deb34e3d8a8..81804e5a9b1 100644 --- a/addons/web_graph/static/src/xml/web_graph.xml +++ b/addons/web_graph/static/src/xml/web_graph.xml @@ -41,6 +41,9 @@ +