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

[12.0][BPRT] edi_oca: add smart button in record form to show all related queue jobs #34

Open
wants to merge 3 commits into
base: 12.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
11 changes: 10 additions & 1 deletion edi_oca/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ EDI
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:94c4f16b777e3058e707246c83141fbbefa255b7797d1f5131313f5c1b4bce9b
!! source digest: sha256:b91a993abd09a6569e0356331dda03f0d008aecd1e1e2bb36791d606a4a24c48
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down Expand Up @@ -59,6 +59,15 @@ In order to define a new Exchange Record, we need to configure:
* Backend
* 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.

2. EDI User: more conscious EDI user that might sometimes need to debug things a bit further and thus needs access to related queue jobs.

3. EDI Manager: full configuration access.

Component definition
~~~~~~~~~~~~~~~~~~~~

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
9 changes: 9 additions & 0 deletions edi_oca/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ In order to define a new Exchange Record, we need to configure:
* Backend
* 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.

2. EDI User: more conscious EDI user that might sometimes need to debug things a bit further and thus needs access to related queue jobs.

3. EDI Manager: full configuration access.

Component definition
~~~~~~~~~~~~~~~~~~~~

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
75 changes: 41 additions & 34 deletions edi_oca/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand All @@ -8,11 +9,10 @@

/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -275,7 +275,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: gray; } /* line numbers */
pre.code .ln { color: grey; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -301,7 +301,7 @@
span.pre {
white-space: pre }

span.problematic, pre.problematic {
span.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -367,7 +367,7 @@ <h1 class="title">EDI</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:94c4f16b777e3058e707246c83141fbbefa255b7797d1f5131313f5c1b4bce9b
!! source digest: sha256:b91a993abd09a6569e0356331dda03f0d008aecd1e1e2bb36791d606a4a24c48
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/edi/tree/12.0/edi_oca"><img alt="OCA/edi" src="https://img.shields.io/badge/github-OCA%2Fedi-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/edi-12-0/edi-12-0-edi_oca"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/edi&amp;target_branch=12.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>Base EDI backend.</p>
Expand All @@ -383,25 +383,26 @@ <h1 class="title">EDI</h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#configuration" id="toc-entry-1">Configuration</a><ul>
<li><a class="reference internal" href="#component-definition" id="toc-entry-2">Component definition</a></li>
<li><a class="reference internal" href="#user-edi-generation" id="toc-entry-3">User EDI generation</a></li>
<li><a class="reference internal" href="#exchange-type-rules-configuration" id="toc-entry-4">Exchange type rules configuration</a></li>
<li><a class="reference internal" href="#jobs" id="toc-entry-2">Jobs</a></li>
<li><a class="reference internal" href="#component-definition" id="toc-entry-3">Component definition</a></li>
<li><a class="reference internal" href="#user-edi-generation" id="toc-entry-4">User EDI generation</a></li>
<li><a class="reference internal" href="#exchange-type-rules-configuration" id="toc-entry-5">Exchange type rules configuration</a></li>
</ul>
</li>
<li><a class="reference internal" href="#usage" id="toc-entry-5">Usage</a><ul>
<li><a class="reference internal" href="#output-exchange-records" id="toc-entry-6">Output Exchange records</a></li>
<li><a class="reference internal" href="#input-exchange-records" id="toc-entry-7">Input Exchange records</a></li>
<li><a class="reference internal" href="#usage" id="toc-entry-6">Usage</a><ul>
<li><a class="reference internal" href="#output-exchange-records" id="toc-entry-7">Output Exchange records</a></li>
<li><a class="reference internal" href="#input-exchange-records" id="toc-entry-8">Input Exchange records</a></li>
</ul>
</li>
<li><a class="reference internal" href="#known-issues-roadmap" id="toc-entry-8">Known issues / Roadmap</a><ul>
<li><a class="reference internal" href="#section-1" id="toc-entry-9">14.0.1.0.0</a></li>
<li><a class="reference internal" href="#known-issues-roadmap" id="toc-entry-9">Known issues / Roadmap</a><ul>
<li><a class="reference internal" href="#section-1" id="toc-entry-10">14.0.1.0.0</a></li>
</ul>
</li>
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-10">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="toc-entry-11">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="toc-entry-12">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="toc-entry-13">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-14">Maintainers</a></li>
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-11">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="toc-entry-12">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="toc-entry-13">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="toc-entry-14">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-15">Maintainers</a></li>
</ul>
</li>
</ul>
Expand All @@ -419,8 +420,16 @@ <h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
<li>Backend</li>
<li>Components</li>
</ul>
<div class="section" id="jobs">
<h2><a class="toc-backref" href="#toc-entry-2">Jobs</a></h2>
<ol class="arabic simple">
<li>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.</li>
<li>EDI User: more conscious EDI user that might sometimes need to debug things a bit further and thus needs access to related queue jobs.</li>
<li>EDI Manager: full configuration access.</li>
</ol>
</div>
<div class="section" id="component-definition">
<h2><a class="toc-backref" href="#toc-entry-2">Component definition</a></h2>
<h2><a class="toc-backref" href="#toc-entry-3">Component definition</a></h2>
<p>The component usage must be defined like <cite>edi.{direction}.{kind}.{code}</cite> where:</p>
<ul class="simple">
<li>direction is <cite>output</cite> or <cite>input</cite></li>
Expand All @@ -429,15 +438,15 @@ <h2><a class="toc-backref" href="#toc-entry-2">Component definition</a></h2>
</ul>
</div>
<div class="section" id="user-edi-generation">
<h2><a class="toc-backref" href="#toc-entry-3">User EDI generation</a></h2>
<h2><a class="toc-backref" href="#toc-entry-4">User EDI generation</a></h2>
<p>On the exchange type, it might be possible to define a set of models, a domain and a
snippet of code.
After defining this fields, we will automatically see buttons on the view to generate
the exchange records.
This configuration is useful to define a way of generation managed by user.</p>
</div>
<div class="section" id="exchange-type-rules-configuration">
<h2><a class="toc-backref" href="#toc-entry-4">Exchange type rules configuration</a></h2>
<h2><a class="toc-backref" href="#toc-entry-5">Exchange type rules configuration</a></h2>
<p>Exchange types can be further configured with rules.
You can use rules to:</p>
<ol class="arabic simple">
Expand All @@ -456,13 +465,13 @@ <h2><a class="toc-backref" href="#toc-entry-4">Exchange type rules configuration
</div>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#toc-entry-5">Usage</a></h1>
<h1><a class="toc-backref" href="#toc-entry-6">Usage</a></h1>
<p>After certain operations or manual execution, Exchange records will be generated.
This Exchange records might be input records or outputs records.</p>
<p>The change of state can be manually executed by the system or be managed through by
<cite>ir.cron</cite>.</p>
<div class="section" id="output-exchange-records">
<h2><a class="toc-backref" href="#toc-entry-6">Output Exchange records</a></h2>
<h2><a class="toc-backref" href="#toc-entry-7">Output Exchange records</a></h2>
<p>An output record is intended to be used for exchange information from Odoo to another
system.</p>
<p>The flow of an output record should be:</p>
Expand All @@ -475,7 +484,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Output Exchange records</a></h2>
</ul>
</div>
<div class="section" id="input-exchange-records">
<h2><a class="toc-backref" href="#toc-entry-7">Input Exchange records</a></h2>
<h2><a class="toc-backref" href="#toc-entry-8">Input Exchange records</a></h2>
<p>An input record is intended to be used for exchange information another system to odoo.</p>
<p>The flow of an input record should be:</p>
<ul class="simple">
Expand All @@ -487,43 +496,41 @@ <h2><a class="toc-backref" href="#toc-entry-7">Input Exchange records</a></h2>
</div>
</div>
<div class="section" id="known-issues-roadmap">
<h1><a class="toc-backref" href="#toc-entry-8">Known issues / Roadmap</a></h1>
<h1><a class="toc-backref" href="#toc-entry-9">Known issues / Roadmap</a></h1>
<div class="section" id="section-1">
<h2><a class="toc-backref" href="#toc-entry-9">14.0.1.0.0</a></h2>
<h2><a class="toc-backref" href="#toc-entry-10">14.0.1.0.0</a></h2>
<p>The module name has been changed from edi to edi_oca.</p>
</div>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#toc-entry-10">Bug Tracker</a></h1>
<h1><a class="toc-backref" href="#toc-entry-11">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/edi/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/edi/issues/new?body=module:%20edi_oca%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h1><a class="toc-backref" href="#toc-entry-11">Credits</a></h1>
<h1><a class="toc-backref" href="#toc-entry-12">Credits</a></h1>
<div class="section" id="authors">
<h2><a class="toc-backref" href="#toc-entry-12">Authors</a></h2>
<h2><a class="toc-backref" href="#toc-entry-13">Authors</a></h2>
<ul class="simple">
<li>ACSONE</li>
<li>Creu Blanca</li>
<li>Camptocamp</li>
</ul>
</div>
<div class="section" id="contributors">
<h2><a class="toc-backref" href="#toc-entry-13">Contributors</a></h2>
<h2><a class="toc-backref" href="#toc-entry-14">Contributors</a></h2>
<ul class="simple">
<li>Simone Orsi &lt;<a class="reference external" href="mailto:simahawk&#64;gmail.com">simahawk&#64;gmail.com</a>&gt;</li>
<li>Enric Tobella &lt;<a class="reference external" href="mailto:etobella&#64;creublanca.es">etobella&#64;creublanca.es</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-14">Maintainers</a></h2>
<h2><a class="toc-backref" href="#toc-entry-15">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
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": "poor.partner@ododo.com",
"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
Loading
Loading