From f320a1e1b82d6ea7431e6459392037bf490d1db1 Mon Sep 17 00:00:00 2001 From: Peter Weber Date: Tue, 10 Dec 2024 12:29:50 +0100 Subject: [PATCH] acquisition: add logging file to rollover cli * Adds logging file parameter to rollover cli. * Adds time stamp to log file if no file parameter is given. * Closes #3560. Co-Authored-by: Peter Weber --- rero_ils/config.py | 2 +- .../modules/acquisition/acq_order_lines/api.py | 1 + rero_ils/modules/acquisition/cli.py | 7 ++++++- rero_ils/modules/acquisition/rollover.py | 15 ++++++++++++++- .../api/acquisition/test_acquisition_rollover.py | 2 ++ 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/rero_ils/config.py b/rero_ils/config.py index 7ce7b5ec4d..d66fc6599f 100644 --- a/rero_ils/config.py +++ b/rero_ils/config.py @@ -3482,7 +3482,7 @@ def _(x): "console": {"class": "logging.StreamHandler", "formatter": "standard"}, "file": { "class": "logging.handlers.RotatingFileHandler", - "filename": "logs/rollover_log", + "filename": "logs/rollover", "backupCount": 10, "formatter": "standard", }, diff --git a/rero_ils/modules/acquisition/acq_order_lines/api.py b/rero_ils/modules/acquisition/acq_order_lines/api.py index 7073b5c787..5f43a809c7 100644 --- a/rero_ils/modules/acquisition/acq_order_lines/api.py +++ b/rero_ils/modules/acquisition/acq_order_lines/api.py @@ -255,6 +255,7 @@ def status(self): if self.order_date else AcqOrderLineStatus.APPROVED ) + received_quantity = self.received_quantity # not use the property to prevent an extra ES call unreceived_quantity = self.quantity - received_quantity diff --git a/rero_ils/modules/acquisition/cli.py b/rero_ils/modules/acquisition/cli.py index 9db1f166c5..7a5788127b 100644 --- a/rero_ils/modules/acquisition/cli.py +++ b/rero_ils/modules/acquisition/cli.py @@ -57,6 +57,7 @@ def acquisition(): "--budget-start-date", "budget_start_date", help="The new budget start-date" ) @click.option("--budget-end-date", "budget_end_date", help="The new budget end-date") +@click.option("--logging_file", "logging_file", help="Logging file name", default=None) @with_appcontext def rollover( origin_budget_pid, @@ -66,6 +67,7 @@ def rollover( budget_name, budget_start_date, budget_end_date, + logging_file, ): """CLI to run rollover process between two acquisition budgets.""" # Check parameters @@ -92,6 +94,9 @@ def rollover( destination_budget = Budget.get_record_by_pid(dest_budget_pid) rollover_runner = AcqRollover( - original_budget, destination_budget, is_interactive=interactive + original_budget=original_budget, + destination_budget=destination_budget, + is_interactive=interactive, + logging_file=logging_file, ) rollover_runner.run() diff --git a/rero_ils/modules/acquisition/rollover.py b/rero_ils/modules/acquisition/rollover.py index 346ba17979..ccf013176a 100644 --- a/rero_ils/modules/acquisition/rollover.py +++ b/rero_ils/modules/acquisition/rollover.py @@ -18,10 +18,13 @@ """Rollover on acquisition resource.""" + +import contextlib import logging.config import random import string from copy import deepcopy +from datetime import datetime from elasticsearch_dsl import Q from flask import current_app @@ -117,6 +120,7 @@ def __init__( original_budget, destination_budget=None, logging_config=None, + logging_file=None, is_interactive=True, propagate_errors=False, **kwargs, @@ -130,6 +134,8 @@ def __init__( :param logging_config: (optional) a dictionary containing all necessary configuration to log the rollover process result. If not specified the configuration comes from `ROLLOVER_LOGGING_CONFIG` setting. + :param logging_file: (optional) Logging file name. If not specified + the configuration comes from `ROLLOVER_LOGGING_CONFIG` setting is used. :param is_interactive: boolean to determine if user confirmation is required. True by default. :param propagate_errors: Boolean to determine if error will be @@ -148,7 +154,14 @@ def __init__( self.propagate_errors = propagate_errors # Set special logging configuration for rollover process - default_config = current_app.config.get("ROLLOVER_LOGGING_CONFIG") + default_config = deepcopy(current_app.config.get("ROLLOVER_LOGGING_CONFIG")) + if logging_file: + default_config["handlers"]["file"]["filename"] = logging_file + else: + time_stamp = datetime.now().strftime("%Y%m%d_%H%M") + file_name = default_config["handlers"]["file"]["filename"] + file_name = f"{file_name}_{time_stamp}.log" + default_config["handlers"]["file"]["filename"] = file_name logging.config.dictConfig(logging_config or default_config) self.logger = logging.getLogger(__name__) self.logger.info("ROLLOVER PROCESS ==================================") diff --git a/tests/api/acquisition/test_acquisition_rollover.py b/tests/api/acquisition/test_acquisition_rollover.py index 60752ee19c..08eb313943 100644 --- a/tests/api/acquisition/test_acquisition_rollover.py +++ b/tests/api/acquisition/test_acquisition_rollover.py @@ -64,6 +64,8 @@ def test_rollover_cli(client, acq_full_structure_a, org_martigny): "2022-01-01", "--budget-end-date", "2022-12-31", + "--logging_file", + "logs/rollover_log", ], ) assert result.exit_code == 0 # all works fine !