Sample Input Instruction as Dictionary
@@ -166,7 +288,7 @@
Normally, this will be within templates.xml file within addons.
-
+
{
'__EXPORT__': {
'sale_order': { # sheet can be name (string) or index (integer)
@@ -203,7 +325,6 @@
-
XLSX Templates
ir.actions.act_window
@@ -216,15 +337,16 @@
-
-
-
-
-
+
+
diff --git a/excel_import_export/wizard/export_xlsx_wizard.py b/excel_import_export/wizard/export_xlsx_wizard.py
index 1807ea7e9..be63c745f 100644
--- a/excel_import_export/wizard/export_xlsx_wizard.py
+++ b/excel_import_export/wizard/export_xlsx_wizard.py
@@ -1,82 +1,68 @@
# Copyright 2019 Ecosoft Co., Ltd (http://ecosoft.co.th/)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)
-from odoo import models, fields, api, _
+from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
class ExportXLSXWizard(models.TransientModel):
""" This wizard is used with the template (xlsx.template) to export
xlsx template filled with data form the active record """
- _name = 'export.xlsx.wizard'
- _description = 'Wizard for exporting excel'
- name = fields.Char(
- string='File Name',
- readonly=True,
- size=500,
- )
- data = fields.Binary(
- string='File',
- readonly=True,
- )
+ _name = "export.xlsx.wizard"
+ _description = "Wizard for exporting excel"
+
+ name = fields.Char(string="File Name", readonly=True, size=500,)
+ data = fields.Binary(string="File", readonly=True,)
template_id = fields.Many2one(
- 'xlsx.template',
- string='Template',
- required=True,
- ondelete='cascade',
- domain=lambda self: self._context.get('template_domain', []),
- )
- res_id = fields.Integer(
- string='Resource ID',
- readonly=True,
+ "xlsx.template",
+ string="Template",
required=True,
+ ondelete="cascade",
+ domain=lambda self: self._context.get("template_domain", []),
)
+ res_id = fields.Integer(string="Resource ID", readonly=True, required=True,)
res_model = fields.Char(
- string='Resource Model',
- readonly=True,
- required=True,
- size=500,
+ string="Resource Model", readonly=True, required=True, size=500,
)
state = fields.Selection(
- [('choose', 'Choose'),
- ('get', 'Get')],
- default='choose',
+ [("choose", "Choose"), ("get", "Get")],
+ default="choose",
help="* Choose: wizard show in user selection mode"
- "\n* Get: wizard show results from user action",
+ "\n* Get: wizard show results from user action",
)
@api.model
def default_get(self, fields):
- res_model = self._context.get('active_model', False)
- res_id = self._context.get('active_id', False)
- template_domain = self._context.get('template_domain', [])
- templates = self.env['xlsx.template'].search(template_domain)
+ res_model = self._context.get("active_model", False)
+ res_id = self._context.get("active_id", False)
+ template_domain = self._context.get("template_domain", [])
+ templates = self.env["xlsx.template"].search(template_domain)
if not templates:
- raise ValidationError(_('No template found'))
+ raise ValidationError(_("No template found"))
defaults = super(ExportXLSXWizard, self).default_get(fields)
for template in templates:
if not template.datas:
- raise ValidationError(_('No file in %s') % (template.name,))
- defaults['template_id'] = len(templates) == 1 and templates.id or False
- defaults['res_id'] = res_id
- defaults['res_model'] = res_model
+ raise ValidationError(_("No file in %s") % (template.name,))
+ defaults["template_id"] = len(templates) == 1 and templates.id or False
+ defaults["res_id"] = res_id
+ defaults["res_model"] = res_model
return defaults
@api.multi
def action_export(self):
self.ensure_one()
- Export = self.env['xlsx.export']
- out_file, out_name = Export.export_xlsx(self.template_id,
- self.res_model,
- self.res_id)
- self.write({'state': 'get', 'data': out_file, 'name': out_name})
+ Export = self.env["xlsx.export"]
+ out_file, out_name = Export.export_xlsx(
+ self.template_id, self.res_model, self.res_id
+ )
+ self.write({"state": "get", "data": out_file, "name": out_name})
return {
- 'type': 'ir.actions.act_window',
- 'res_model': 'export.xlsx.wizard',
- 'view_mode': 'form',
- 'view_type': 'form',
- 'res_id': self.id,
- 'views': [(False, 'form')],
- 'target': 'new',
+ "type": "ir.actions.act_window",
+ "res_model": "export.xlsx.wizard",
+ "view_mode": "form",
+ "view_type": "form",
+ "res_id": self.id,
+ "views": [(False, "form")],
+ "target": "new",
}
diff --git a/excel_import_export/wizard/export_xlsx_wizard.xml b/excel_import_export/wizard/export_xlsx_wizard.xml
index b39ec97c7..d8d97f91d 100644
--- a/excel_import_export/wizard/export_xlsx_wizard.xml
+++ b/excel_import_export/wizard/export_xlsx_wizard.xml
@@ -1,39 +1,50 @@
-
+
-
export.xlsx.wizard
export.xlsx.wizard
-
-
diff --git a/excel_import_export/wizard/import_xlsx_wizard.py b/excel_import_export/wizard/import_xlsx_wizard.py
index d3835551e..3c785814d 100644
--- a/excel_import_export/wizard/import_xlsx_wizard.py
+++ b/excel_import_export/wizard/import_xlsx_wizard.py
@@ -1,161 +1,156 @@
# Copyright 2019 Ecosoft Co., Ltd (http://ecosoft.co.th/)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)
-from odoo import models, fields, api, _
-from odoo.exceptions import ValidationError, RedirectWarning
+from odoo import _, api, fields, models
+from odoo.exceptions import RedirectWarning, ValidationError
class ImportXLSXWizard(models.TransientModel):
""" This wizard is used with the template (xlsx.template) to import
xlsx template back to active record """
- _name = 'import.xlsx.wizard'
- _description = 'Wizard for importing excel'
- import_file = fields.Binary(
- string='Import File (*.xlsx)',
- )
+ _name = "import.xlsx.wizard"
+ _description = "Wizard for importing excel"
+
+ import_file = fields.Binary(string="Import File (*.xlsx)",)
template_id = fields.Many2one(
- 'xlsx.template',
- string='Template',
+ "xlsx.template",
+ string="Template",
required=True,
- ondelete='set null',
- domain=lambda self: self._context.get('template_domain', []),
- )
- res_id = fields.Integer(
- string='Resource ID',
- readonly=True,
- )
- res_model = fields.Char(
- string='Resource Model',
- readonly=True,
- size=500,
- )
- datas = fields.Binary(
- string='Sample',
- related='template_id.datas',
- readonly=True,
+ ondelete="set null",
+ domain=lambda self: self._context.get("template_domain", []),
)
+ res_id = fields.Integer(string="Resource ID", readonly=True,)
+ res_model = fields.Char(string="Resource Model", readonly=True, size=500,)
+ datas = fields.Binary(string="Sample", related="template_id.datas", readonly=True,)
fname = fields.Char(
- string='Template Name',
- related='template_id.fname',
- readonly=True,
+ string="Template Name", related="template_id.fname", readonly=True,
)
attachment_ids = fields.Many2many(
- 'ir.attachment',
- string='Import File(s) (*.xlsx)',
+ "ir.attachment",
+ string="Import File(s) (*.xlsx)",
required=True,
help="You can select multiple files to import.",
)
state = fields.Selection(
- [('choose', 'Choose'),
- ('get', 'Get')],
- default='choose',
+ [("choose", "Choose"), ("get", "Get")],
+ default="choose",
help="* Choose: wizard show in user selection mode"
- "\n* Get: wizard show results from user action",
+ "\n* Get: wizard show results from user action",
)
@api.model
def view_init(self, fields_list):
""" This template only works on some context of active record """
res = super(ImportXLSXWizard, self).view_init(fields_list)
- res_model = self._context.get('active_model', False)
- res_id = self._context.get('active_id', False)
+ res_model = self._context.get("active_model", False)
+ res_id = self._context.get("active_id", False)
if not res_model or not res_id:
return res
record = self.env[res_model].browse(res_id)
messages = []
valid = True
# For all import, only allow import in draft state (for documents)
- import_states = self._context.get('template_import_states', [])
+ import_states = self._context.get("template_import_states", [])
if import_states: # states specified in context, test this
- if 'state' in record and \
- record['state'] not in import_states:
- messages.append(
- _('Document must be in %s states') % import_states)
+ if "state" in record and record["state"] not in import_states:
+ messages.append(_("Document must be in %s states") % import_states)
valid = False
else: # no specific state specified, test with draft
- if 'state' in record and 'draft' not in record['state']: # not in
- messages.append(_('Document must be in draft state'))
+ if "state" in record and "draft" not in record["state"]: # not in
+ messages.append(_("Document must be in draft state"))
valid = False
# Context testing
- if self._context.get('template_context', False):
- template_context = self._context['template_context']
+ if self._context.get("template_context", False):
+ template_context = self._context["template_context"]
for key, value in template_context.items():
- if key not in record or \
- (record._fields[key].type == 'many2one' and
- record[key].id or record[key]) != value:
+ if (
+ key not in record
+ or (
+ record._fields[key].type == "many2one"
+ and record[key].id
+ or record[key]
+ )
+ != value
+ ):
valid = False
messages.append(
- _('This import action is not usable '
- 'in this document context'))
+ _(
+ "This import action is not usable "
+ "in this document context"
+ )
+ )
break
if not valid:
- raise ValidationError('\n'.join(messages))
+ raise ValidationError("\n".join(messages))
return res
@api.model
def default_get(self, fields):
- res_model = self._context.get('active_model', False)
- res_id = self._context.get('active_id', False)
- template_domain = self._context.get('template_domain', [])
- templates = self.env['xlsx.template'].search(template_domain)
+ res_model = self._context.get("active_model", False)
+ res_id = self._context.get("active_id", False)
+ template_domain = self._context.get("template_domain", [])
+ templates = self.env["xlsx.template"].search(template_domain)
if not templates:
- raise ValidationError(_('No template found'))
+ raise ValidationError(_("No template found"))
defaults = super(ImportXLSXWizard, self).default_get(fields)
for template in templates:
if not template.datas:
- act = self.env.ref('excel_import_export.action_xlsx_template')
+ act = self.env.ref("excel_import_export.action_xlsx_template")
raise RedirectWarning(
- _('File "%s" not found in template, %s.') %
- (template.fname, template.name),
- act.id, _('Set Templates'))
- defaults['template_id'] = len(templates) == 1 and template.id or False
- defaults['res_id'] = res_id
- defaults['res_model'] = res_model
+ _('File "%s" not found in template, %s.')
+ % (template.fname, template.name),
+ act.id,
+ _("Set Templates"),
+ )
+ defaults["template_id"] = len(templates) == 1 and template.id or False
+ defaults["res_id"] = res_id
+ defaults["res_model"] = res_model
return defaults
@api.multi
def get_import_sample(self):
self.ensure_one()
return {
- 'name': _('Import Excel'),
- 'type': 'ir.actions.act_window',
- 'res_model': 'import.xlsx.wizard',
- 'view_mode': 'form',
- 'view_type': 'form',
- 'res_id': self.id,
- 'views': [(False, 'form')],
- 'target': 'new',
- 'context': self._context.copy()
+ "name": _("Import Excel"),
+ "type": "ir.actions.act_window",
+ "res_model": "import.xlsx.wizard",
+ "view_mode": "form",
+ "view_type": "form",
+ "res_id": self.id,
+ "views": [(False, "form")],
+ "target": "new",
+ "context": self._context.copy(),
}
@api.multi
def action_import(self):
self.ensure_one()
- Import = self.env['xlsx.import']
+ Import = self.env["xlsx.import"]
res_ids = []
if self.import_file:
- record = Import.import_xlsx(self.import_file, self.template_id,
- self.res_model, self.res_id)
+ record = Import.import_xlsx(
+ self.import_file, self.template_id, self.res_model, self.res_id
+ )
res_ids = [record.id]
elif self.attachment_ids:
for attach in self.attachment_ids:
record = Import.import_xlsx(attach.datas, self.template_id)
res_ids.append(record.id)
else:
- raise ValidationError(_('Please select Excel file to import'))
+ raise ValidationError(_("Please select Excel file to import"))
# If redirect_action is specified, do redirection
if self.template_id.redirect_action:
vals = self.template_id.redirect_action.read()[0]
- vals['domain'] = [('id', 'in', res_ids)]
+ vals["domain"] = [("id", "in", res_ids)]
return vals
- self.write({'state': 'get'})
+ self.write({"state": "get"})
return {
- 'type': 'ir.actions.act_window',
- 'res_model': self._name,
- 'view_mode': 'form',
- 'view_type': 'form',
- 'res_id': self.id,
- 'views': [(False, 'form')],
- 'target': 'new',
+ "type": "ir.actions.act_window",
+ "res_model": self._name,
+ "view_mode": "form",
+ "view_type": "form",
+ "res_id": self.id,
+ "views": [(False, "form")],
+ "target": "new",
}
diff --git a/excel_import_export/wizard/import_xlsx_wizard.xml b/excel_import_export/wizard/import_xlsx_wizard.xml
index 0e91e789d..6f8a68d54 100644
--- a/excel_import_export/wizard/import_xlsx_wizard.xml
+++ b/excel_import_export/wizard/import_xlsx_wizard.xml
@@ -1,32 +1,47 @@
-
+
-
import.xlsx.wizard
import.xlsx.wizard
-