Skip to content

Commit

Permalink
[IMP] edi_oca: add smart button in record form to show all related qu…
Browse files Browse the repository at this point in the history
…eue jobs
  • Loading branch information
QuocDuong1306 authored and thienvh332 committed Aug 21, 2024
1 parent c5b679e commit a08eed6
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 3 deletions.
2 changes: 1 addition & 1 deletion edi_oca/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ In order to define a new Exchange Record, we need to configure:
* Components

Jobs
~~~~~~~~~~~~~~~~~~~~
~~~~

1. Internal User: might be an EDI user without even knowing about it, triggering EDI flows by some of his actions on business records; does not need access to related queue jobs.

Expand Down
35 changes: 35 additions & 0 deletions edi_oca/models/edi_exchange_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import base64
import logging
from ast import literal_eval
from collections import defaultdict

from odoo import _, api, exceptions, fields, models
Expand Down Expand Up @@ -112,6 +113,10 @@ class EDIExchangeRecord(models.Model):
compute="_compute_retryable",
help="The record state can be rolled back manually in case of failure.",
)
related_queue_jobs_count = fields.Integer(
compute="_compute_related_queue_jobs_count"
)
company_id = fields.Many2one("res.company", string="Company")

_sql_constraints = [
("identifier_uniq", "unique(identifier)", "The identifier must be unique."),
Expand Down Expand Up @@ -620,3 +625,33 @@ def _inverse_res_id(self):
})
except (KeyError, ValueError, MissingError):
continue

def _compute_related_queue_jobs_count(self):
for rec in self:
# TODO: We should refactor the object field on queue_job to use jsonb field
# so that we can search directly into it.
rec.related_queue_jobs_count = rec.env["queue.job"].search_count(
[("func_string", "like", str(rec))]
)

def action_view_related_queue_jobs(self):
self.ensure_one()
xmlid = "queue_job.action_queue_job"
action = self.env.ref(xmlid).read()[0]
# Searching based on task name.
# Ex: `edi.exchange.record(1,).action_exchange_send()`
# TODO: We should refactor the object field on queue_job to use jsonb field
# so that we can search directly into it.
action["domain"] = [("func_string", "like", str(self))]
# Purge default search filters from ctx to avoid hiding records
ctx = action.get("context", {})
if isinstance(ctx, str):
ctx = literal_eval(ctx)
# Update the current contexts
ctx.update(self.env.context)
action["context"] = {
k: v for k, v in ctx.items() if not k.startswith("search_default_")
}
# Drop ID otherwise the context will be loaded from the action's record
action.pop("id")
return action
2 changes: 1 addition & 1 deletion edi_oca/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ In order to define a new Exchange Record, we need to configure:
* Components

Jobs
~~~~~~~~~~~~~~~~~~~~
~~~~

1. Internal User: might be an EDI user without even knowing about it, triggering EDI flows by some of his actions on business records; does not need access to related queue jobs.

Expand Down
12 changes: 12 additions & 0 deletions edi_oca/security/ir_model_access.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@
<field name="perm_write" eval="1" />
<field name="perm_unlink" eval="1" />
</record>

<!-- Access right to read related jobs on record -->
<record model="ir.model.access" id="access_queue_job_user">
<field name="name">access_queue_job edi user</field>
<field name="model_id" ref="queue_job.model_queue_job" />
<field name="group_id" ref="base_edi.group_edi_user" />
<field name="perm_read" eval="1" />
<field name="perm_create" eval="0" />
<field name="perm_write" eval="0" />
<field name="perm_unlink" eval="0" />
</record>

<record model="ir.model.access" id="access_edi_backend_type_user">
<field name="name">access_edi_backend_type user</field>
<field name="model_id" ref="model_edi_backend_type" />
Expand Down
16 changes: 16 additions & 0 deletions edi_oca/tests/test_backend_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class EDIBackendTestJobsCase(EDIBackendCommonTestCase, JobMixin):
def _setup_context(cls):
return dict(super()._setup_context(), test_queue_job_no_delay=None)

def _get_related_jobs(self, record):
# Use domain in action to find all related jobs
record.ensure_one()
action = record.action_view_related_queue_jobs()
return self.env["queue.job"].search(action["domain"])

def test_output(self):
job_counter = self.job_counter()
vals = {
Expand All @@ -30,6 +36,8 @@ def test_output(self):
self.assertEqual(
created.name, "Generate output content for given exchange record."
)
# Check related jobs
self.assertEqual(created, self._get_related_jobs(record))
with mock.patch.object(
type(self.backend), "_exchange_generate"
) as mocked_generate, mock.patch.object(
Expand All @@ -48,6 +56,9 @@ def test_output(self):
self.assertEqual(res, "Exchange sent")
self.assertEqual(record.edi_exchange_state, "output_sent")
self.assertEqual(created[0].name, "Send exchange file.")
# Check related jobs
record.invalidate_cache()
self.assertEqual(created, self._get_related_jobs(record))

def test_output_fail_retry(self):
job_counter = self.job_counter()
Expand Down Expand Up @@ -76,6 +87,8 @@ def test_input(self):
created = job_counter.search_created()
self.assertEqual(len(created), 1)
self.assertEqual(created.name, "Retrieve an incoming document.")
# Check related jobs
self.assertEqual(created, self._get_related_jobs(record))
with mock.patch.object(
type(self.backend), "_exchange_receive"
) as mocked_receive, mock.patch.object(
Expand Down Expand Up @@ -115,3 +128,6 @@ def test_input_processed_error(self):
new_created = job_counter.search_created() - created
# Should not create new job
self.assertEqual(len(new_created), 0)
# Check related jobs
record.invalidate_cache()
self.assertEqual(created, self._get_related_jobs(record))
2 changes: 1 addition & 1 deletion edi_oca/tests/test_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _setup_records(cls):
"name": "Poor Partner (not integrating one)",
"email": "[email protected]",
"login": "poorpartner",
"groups_id": [(6, 0, [cls.env.ref("base.group_user").id])],
"groups_id": [(6, 0, [cls.env.ref("base_edi.group_edi_user").id])],
}
)
)
Expand Down
17 changes: 17 additions & 0 deletions edi_oca/views/edi_exchange_record_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@
/>
</header>
<sheet>
<div class="oe_button_box" name="button_box">
<button
type="object"
class="oe_stat_button"
icon="fa-calendar"
name="action_view_related_queue_jobs"
attrs="{'invisible': [('related_queue_jobs_count', '=', 0)]}"
groups="base_edi.group_edi_user"
>
<field
string="Jobs"
name="related_queue_jobs_count"
widget="statinfo"
readonly="1"
/>
</button>
</div>
<div class="oe_title">
<label for="identifier" class="oe_edit_only" />
<h1>
Expand Down

0 comments on commit a08eed6

Please sign in to comment.