From 8c640b33a65cddb5d7b8b88e47721c5bb4a407f7 Mon Sep 17 00:00:00 2001 From: Ferry Boender Date: Mon, 22 Jul 2019 09:58:37 +0200 Subject: [PATCH] Allow 'today' for 'date' field values. --- doc/MANUAL.md | 25 +++++++++++++++++++++---- src/formrender.py | 9 +++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/doc/MANUAL.md b/doc/MANUAL.md index 459cbfc..bfc35b6 100644 --- a/doc/MANUAL.md +++ b/doc/MANUAL.md @@ -836,13 +836,16 @@ enter a date. Depending on the browser's support for HTML5 forms, the input field may have a pop-out calendar from which the user can select a date. The date must be entered, and will be passed to the callback, in the form -YYYY-MM-DD. +`YYYY-MM-DD`. The `date` field type supports the following additional options: -- **`min`**: The minimum allowed date (format: a string YYYY-MM-DD) -- **`max`**: The maximum allowed date (format: a string YYYY-MM-DD) -- **`default_value`**: The default value. +- **`min`**: The minimum allowed date (format: a string `YYYY-MM-DD`) or + "`today`" for today. +- **`max`**: The maximum allowed date (format: a string `YYYY-MM-DD`) or + "`today`" for today. +- **`default_value`**: The default value (format: a string `YYYY-MM-DD`) or + "`today`" for today. For example: @@ -858,6 +861,20 @@ For example: ] ... +Must start today or in the future (Default value today): + + ... + "fields": [ + { + "name": "startdate", + "title": "Start date", + "type": "date", + "min": "today", + "default_value": "today" + } + ] + ... + ### Radio The `radio` field type lets the user pick one option from a list of options. diff --git a/src/formrender.py b/src/formrender.py index a38dfad..911e282 100644 --- a/src/formrender.py +++ b/src/formrender.py @@ -4,6 +4,9 @@ FormRender takes care of the rendering of forms to HTML. """ +import datetime + + HTML_FIELD = u'''
  • {title}

    @@ -140,6 +143,12 @@ class FormRender(object): Render a date field to HTML. """ tpl = self.field_tpl['date'] + if value == "today": + value = datetime.datetime.now().strftime("%Y-%m-%d") + if minval == "today": + minval = datetime.datetime.now().strftime("%Y-%m-%d") + if maxval == "today": + maxval = datetime.datetime.now().strftime("%Y-%m-%d") return tpl.format(name=name, value=value, minval=minval, maxval=maxval, required=required, classes=classes, style=style)