From 78ffbdd65f0a58a7769047aab92ae612177406a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 9 Sep 2026 12:22:52 +0200 Subject: [PATCH] Use decorator before ModelView.button to set default records for cron task Closes #15076 (grafted from e92eb501cc50ea906fa839596f2c533b787126d6) --HG-- branch : 8.0 --- modules/account_fr_chorus/account.py | 46 ++++++++++++--------------- modules/edocument_peppol/edocument.py | 26 +++++++++------ 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/modules/account_fr_chorus/account.py b/modules/account_fr_chorus/account.py index fc49d33314..54ec707491 100644 --- a/modules/account_fr_chorus/account.py +++ b/modules/account_fr_chorus/account.py @@ -5,6 +5,7 @@ import datetime import logging import posixpath from collections import defaultdict +from functools import wraps from oauthlib.oauth2 import BackendApplicationClient, TokenExpiredError from requests_oauthlib import OAuth2Session @@ -204,6 +205,22 @@ class Invoice(metaclass=PoolMeta): InvoiceChorus.save(invoices_chorus) +def default_records(state): + def decorator(func): + @wraps(func) + def wrapper(cls, records=None): + transaction = Transaction() + if records is None: + records = cls.search([ + ('invoice.company', '=', + transaction.context.get('company')), + ('state', '=', state), + ]) + return func(cls, records) + return wrapper + return decorator + + class InvoiceChorus( Workflow, ModelSQL, ModelView, _SyntaxMixin, metaclass=PoolMeta): __name__ = 'account.invoice.chorus' @@ -337,20 +354,10 @@ class InvoiceChorus( } @classmethod - def send(cls, records=None): - transaction = Transaction() - if not records: - records = cls.search([ - ('invoice.company', '=', - transaction.context.get('company')), - ('state', '=', 'draft'), - ]) - cls.send_button(records) - - @classmethod + @default_records('draft') @ModelView.button @Workflow.transition('sent') - def send_button(cls, records=None): + def send(cls, records=None): """Send invoice to Chorus The transaction is committed after each invoice. @@ -396,20 +403,9 @@ class InvoiceChorus( } @classmethod - def update(cls, records=None): - transaction = Transaction() - - if not records: - records = cls.search([ - ('invoice.company', '=', - transaction.context.get('company')), - ('state', '=', 'sent'), - ]) - cls.update_button(records) - - @classmethod + @default_records('sent') @ModelView.button - def update_button(cls, records=None): + def update(cls, records=None): "Update state from Chorus" pool = Pool() Credential = pool.get('account.credential.chorus') diff --git a/modules/edocument_peppol/edocument.py b/modules/edocument_peppol/edocument.py index 581278e7e4..b2c8643292 100644 --- a/modules/edocument_peppol/edocument.py +++ b/modules/edocument_peppol/edocument.py @@ -1,6 +1,8 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. +from functools import wraps + from sql.conditionals import NullIf from sql.operators import Equal @@ -22,6 +24,18 @@ else: file_id = store_prefix = None +def default_records(func): + @wraps(func) + def wrapper(cls, documents=None): + if documents is None: + documents = cls.search([ + ('direction', '=', 'out'), + ('state', '=', 'processing'), + ]) + return func(cls, documents) + return wrapper + + class Peppol(Workflow, ModelSQL, ModelView): __name__ = 'edocument.peppol' @@ -239,17 +253,9 @@ class Peppol(Workflow, ModelSQL, ModelView): self.succeed() @classmethod - def update_status(cls, documents=None): - if documents is None: - documents = cls.search([ - ('direction', '=', 'out'), - ('state', '=', 'processing'), - ]) - cls.update_status_button(documents) - - @classmethod + @default_records @ModelView.button - def update_status_button(cls, documents): + def update_status(cls, documents): for document in documents: document._update_status() cls.save(documents)