config: new field 'holidays_info' to show generated holidays in config-form

This commit is contained in:
Frederik Jaeckel 2024-06-02 10:27:15 +02:00
parent b54a8d678f
commit e76ec5792c
5 changed files with 55 additions and 6 deletions

View file

@ -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

View file

@ -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 #

View file

@ -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"

View file

@ -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):

View file

@ -8,7 +8,7 @@ full copyright notices and license terms. -->
<separator id="planner" colspan="4" string="Scheduled Bookings"/>
<label name="holidays"/>
<field name="holidays" colspan="3"/>
<newline/>
<field name="holidays_info" colspan="4"/>
</xpath>