Skip to content
Extraits de code Groupes Projets
Valider 39b7150a rédigé par Benjamin - Le Filament's avatar Benjamin - Le Filament
Parcourir les fichiers

[add] queue job batch management

parent de2e86fc
Branches
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -78,6 +78,15 @@ class ScopCotisation(models.Model):
graph_values = fields.Text(compute="_compute_graph_values")
batch_id = fields.Many2one(
comodel_name="queue.job.batch",
name="Files d'attente en cours"
)
batch_ids_str = fields.Char()
batch_count = fields.Integer(
compute="_compute_batch_count"
)
# ------------------------------------------------------
# Contraintes SQL
# ------------------------------------------------------
......@@ -191,6 +200,13 @@ class ScopCotisation(models.Model):
elif cotiz.state != "ongoing":
cotiz.state = "ongoing"
def _compute_batch_count(self):
for base in self:
if not base.batch_ids_str:
base.batch_count = 0
else:
base.batch_count = len(base.batch_ids_str.split(","))
# ------------------------------------------------------
# Button functions
# ------------------------------------------------------
......@@ -286,6 +302,17 @@ class ScopCotisation(models.Model):
Create contribution for members
:params member_ids: list of member ids
"""
# Vérifie que des tâches ne sont pas en cours
self.ensure_one()
queue_ids = self.env["queue.job"].search([
("job_batch_id", "in", [int(x) for x in self.batch_ids_str.split(",")]),
("state", "not in", ["done", "cancelled", "failed"])
])
if queue_ids:
raise exceptions.UserError(_(
"Des tâches de création sont encore en cours."
))
member_to_invoice = self.env["res.partner"].browse(member_ids)
# Job queue
batch_name = (
......@@ -294,9 +321,22 @@ class ScopCotisation(models.Model):
+ self.year
)
batch = self.env["queue.job.batch"].get_new_batch(batch_name)
# Stocke le dernier batch en cours
self.batch_id = batch
# Stocke la liste des batchs au format texte
if not self.batch_ids_str:
self.batch_ids_str = str(batch.id)
else:
self.batch_ids_str += ", %s" % (str(batch.id),)
# Ajoute les bordereaux au batch
for member in member_to_invoice:
liasse_id = self.get_liasse(member)
self.with_context(job_batch=batch).with_delay().create_bordereau(
description = "Cotisation %s - Adhérent n°%s - %s" % (
self.year, member.member_number, member.name
)
self.with_context(
job_batch=batch).with_delay(
max_retries=3, description=description).create_bordereau(
member=member, liasse=liasse_id, nb_quarter="4", date=False
)
batch.enqueue()
......@@ -758,6 +798,20 @@ class ScopCotisation(models.Model):
"flags": {"mode": "readonly", "default_buttons": False},
}
def show_batch(self):
self.ensure_one()
batch_ids = self.env["queue.job.batch"].browse(
[int(x) for x in self.batch_ids_str.split(",")]
)
return {
"name": "Files d'attente",
"type": "ir.actions.act_window",
"view_type": "form",
"view_mode": "tree,form",
"res_model": "queue.job.batch",
"domain": [("id", "in", batch_ids.ids)],
}
# ------------------------------------------------------
# Global functions
# ------------------------------------------------------
......
......@@ -62,6 +62,16 @@
<field name="state" widget="statusbar" clickable="False" />
</header>
<sheet>
<div class="oe_button_box" name="contribution_button_box">
<button name="show_batch"
type="object"
class="oe_stat_button"
icon="fa-list"
attrs="{'invisible': [('batch_count', '=', 0)]}"
>
<field string="Files d'attente" name="batch_count" widget="statinfo" />
</button>
</div>
<h1><field name="name" /></h1>
<group>
<group name="contribution" string="Cotisations">
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter