From b342dfd7d37c71392fa2d82e186562d1af236aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Mon, 13 Jan 2014 23:14:18 +0100 Subject: Fix calculation of default time span for constant costs. --- app/views/consts.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/views/consts.py b/app/views/consts.py index 4b5ea68..c8d9d99 100644 --- a/app/views/consts.py +++ b/app/views/consts.py @@ -13,6 +13,14 @@ from functools import partial mod = Blueprint('consts', __name__) assert_authorisation = partial(assert_authorisation, ConstExpense.get) +def one_year(d): + """Returns the date d', such that [d, d'] spans exactly one year. + In effect, this is d + 11 months (NB: not 12!)""" + if d.month == 1: + return d.replace(month = 12) + else: + return d.replace(month = (d.month + 11) % 12, year = d.year + 1) + # # Form # @@ -23,7 +31,7 @@ class ConstForm(F.Form): end = F.DateField(u'Ende', F.req, format='%m.%Y', - default=lambda: today().replace(year = today().year + 1), + default=lambda: one_year(today()), description=u'(einschließlich)') months = F.IntegerField(u'Zahlungsrythmus', F.req, @@ -155,7 +163,7 @@ def add_from(other): # replace some fields to be more meaningful start = max(form.end.data, today()) form.start.data = start - form.end.data = start.replace(year = start.year + 1) + form.end.data = one_year(start) if not other.next: form.prev.data = other return { 'form': form } -- cgit v1.2.3