Skip to content

Commit

Permalink
[MIG] edi_oca: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
john-herholz-dt committed Jan 18, 2024
1 parent 410d44e commit 3fc2d4d
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 106 deletions.
2 changes: 1 addition & 1 deletion edi_oca/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Define backends, exchange types, exchange records,
basic automation and views for handling EDI exchanges.
""",
"version": "16.0.1.2.1",
"version": "17.0.1.0.0",
"website": "https://github.com/OCA/edi-framework",
"development_status": "Beta",
"license": "LGPL-3",
Expand Down
5 changes: 4 additions & 1 deletion edi_oca/models/edi_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,10 @@ def _find_existing_exchange_records(
("backend_id", "=", self.id),
("type_id", "=", exchange_type.id),
] + extra_domain or []
return self.env["edi.exchange.record"].search(domain, count=count_only)
if count_only:
return self.env["edi.exchange.record"].search_count(domain)
else:
return self.env["edi.exchange.record"].search(domain)

def action_view_exchanges(self):
xmlid = "edi_oca.act_open_edi_exchange_record_view"
Expand Down
73 changes: 32 additions & 41 deletions edi_oca/models/edi_exchange_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,13 @@ def _get_file_content(
return res.decode() if not as_bytes else res
return self[field_name]

def name_get(self):
result = []
@api.depends("identifier", "res_id", "model", "type_id")
def _compute_display_name(self):
for rec in self:
rec_name = rec.identifier
if rec.res_id and rec.model:
rec_name = rec.record.display_name
name = f"[{rec.type_id.name}] {rec_name}"
result.append((rec.id, name))
return result
rec_name = (
rec.record.display_name if rec.res_id and rec.model else rec.identifier
)
rec.display_name = f"[{rec.type_id.name}] {rec_name}"

@api.model_create_multi
def create(self, vals_list):
Expand Down Expand Up @@ -439,46 +437,36 @@ def _notify_ack_received_error(self):
self._trigger_edi_event("done", suffix="ack_received_error")

@api.model
def _search(
self,
args,
offset=0,
limit=None,
order=None,
count=False,
access_rights_uid=None,
):
ids = super()._search(
args,
def _search(self, domain, offset=0, limit=None, order=None, access_rights_uid=None):
query = super()._search(
domain=domain,
offset=offset,
limit=limit,
order=order,
count=False,
access_rights_uid=access_rights_uid,
)
if self.env.is_system():
# restrictions do not apply to group "Settings"
return len(ids) if count else ids
return query

# TODO highlight orphaned EDI records in UI:
# - self.model + self.res_id are set
# - self.record returns empty recordset
# Remark: self.record is @property, not field

if not ids:
return 0 if count else []
orig_ids = ids
ids = set(ids)
if query.is_empty():
return query
orig_ids = list(query)
ids = set(orig_ids)
result = []
model_data = defaultdict(
lambda: defaultdict(set)
) # {res_model: {res_id: set(ids)}}
model_data = defaultdict(lambda: defaultdict(set))
for sub_ids in self._cr.split_for_in_conditions(ids):
self._cr.execute(
"""
SELECT id, res_id, model
FROM "%s"
WHERE id = ANY (%%(ids)s)"""
SELECT id, res_id, model
FROM "%s"
WHERE id = ANY (%%(ids)s)
"""
% self._table,
dict(ids=list(sub_ids)),
)
Expand Down Expand Up @@ -510,19 +498,22 @@ def _search(
for target_id in allowed:
result += list(targets[target_id])
if len(orig_ids) == limit and len(result) < len(orig_ids):
result.extend(
self._search(
args,
offset=offset + len(orig_ids),
limit=limit,
order=order,
count=count,
access_rights_uid=access_rights_uid,
)[: limit - len(result)]
extend_query = self._search(
domain,
offset=offset + len(orig_ids),
limit=limit,
order=order,
access_rights_uid=access_rights_uid,
)
extend_ids = list(extend_query)
result.extend(extend_ids[: limit - len(result)])

# Restore original ordering
result = [x for x in orig_ids if x in result]
return len(result) if count else list(result)
if set(orig_ids) != set(result):
# Create a virgin query
query = self.browse(result)._as_query()
return query

def read(self, fields=None, load="_classic_read"):
"""Override to explicitely call check_access_rule, that is not called
Expand Down
2 changes: 1 addition & 1 deletion edi_oca/static/src/xml/widget_edi.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates id="template" xml:space="preserve">
<div t-name="edi_oca.EdiConfigurationWidget" owl="1">
<div t-name="edi_oca.EdiConfigurationWidget">
<t t-foreach="props.value" t-as="rule_id" t-key="rule_index">
<t t-set="rule" t-value="props.value[rule_id]" />
<t t-if="rule.form">
Expand Down
2 changes: 1 addition & 1 deletion edi_oca/templates/exchange_mixin_buttons.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class="alert alert-warning"
role="alert"
style="margin-bottom:0px;"
attrs="{'invisible': [('edi_has_form_config', '=', False)]}"
invisible="not edi_has_form_config"
>
<div>
<span class="pr-4"><i class="fa fa-retweet" /> EDI actions</span>
Expand Down
3 changes: 2 additions & 1 deletion edi_oca/tests/test_consumer_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ def test_form(self):
Unfortunately we are unable to test the buttons here
"""
view = self.env[self.consumer_record._name].get_view(False, "form")
with Form(self.consumer_record) as f:
self.assertIn("edi_has_form_config", f._values)
self.assertIn("edi_config", f._values)
form = etree.fromstring(f._view["arch"])
form = etree.fromstring(view["arch"])
self.assertTrue(form.xpath("//field[@name='edi_has_form_config']"))
self.assertTrue(form.xpath("//field[@name='edi_config']"))
7 changes: 5 additions & 2 deletions edi_oca/tests/test_exchange_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from freezegun import freeze_time

from odoo.tools import mute_logger

from .common import EDIBackendCommonTestCase


Expand All @@ -24,8 +26,9 @@ def test_ack_for(self):
)

def test_same_code_same_backend(self):
with self.assertRaises(Exception) as err:
self.exchange_type_in.copy({"code": "test_csv_input"})
with mute_logger("odoo.sql_db"):
with self.assertRaises(Exception) as err:
self.exchange_type_in.copy({"code": "test_csv_input"})
err_msg = err.exception.args[0]
self.assertTrue(
err_msg.startswith("duplicate key value violates unique constraint")
Expand Down
20 changes: 5 additions & 15 deletions edi_oca/tests/test_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ def test_group_create(self):
def test_rule_no_create(self):
self.user.write({"groups_id": [(4, self.group.id)]})
self.consumer_record.name = "no_rule"
model = self.consumer_record
msg = rf"not allowed to modify '{model._description}' \({model._name}\)"
with self.assertRaisesRegex(AccessError, msg):
with self.assertRaisesRegex(AccessError, "doesn't have 'write' access to"):
self.create_record(self.user)

@mute_logger("odoo.addons.base.models.ir_model")
Expand All @@ -105,9 +103,7 @@ def test_no_group_no_create(self):
@mute_logger("odoo.addons.base.models.ir_model")
def test_no_group_no_read(self):
exchange_record = self.create_record()
model = self.consumer_record
msg = rf"not allowed to access '{model._description}' \({model._name}\)"
with self.assertRaisesRegex(AccessError, msg):
with self.assertRaisesRegex(AccessError, "You are not allowed to access"):
exchange_record.with_user(self.user).read()

@mute_logger("odoo.addons.base.models.ir_rule")
Expand All @@ -116,9 +112,7 @@ def test_rule_no_read(self):
self.user.write({"groups_id": [(4, self.group.id)]})
self.assertTrue(exchange_record.with_user(self.user).read())
self.consumer_record.name = "no_rule"
model = self.consumer_record
msg = rf"not allowed to access '{model._description}' \({model._name}\)"
with self.assertRaisesRegex(AccessError, msg):
with self.assertRaisesRegex(AccessError, "You are not allowed to access"):
exchange_record.with_user(self.user).read()

@mute_logger("odoo.addons.base.models.ir_model")
Expand All @@ -138,9 +132,7 @@ def test_rule_no_unlink(self):
exchange_record = self.create_record()
self.user.write({"groups_id": [(4, self.group.id)]})
self.consumer_record.name = "no_rule"
model = self.consumer_record
msg = rf"not allowed to modify '{model._description}' \({model._name}\)"
with self.assertRaisesRegex(AccessError, msg):
with self.assertRaisesRegex(AccessError, "doesn't have 'write' access to"):
exchange_record.with_user(self.user).unlink()

def test_no_group_no_search(self):
Expand Down Expand Up @@ -225,7 +217,5 @@ def test_rule_no_write(self):
exchange_record = self.create_record()
self.user.write({"groups_id": [(4, self.group.id)]})
self.consumer_record.name = "no_rule"
model = self.consumer_record
msg = rf"not allowed to modify '{model._description}' \({model._name}\)"
with self.assertRaisesRegex(AccessError, msg):
with self.assertRaisesRegex(AccessError, "doesn't have 'write' access"):
exchange_record.with_user(self.user).write({"external_identifier": "1234"})
2 changes: 1 addition & 1 deletion edi_oca/views/edi_backend_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
name="web_ribbon"
title="Archived"
bg_color="bg-danger"
attrs="{'invisible': [('active', '=', True)]}"
invisible="active"
/>
<div class="oe_button_box" name="button_box">
<button
Expand Down
59 changes: 25 additions & 34 deletions edi_oca/views/edi_exchange_record_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,26 @@
name="action_open_related_record"
type="object"
string="Related record"
attrs="{'invisible': [('related_record_exists', '=', False)]}"
invisible="not related_record_exists"
groups="base.group_no_one"
/>
<!-- FIXME: this `invisible` domain does not work -->
<button
name="action_open_related_exchanges"
type="object"
string="Related exchanges"
attrs="{'invisible': [('related_exchange_ids', '=', False)]}"
invisible="not related_exchange_ids"
/>
<button
name="action_retry"
type="object"
string="Retry"
attrs="{'invisible': [('retryable', '=', False)]}"
invisible="not retryable"
/>
<!-- FIXME: colors are ignored...
and can't find how they are supposed to work.
Probably not supported at all. -->
<field
attrs="{'invisible': [('direction', 'in', ('input', False))]}"
invisible="direction in ('input', False)"
name="edi_exchange_state"
widget="statusbar"
statusbar_visible="new,validate_error,output_pending,output_error_on_send,output_sent,output_sent_and_processed,output_sent_and_error"
Expand All @@ -73,7 +72,7 @@
}'
/>
<field
attrs="{'invisible': [('direction', 'in', ('output', False))]}"
invisible="direction in ('output', False)"
name="edi_exchange_state"
widget="statusbar"
statusbar_visible="new,validate_error,input_pending,input_received,input_receive_error,input_processed,input_processed_error"
Expand Down Expand Up @@ -110,16 +109,9 @@
<group name="status" string="Status">
<field name="exchanged_on" />
<field name="exchange_file" filename="exchange_filename" />
<field
name="exchange_filename"
attrs="{'invisible': [('exchange_file', '!=', False)]}"
/>
<field name="exchange_filename" invisible="exchange_file" />
</group>
<group
name="ack"
string="ACK"
attrs="{'invisible': [('ack_expected', '!=', True)]}"
>
<group name="ack" string="ACK" invisible="not ack_expected">
<field name="ack_expected" />
<field name="ack_exchange_id" />
<field name="ack_received_on" />
Expand All @@ -128,44 +120,43 @@
name="related_odoo_record"
string="Related record"
groups="base.group_no_one"
attrs="{'invisible': [('res_id', '=', 0)]}"
invisible="res_id == 0"
>
<field name="res_id" />
<field name="model" />
<span
<field name="res_id" />
<field name="model" />
<span
class="text-warning"
attrs="{'invisible': [('related_record_exists', '=', True)]}"
invisible="related_record_exists"
>
The related record is not available anymore.
Consider deleting this record too or fixing its relation.
</span>
The related record is not available anymore.
Consider deleting this record too or fixing its relation.
</span>
</group>
</group>
<notebook>
<page
name="error"
string="Error"
attrs="{'invisible': [('exchange_error', '=', False)]}"
invisible="not exchange_error"
>
<field name="exchange_error" />
</page>
<!-- FIXME: this `invisible` domain does not work -->
<page
name="related_exchanges"
string="Related exchanges"
attrs="{'invisible': [('related_exchange_ids', '=', False), ('parent_id', '=', False)]}"
invisible="not related_exchange_ids or not parent_id"
>
<group name="rel_exchanges">
<field name="parent_id" />
<field name="parent_id" />
</group>
<field name="related_exchange_ids" nolabel="1" colspan="4">
<tree>
<field name="type_id" />
<field name="identifier" />
<field name="exchanged_on" />
<field name="ack_received_on" />
<field name="edi_exchange_state" />
</tree>
<tree>
<field name="type_id" />
<field name="identifier" />
<field name="exchanged_on" />
<field name="ack_received_on" />
<field name="edi_exchange_state" />
</tree>
</field>
</page>
</notebook>
Expand Down
Loading

0 comments on commit 3fc2d4d

Please sign in to comment.