Skip to content

Commit

Permalink
Add module product_import_helper
Browse files Browse the repository at this point in the history
Move code from partner_import_helper to import_helper_base
  • Loading branch information
alexis-via committed Oct 6, 2023
1 parent 3116314 commit 5bfd166
Show file tree
Hide file tree
Showing 15 changed files with 517 additions and 155 deletions.
8 changes: 8 additions & 0 deletions import_helper_base/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
==================
Import Helper Base
==================

This is a technical module that contains common code for several import helper modules:

* partner_import_helper
* product_import_helper
1 change: 1 addition & 0 deletions import_helper_base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import wizards
21 changes: 21 additions & 0 deletions import_helper_base/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2023 Akretion France (http://www.akretion.com/)
# @author: Alexis de Lattre <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
'name': 'Import Helper Base',
'version': '16.0.1.0.0',
'category': 'Extra Tools',
'license': 'AGPL-3',
'summary': 'Common code for all import helper modules',
'author': 'Akretion',
'website': 'https://github.com/akretion/odoo-import-helper',
'depends': [
'base',
],
'data': [
'security/ir.model.access.csv',
'wizards/import_show_logs_view.xml',
],
'installable': True,
}
2 changes: 2 additions & 0 deletions import_helper_base/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_import_show_logs,Full access on import.show.logs wizard,model_import_show_logs,base.group_user,1,1,1,1
File renamed without changes.
142 changes: 142 additions & 0 deletions import_helper_base/wizards/import_show_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Copyright 2023 Akretion France (http://www.akretion.com/)
# @author: Alexis de Lattre <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, fields, models, tools, _
from odoo.exceptions import UserError
from collections import defaultdict
from datetime import datetime

import logging
logger = logging.getLogger(__name__)

try:
import openai
except ImportError:
logger.debug('Cannot import openai')

class ImportShowLogs(models.TransientModel):
_name = "import.show.logs"
_description = "Pop-up to show warnings after import"

logs = fields.Html(readonly=True)

@api.model
def _import_speedy(self, chatgpt=False):
logger.debug('Start to prepare import speedy')
speedy = {
'chatgpt': chatgpt,
'field2label': {},
'logs': [],
# 'logs' should contain a list of dict :
# {'msg': 'Checksum IBAN wrong',
# 'value': 'FR9879834739',
# 'vals': vals, # used to get the line
# (and display_name if partner has been created)
# 'field': 'res.partner,email',
# 'reset': True, # True if the data is NOT imported in Odoo
# }
}
if chatgpt:
openai_api_key = tools.config.get('openai_api_key', False)
if not openai_api_key:
raise UserError(_(
"Missing entry openai_api_key in the Odoo server configuration file."))
openai.api_key = openai_api_key
speedy['openai_tokens'] = 0
return speedy

def _field_label(self, field, speedy):
if field not in speedy['field2label']:
field_split = field.split(',')
ofield = self.env['ir.model.fields'].search([
('model', '=', field_split[0]),
('name', '=', field_split[1]),
], limit=1)
if ofield:
speedy['field2label'][field] = ofield.field_description
else:
speedy['field2label'][field] = '%s (%s)' % (
field_split[1], field_split[0])
return speedy['field2label'][field]

def _import_logs2html(self, speedy):
line2logs = defaultdict(list)
field2logs = defaultdict(list)
for log in speedy['logs']:
if log['vals'].get('line'):
line2logs[log['vals']['line']].append(log)
if log.get('field'):
field2logs[log['field']].append(log)
html = '<p><small>For the logs in <span style="color: red">red</span>, the data was <b>not imported</b> in Odoo</small><br/>'
if speedy.get('chatgpt'):
html += '<small><b>%d</b> OpenAI tokens where used</small></p>' % speedy['openai_tokens']
html += '<h1>Logs per line</h1>'
for line, logs in line2logs.items():
log_labels = []
for log in logs:
log_labels.append(
'<li style="color: %s"><b>%s</b>: <b>%s</b> - %s</li>' % (
log.get('reset') and 'red' or 'black',
self._field_label(log['field'], speedy),
log['value'],
log['msg'],
))
h3 = 'Line %s' % line
if log['vals'].get('id'):
h3 += ': %s (ID %d)' % (log['vals']['display_name'], log['vals']['id'])
html += '<h3>%s</h3>\n<p><ul>%s</ul></p>' % (h3, '\n'.join(log_labels))
html += '<h1>Logs per field</h1>'
for field, logs in field2logs.items():
log_labels = []
for log in logs:
line_label = 'Line %s' % log['vals'].get('line', 'unknown')
if log['vals'].get('id'):
line_label += ' (%s ID %d)' % (log['vals']['display_name'], log['vals']['id'])
log_labels.append(
'<li style="color: %s"><b>%s</b>: <b>%s</b> - %s</li>' % (
log.get('reset') and 'red' or 'black',
line_label,
log['value'],
log['msg'],
))
html += '<h3>%s</h3>\n<p><ul>%s</ul></p>' % (
self._field_label(field, speedy), '\n'.join(log_labels))
return html

def _import_result_action(self, speedy):
action = {
'name': 'Result',
'type': 'ir.actions.act_window',
'res_model': 'import.show.logs',
'view_mode': 'form',
'target': 'new',
'context': dict(self._context, default_logs=self._import_logs2html(speedy)),
}
return action

def _prepare_create_date(self, vals, speedy):
create_date = vals.get('create_date')
create_date_dt = False
if isinstance(create_date, str) and len(create_date) == 10:
try:
create_date_dt = datetime.strptime(create_date, '%Y-%m-%d')
except Exception as e:
speedy['logs'].append({
'msg': "Failed to convert '%s' to datetime: %s" % (create_date, e),
'value': vals['create_date'],
'vals': vals,
'field': 'product.product,create_date',
'reset': True,
})
elif isinstance(create_date, datetime):
create_date_dt = create_date
if create_date_dt and create_date_dt.date() > fields.Date.context_today(self):
speedy['logs'].append({
'msg': 'create_date %s cannot be in the future' % create_date_dt,
'value': create_date,
'vals': vals,
'field': 'product.product,create_date',
'reset': True,
})
return create_date_dt
1 change: 0 additions & 1 deletion partner_import_helper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from . import models
from . import wizards
10 changes: 3 additions & 7 deletions partner_import_helper/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
{
'name': 'Partner Import Helper',
'version': '16.0.1.0.0',
'category': 'Accounting',
'category': 'Extra Tools',
'license': 'AGPL-3',
'summary': 'Helper methods to import partners',
'author': 'Akretion',
'website': 'https://github.com/akretion/odoo-import-helper',
'depends': [
'base',
'phone_validation', # would be nice to avoid depending on it
],
'data': [
'security/ir.model.access.csv',
'wizards/import_show_logs_view.xml',
'import_helper_base',
'phone_validation', # would be nice to avoid depending on it ?
],
'installable': True,
}
Loading

0 comments on commit 5bfd166

Please sign in to comment.