summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/export.py
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2024-02-07 15:17:15 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2024-02-07 15:17:15 +0700
commit1a339c7e5b644add00c3bd7c623cb6d4553277cf (patch)
tree966b744ecde042810aebbf61b658ecfe31e59bc2 /indoteknik_api/controllers/export.py
parent953c3e611af5c57a8f7d57b5f2f651314c2a92a3 (diff)
parent20b1410fc2335f51ab08fdbecb54d6bfc437b6e1 (diff)
Merge branch 'feature/role-permission' into production
# Conflicts: # indoteknik_custom/__manifest__.py # indoteknik_custom/models/__init__.py
Diffstat (limited to 'indoteknik_api/controllers/export.py')
-rw-r--r--indoteknik_api/controllers/export.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/indoteknik_api/controllers/export.py b/indoteknik_api/controllers/export.py
new file mode 100644
index 00000000..c29c82c7
--- /dev/null
+++ b/indoteknik_api/controllers/export.py
@@ -0,0 +1,46 @@
+import json
+
+from odoo.tools import pycompat
+from odoo.exceptions import Warning
+from odoo import http
+from odoo.http import request
+from odoo.addons.web.controllers.main import ExportFormat, GroupExportXlsxWriter, ExportXlsxWriter, serialize_exception, clean_action
+
+class Export(ExportFormat, http.Controller):
+ @http.route('/web/export/xlsx', type='http', auth="public", csrf=False)
+ @serialize_exception
+ def export_xlsx(self, data, token, **kw):
+ data_obj = json.loads(data)
+ model = data_obj['model']
+ can_export = request.env.user.check_access(model, 'export')
+
+ if not can_export:
+ raise Warning('You are not allowed to export')
+
+ return self.base(data, token)
+
+ @property
+ def content_type(self):
+ return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
+
+ def filename(self, base):
+ return base + '.xlsx'
+
+ def from_group_data(self, fields, groups):
+ with GroupExportXlsxWriter(fields, groups.count) as xlsx_writer:
+ x, y = 1, 0
+ for group_name, group in groups.children.items():
+ x, y = xlsx_writer.write_group(x, y, group_name, group)
+
+ return xlsx_writer.value
+
+ def from_data(self, fields, rows):
+ with ExportXlsxWriter(fields, len(rows)) as xlsx_writer:
+ for row_index, row in enumerate(rows):
+ for cell_index, cell_value in enumerate(row):
+ if isinstance(cell_value, (list, tuple)):
+ cell_value = pycompat.to_text(cell_value)
+ xlsx_writer.write_cell(row_index + 1, cell_index, cell_value)
+
+ return xlsx_writer.value
+ \ No newline at end of file