Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP][14.0]: pattern_import_export: Add alert msg if used key is not … #105

Open
wants to merge 1 commit into
base: 14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions pattern_import_export/models/pattern_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import ast
import base64

from lxml import etree as ET

from odoo import _, api, fields, models
from odoo.osv import expression

Expand Down Expand Up @@ -50,6 +52,98 @@ class PatternConfig(models.Model):
pattern_file_ids = fields.One2many("pattern.file", "pattern_config_id")
process_multi = fields.Boolean()
job_priority = fields.Integer(default=20)
alert_display = fields.Boolean(compute="_compute_display_alert")
alert_msg = fields.Html(compute="_compute_display_alert")

@api.depends(
"export_fields.is_key", "export_fields.field1_id", "export_fields.field2_id"
)
def _compute_display_alert(self):
default_msg_tmpl = """
<div id="##msg_kind##_alert">
<div id="##msg_kind##_alert_title" class="font-weight-bold">
##title##
</div>
<div id="##msg_kind##_alert_msg" class="alert alert-warning" role="alert">
##alert_msg##
</div>
</div>
"""
for rec in self:
rec.alert_display = False
rec.alert_msg = False
for key_field in rec.export_fields.filtered(lambda x: x.is_key).sorted(
"sequence"
):
is_unique = False
is_translatable = False
if key_field.field2_id:
is_unique = rec._is_field_unique(key_field.field2_id)
is_translatable = key_field.field2_id.translate
else:
is_unique = rec._is_field_unique(key_field.field1_id)
is_translatable = key_field.field2_id.translate
rec.alert_display = not is_unique or is_translatable
if rec.alert_display:
if not rec.alert_msg:
rec.alert_msg = """
<div id="warning_msg">
<div id="key_warning_msg">
</div>
</div>
"""
if is_translatable:
msg_root = ET.fromstring(rec.alert_msg)
unique_snippet = default_msg_tmpl.replace(
"##msg_kind##", "translatable" + key_field.name
)
unique_snippet = unique_snippet.replace(
"##title##", _("translatable Key alert")
)
unique_snippet = unique_snippet.replace(
"##alert_msg##",
_(
'The field "%s" used as key is translatable. '
"You can got somme errors or los data by updating wrong record. "
"Make sur you log with the right language"
)
% key_field.name,
)
unique_snippet = unique_snippet.replace(
"alert alert-warning", "alert alert-info"
)
msg_root.insert(1, ET.fromstring(unique_snippet))
rec.alert_msg = ET.tostring(msg_root)
if not is_unique:
msg_root = ET.fromstring(rec.alert_msg)
unique_snippet = default_msg_tmpl.replace(
"##msg_kind##", "unique" + key_field.name
)
unique_snippet = unique_snippet.replace(
"##title##", _("Unique Key alert")
)
unique_snippet = unique_snippet.replace(
"##alert_msg##",
_(
'The field "%s" used as key is not unique. '
"You can got somme errors or los data by updating wrong record. "
)
% key_field.name,
)
msg_root.insert(1, ET.fromstring(unique_snippet))
rec.alert_msg = ET.tostring(msg_root)

def _is_field_unique(self, field):
is_unique = False
if not is_unique:
sql_constraints = self.env[field.model]._sql_constraints
for sql_constraint in sql_constraints:
sql_constraint_str = sql_constraint[1]
if "unique" in sql_constraint_str:
is_unique = field.name in sql_constraint_str
break

return is_unique

# we redefine previous onchanges since delegation inheritance breaks
# onchanges on ir.exports
Expand Down
25 changes: 20 additions & 5 deletions pattern_import_export/views/pattern_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,27 @@

<xpath expr="//form/group[1]" position="after">
<group name="general_info">
<field name="export_format" required="True" />
<field name="pattern_file" filename="pattern_file_name" />
<field name="pattern_file_name" invisible="1" />
<field name="id" invisible="1" />
<field name="header_format" />
<group name="general_info_data">
<field name="name" />
<field name="model_id" options="{'no_create': True}" />
<field name="export_format" required="True" />
<field name="pattern_file" filename="pattern_file_name" />
<field name="pattern_file_name" invisible="1" />
<field name="id" invisible="1" />
<field name="header_format" />
</group>
<group name="general_info_alert">
<field name="alert_display" invisible="1" />
<field
name="alert_msg"
attrs="{
'invisible': [('alert_display', '=', False)]}"
/>
</group>
</group>
</xpath>
<xpath expr="//form/group[1]" position="replace">
<!-- replaced group is added above -->
</xpath>
<xpath expr="//form/group[@name='general_info']" position="after">
<notebook>
Expand Down