|
30 | 30 | from datetime import datetime
|
31 | 31 | from numbers import Number
|
32 | 32 | from tools.translate import _
|
| 33 | +from tools import readonly |
33 | 34 | import six
|
34 | 35 | import tools
|
35 | 36 |
|
@@ -251,6 +252,38 @@ def exists(self, cr, uid, ids, context=None):
|
251 | 252 | res = [x for x in mongo_cr]
|
252 | 253 | return True if res else False
|
253 | 254 |
|
| 255 | + @readonly() |
| 256 | + def export_data2(self, cursor, uid, domain, limit, fields_to_export, format, |
| 257 | + context=None): |
| 258 | + def get_human_name(path): |
| 259 | + result = [] |
| 260 | + obj = self |
| 261 | + for f in path.split('.'): |
| 262 | + r = obj.fields_get(cursor, uid, [f], context=context) |
| 263 | + result.append(r.get(f, {'string': f})['string']) |
| 264 | + if 'relation' in r: |
| 265 | + obj = self.pool.get(r['relation']) |
| 266 | + return ' > '.join(result) |
| 267 | + |
| 268 | + ids = self.search(cursor, uid, domain, limit=limit, context=context) |
| 269 | + result = self.export_data(cursor, uid, ids, fields_to_export, context=context) |
| 270 | + import pandas as pd |
| 271 | + import base64 |
| 272 | + from io import BytesIO |
| 273 | + columns = [get_human_name(f) for f in fields_to_export] |
| 274 | + df = pd.DataFrame(result['datas'], columns=columns) |
| 275 | + # Respect the columns order |
| 276 | + buf = BytesIO() |
| 277 | + if format == 'csv': |
| 278 | + df.to_csv(buf, index=None, sep=str(';')) |
| 279 | + elif format == 'xlsx': |
| 280 | + xlsx_writer = pd.ExcelWriter(buf, engine='xlsxwriter') |
| 281 | + df.to_excel(xlsx_writer, index=None) |
| 282 | + xlsx_writer.save() |
| 283 | + res = {'datas': base64.b64encode(buf.getvalue()), 'format': format} |
| 284 | + buf.close() |
| 285 | + return res |
| 286 | + |
254 | 287 | def read(self, cr, user, ids, fields=None, context=None,
|
255 | 288 | load='_classic_read'):
|
256 | 289 |
|
|
0 commit comments