Skip to content

Commit

Permalink
[IMP] edi: minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
acsonefho committed Feb 18, 2025
1 parent 25ab3fc commit e0685da
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
56 changes: 35 additions & 21 deletions edi/models/edi_exchange_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,25 @@ class EDIExchangeRecord(models.Model):
),
]

@api.depends("type_id.code", "model", "res_id")
@api.model
def get_external_states(self):
return [
"input_received",
"output_sent",
]

@api.model
def get_not_check_constraint_states(self):
return [
"new",
"validate_error",
]

@api.depends("type_id.name", "model", "res_id")
def _compute_name(self):
for rec in self:
rec.name = "{} - {}".format(
rec.type_id.name, rec.record.name if rec.model else "Unrelated"
rec.type_id.name, rec.record.name if rec.model else _("Unrelated")
)

@api.depends("model", "type_id")
Expand All @@ -131,14 +145,16 @@ def _compute_exchange_filename(self):

@api.depends("edi_exchange_state")
def _compute_exchanged_on(self):
for rec in self:
if rec.edi_exchange_state in ("input_received", "output_sent"):
rec.exchanged_on = fields.Datetime.now()
states = self.get_external_states()
self.filtered(lambda r, st=states: r.edi_exchange_state in st).update(
{"exchanged_on": fields.Datetime.now()}
)

@api.constrains("edi_exchange_state")
def _constrain_edi_exchange_state(self):
states = self.get_not_check_constraint_states()
for rec in self:
if rec.edi_exchange_state in ("new", "validate_error"):
if rec.edi_exchange_state in states:
continue
if not rec.edi_exchange_state.startswith(rec.direction):
raise exceptions.ValidationError(
Expand Down Expand Up @@ -184,7 +200,7 @@ def record(self):
return None
if not self.model and self.parent_id:
return self.parent_id.record
return self.env[self.model].browse(self.res_id)
return self.env[self.model : str].browse(self.res_id)

def _set_file_content(
self, output_string, encoding="utf-8", field_name="exchange_file"
Expand Down Expand Up @@ -214,10 +230,11 @@ def name_get(self):
result.append((rec.id, name))
return result

@api.model
def create(self, vals):
vals["identifier"] = self._get_identifier()
return super().create(vals)
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
vals["identifier"] = self._get_identifier()
return super().create(vals_list)

def _get_identifier(self):
return self.env["ir.sequence"].next_by_code("edi.exchange")
Expand Down Expand Up @@ -391,12 +408,12 @@ def _search(
model_data = defaultdict(
lambda: defaultdict(set)
) # {res_model: {res_id: set(ids)}}
query = """SELECT id, res_id, model
FROM %(this_table)s
WHERE id = ANY %(ids);"""
for sub_ids in self.env.cr.split_for_in_conditions(ids):
self.env.cr.execute(
"""
SELECT id, res_id, model
FROM %(this_table)s
WHERE id = ANY %(ids);""",
query,
{"this_table": AsIs(self._table), "ids": list(sub_ids)},
)
for eid, res_id, model in self.env.cr.fetchall():
Expand Down Expand Up @@ -430,7 +447,7 @@ def _search(
return len(result) if count else list(result)

def read(self, fields=None, load="_classic_read"):
"""Override to explicitely call check_access_rule, that is not called
"""Override to explicitely call check_access, that is not called
by the ORM. It instead directly fetches ir.rules and apply them."""
self.check_access("read")
return super().read(fields=fields, load=load)
Expand All @@ -456,11 +473,8 @@ def check_access(self, operation):
for model, rec_ids in by_model_rec_ids.items():
records = self.env[model].browse(rec_ids)
checker = by_model_checker[model]
for record in records:
check_operation = checker(
[record.id], operation, model_name=record._name
)
record.check_access(check_operation)
check_operation = checker(records.ids, operation, model_name=records._name)
records.check_access(check_operation)

def write(self, vals):
self.check_access("write")
Expand Down
4 changes: 1 addition & 3 deletions edi/models/edi_exchange_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ def get_settings(self):
@api.constrains("backend_id", "backend_type_id")
def _check_backend(self):
for rec in self:
if not rec.backend_id:
continue
if rec.backend_id.backend_type_id != rec.backend_type_id:
if rec.backend_id and rec.backend_id.backend_type_id != rec.backend_type_id:
raise exceptions.UserError(_("Backend should respect backend type!"))

def _make_exchange_filename(self, exchange_record):
Expand Down

0 comments on commit e0685da

Please sign in to comment.