From 2ea31d98ffd5a29f58acf44e254f40ee39cb38c0 Mon Sep 17 00:00:00 2001 From: Ferry Boender Date: Sun, 30 Apr 2017 09:30:47 +0200 Subject: [PATCH] Implemented "default_value" form option. --- doc/MANUAL.md | 8 ++++++- examples/megacorp_acc/megacorp_acc.json | 30 +++++++++---------------- src/formdefinition.py | 5 +++-- src/scriptform.py | 1 + src/webapp.py | 3 ++- 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/doc/MANUAL.md b/doc/MANUAL.md index c31eeb5..2201c5d 100644 --- a/doc/MANUAL.md +++ b/doc/MANUAL.md @@ -756,6 +756,7 @@ The `string` field type supports the following additional options: - **`minlen`**: The minimum allowed length for the field. - **`maxlen`**: The maximum allowed length for the field. - **`size`**: The size (in characters) of the input field. +- **`default_value`**: The default value. For example: @@ -782,6 +783,7 @@ The `integer` field type supports the following additional options: - **`min`**: The minimum allowed value for the field. - **`max`**: The maximum allowed value for the field. +- **`default_value`**: The default value. For example: @@ -806,6 +808,7 @@ The `float` field type supports the following additional options: - **`min`**: The minimum allowed value for the field. - **`max`**: The maximum allowed value for the field. +- **`default_value`**: The default value. For example: @@ -839,6 +842,7 @@ 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. For example: @@ -849,7 +853,7 @@ For example: "title": "Birthdate", "type": "date", "min": "1900-01-01", - "max": "2015-01-01", + "max": "2015-01-01" } ] ... @@ -945,6 +949,7 @@ The `text` field type supports the following additional options: - **`cols`**: The number of cols to make the input filed. - **`minlen`**: The minimum allowed length for the field. - **`maxlen`**: The maximum allowed length for the field. +- **`default_value`**: The default value. For example: @@ -966,6 +971,7 @@ For example: ### Password - **`minlen`**: The minimum allowed length for the field. +- **`default_value`**: The default value. ### File diff --git a/examples/megacorp_acc/megacorp_acc.json b/examples/megacorp_acc/megacorp_acc.json index 78b3900..e43ab27 100644 --- a/examples/megacorp_acc/megacorp_acc.json +++ b/examples/megacorp_acc/megacorp_acc.json @@ -22,6 +22,14 @@ "title": "Last name", "type": "string" }, + { + "name": "birthdate", + "title": "Birthdate", + "type": "date", + "min": "1900-01-01", + "max": "2015-01-01", + "default_value": "1980-02-05" + }, { "name": "email_address", "title": "What's your email address?", @@ -37,24 +45,6 @@ "description": "Please check which topics you are interested in", "script": "job_signup_step2.sh", "fields": [ - { - "name": "first_name", - "title": "First name", - "type": "string", - "hidden": true - }, - { - "name": "last_name", - "title": "Last name", - "type": "string", - "hidden": true - }, - { - "name": "email_address", - "title": "What's your email address?", - "type": "string", - "hidden": true - }, { "name": "check_company", "title": "Negacorp Company news", @@ -154,6 +144,7 @@ "title": "From IP Address", "type": "string", "required": true, + "default_value": "192.168.4.", "min_length": 7, "size": 15 }, @@ -162,7 +153,8 @@ "title": "Expire (days)", "type": "integer", "max": 31, - "min": 2 + "min": 2, + "default_value": 7 }, { "name": "network", diff --git a/src/formdefinition.py b/src/formdefinition.py index 75f056e..4c1cf65 100644 --- a/src/formdefinition.py +++ b/src/formdefinition.py @@ -18,13 +18,14 @@ class FormDefinition(object): for validation of the form values. """ def __init__(self, name, title, description, fields, script, - output='escaped', hidden=False, submit_title="Submit", - allowed_users=None, run_as=None): + default_value=None, output='escaped', hidden=False, + submit_title="Submit", allowed_users=None, run_as=None): self.name = name self.title = title self.description = description self.fields = fields self.script = script + self.default_value = default_value self.output = output self.hidden = hidden self.submit_title = submit_title diff --git a/src/scriptform.py b/src/scriptform.py index 62e118a..39ad486 100755 --- a/src/scriptform.py +++ b/src/scriptform.py @@ -111,6 +111,7 @@ class ScriptForm(object): form['description'], form['fields'], script, + default_value=form.get('default_value', ""), output=form.get('output', 'escaped'), hidden=form.get('hidden', False), submit_title=form.get('submit_title', 'Submit'), diff --git a/src/webapp.py b/src/webapp.py index b5a0a4f..271d10e 100644 --- a/src/webapp.py +++ b/src/webapp.py @@ -280,7 +280,8 @@ class ScriptFormWebApp(RequestHandler): # Get field-specific parameters if field['type'] not in ('file', 'checkbox'): - params['value'] = form_values.get(field['name'], '') + default_value = field.get('default_value', '') + params['value'] = form_values.get(field['name'], default_value) if field['type'] not in ('radio', 'checkbox', 'select'): params['required'] = field.get('required', False)