From e76ec5792c68eb723c233dfd726d2889cacabca8 Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Sun, 2 Jun 2024 10:27:15 +0200 Subject: [PATCH] config: new field 'holidays_info' to show generated holidays in config-form --- config.py | 29 +++++++++++++++++++++++++++++ locale/de.po | 8 ++++++++ locale/en.po | 8 ++++++++ tests/planner.py | 14 +++++++++----- view/configuration_form.xml | 2 +- 5 files changed, 55 insertions(+), 6 deletions(-) diff --git a/config.py b/config.py index 66d5e41..e20aaea 100644 --- a/config.py +++ b/config.py @@ -9,6 +9,9 @@ from dateutil.easter import ( easter, EASTER_JULIAN, EASTER_ORTHODOX, EASTER_WESTERN) from trytond.pool import PoolMeta, Pool from trytond.model import fields +from trytond.transaction import Transaction +from trytond.report import Report + holidays = fields.Char( string='Holidays', help='Semicolon separate list of dates: ' + @@ -21,6 +24,32 @@ class Configuration(metaclass=PoolMeta): __name__ = 'cashbook.configuration' holidays = fields.MultiValue(holidays) + holidays_info = fields.Function(fields.Char( + string='Holidays', readonly=True, + help='Holidays in the current year.'), 'on_change_with_holidays_info') + + @fields.depends('holidays') + def on_change_with_holidays_info(self, name=None): + """ get string of generated holidays for years in + context-value 'holiday_years' or current year + + Args: + name (str, optional): field. Defaults to None. + + Returns: + str: formatted holidays in language of user + """ + pool = Pool() + Config = pool.get('cashbook.configuration') + context = Transaction().context + + years = context.get('holiday_years', []) + cfg1 = Config.get_singleton() + if cfg1: + dates = cfg1.holiday_dates(years) + dates.sort() + return '|'.join([ + Report.format_date(x) for x in dates]) def holiday_dates(self, years=[]): """ get list of dates for list of years diff --git a/locale/de.po b/locale/de.po index 5d4d025..87af133 100644 --- a/locale/de.po +++ b/locale/de.po @@ -94,6 +94,14 @@ msgctxt "help:cashbook.configuration,holidays:" msgid "Semicolon separate list of dates: yyyy-mm-dd = single date, mm-dd = annual repetition, easter[greg|jul|orth] = Easter Sunday, ascension = Ascension Day, whitsun = Whitsunday, offset with :+/-n e.g.: easter:+1 = Easter Monday" msgstr "Semikolon getrennte Liste von Datumswerten: yyyy-mm-dd = Einzeldatum, mm-dd = jährliche Wiederholung, easter[greg|jul|orth] = Ostersonntag, ascension = Christi Himmelfahrt, whitsun = Pfingstsonntag, Offset mit :+/-n z.B: easter:+1 = Ostermontag" +msgctxt "field:cashbook.configuration,holidays_info:" +msgid "Holidays" +msgstr "Feiertage" + +msgctxt "help:cashbook.configuration,holidays_info:" +msgid "Holidays in the current year." +msgstr "Feiertage im aktuellen Jahr." + #################### # cashbook.planner # diff --git a/locale/en.po b/locale/en.po index 814fbea..cb63fac 100644 --- a/locale/en.po +++ b/locale/en.po @@ -70,6 +70,14 @@ msgctxt "help:cashbook.configuration,holidays:" msgid "Semicolon separate list of dates: yyyy-mm-dd = single date, mm-dd = annual repetition, easter[greg|jul|orth] = Easter Sunday, ascension = Ascension Day, whitsun = Whitsunday, offset with :+/-n e.g.: easter:+1 = Easter Monday" msgstr "Semicolon separate list of dates: yyyy-mm-dd = single date, mm-dd = annual repetition, easter[greg|jul|orth] = Easter Sunday, ascension = Ascension Day, whitsun = Whitsunday, offset with :+/-n e.g.: easter:+1 = Easter Monday" +msgctxt "field:cashbook.configuration,holidays_info:" +msgid "Holidays" +msgstr "Holidays" + +msgctxt "help:cashbook.configuration,holidays_info:" +msgid "Holidays in the current year." +msgstr "Holidays in the current year." + msgctxt "model:cashbook.planner,name:" msgid "Scheduled Booking" msgstr "Scheduled Booking" diff --git a/tests/planner.py b/tests/planner.py index a250efd..c15066d 100644 --- a/tests/planner.py +++ b/tests/planner.py @@ -156,11 +156,15 @@ class PlannerTestCase(object): Config.holiday_parseconfig(None), {'definition': '', 'dates': []}) - cfg1 = Config(holidays='2022-05-01;easter;whitsun') - cfg1.save() - self.assertEqual( - cfg1.holiday_dates([2022]), - [date(2022, 5, 1), date(2022, 4, 17), date(2022, 6, 5)]) + with Transaction().set_context({'holiday_years': [2022]}): + cfg1 = Config(holidays='2022-05-01;easter;whitsun') + cfg1.save() + self.assertEqual( + cfg1.holiday_dates([2022]), + [date(2022, 5, 1), date(2022, 4, 17), date(2022, 6, 5)]) + self.assertEqual( + cfg1.holidays_info, + '04/17/2022|05/01/2022|06/05/2022') @with_transaction() def test_planner_create_job(self): diff --git a/view/configuration_form.xml b/view/configuration_form.xml index cb87bf7..fca0b6d 100644 --- a/view/configuration_form.xml +++ b/view/configuration_form.xml @@ -8,7 +8,7 @@ full copyright notices and license terms. -->