From 34c9cac1a712c9baa1a416e3e804c91baea06c04 Mon Sep 17 00:00:00 2001 From: Ferry Boender Date: Fri, 24 Apr 2015 08:04:26 +0200 Subject: [PATCH] Form definitions are now specified in a list instead of a dict. This preserves their order when listing the forms. --- README.md | 6 +++--- doc/MANUAL.md | 28 +++++++++++++++++----------- examples/auth/auth.json | 10 ++++++---- examples/output_types/output.json | 17 ++++++++++------- examples/simple/simple.json | 10 ++++++---- examples/validate/validate.json | 7 ++++--- src/scriptform.py | 5 +++-- 7 files changed, 49 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 3d96094..c20508b 100644 --- a/README.md +++ b/README.md @@ -54,8 +54,8 @@ Form configuration file: `test_server.json` { "title": "Test server", - "forms": { - "add_user": { + "forms": [ + "name": "add_user", "title": "Add user", "description": "Add a user to the htpasswd file", "submit_title": "Add user", @@ -66,7 +66,7 @@ Form configuration file: `test_server.json` {"name": "password2", "title": "Repeat password", "type": "password"} ] } - } + ] } The script `job_add_user.sh`: diff --git a/doc/MANUAL.md b/doc/MANUAL.md index 1c255a6..259ae77 100644 --- a/doc/MANUAL.md +++ b/doc/MANUAL.md @@ -113,12 +113,16 @@ Structurally, they are made up of the following elements: - **`title`**: Text to show at the top of each page. **Required**, **String**. -- **`forms`**: Dictionary where the key is the form id and the value is a - dictionary that is the definition for a single form. **Required**, **Dictionary**. +- **`forms`**: A list of dictionaries of form definitions. **Required**, **List + of dictionaries**. + + - **`name`**: Name for the form. This must be unique. **Required**, + **String**, **Unique**. - **`title`**: Title for the form. **Required**, **String**. - - **`description`**: A description of the form. May include HTML tags. **Required**, **String**. + - **`description`**: A description of the form. May include HTML tags. + **Required**, **String**. - **`submit_title`**: The text on the submit button of the form. The default value is '`Submit`'. **Optional**, **String**. @@ -162,8 +166,8 @@ For example, here's a form config file that contains two forms: { "title": "Test server", - "forms": { - "import": { + "forms": [ + "name": "import", "title": "Import data", "description": "Import SQL into a database", "submit_title": "Import", @@ -185,7 +189,8 @@ For example, here's a form config file that contains two forms: } ] }, - "add_user": { + { + "name": "add_user", "title": "Add user", "description": "Add a user to the htaccess file or change their password", "submit_title": "Add user", @@ -208,7 +213,7 @@ For example, here's a form config file that contains two forms: } ] } - } + ] } Many more examples can be found in the `examples` directory in the source code. @@ -310,7 +315,8 @@ Scripts can have a few different output types. The output type is specified in the **`output`** field of the form definition. For example, the following form definition has a `raw` output type.: - "display_image" { + { + "name": "display_image", "title": "Show an image", "description": "Show an image", "script: "job_display_image.sh", @@ -415,8 +421,8 @@ view the form 'only_some_users'. "test": "2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b", "test2": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8" }, - "forms": { - "only_some_users": { + "forms": [ + "name": "only_some_users", "title": "Only some users", "description": "You should only see this if you're user 'test2'", "submit_title": "Do nothing", @@ -424,7 +430,7 @@ view the form 'only_some_users'. "allowed_users": ["test2"], "fields": [] } - } + ] } ### Passwords diff --git a/examples/auth/auth.json b/examples/auth/auth.json index 82fdeef..64fb752 100644 --- a/examples/auth/auth.json +++ b/examples/auth/auth.json @@ -4,8 +4,9 @@ "test": "2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b", "test2": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8" }, - "forms": { - "do_nothing": { + "forms": [ + { + "name": "do_nothing", "title": "Test form", "description": "You should only see this if you've entered the correct password", "submit_title": "Do nothing", @@ -13,7 +14,8 @@ "fields": [ ] }, - "only_some_users": { + { + "name": "only_some_users", "title": "Only some users", "description": "You should only see this if you're user 'test2'", "submit_title": "Do nothing", @@ -22,5 +24,5 @@ "fields": [ ] } - } + ] } diff --git a/examples/output_types/output.json b/examples/output_types/output.json index 2d8bec7..4918112 100644 --- a/examples/output_types/output.json +++ b/examples/output_types/output.json @@ -1,7 +1,8 @@ { "title": "Output type callback examples", - "forms": { - "show_image": { + "forms": [ + { + "name": "show_image", "title": "Show an image", "description": "Shows you an image. This is a 'raw' output type where the script writes a complete HTTP response to stdout, which is directly streamed to the browser by scriptform.", "submit_title": "Show", @@ -9,7 +10,8 @@ "output": "raw", "fields": {} }, - "large_bin": { + { + "name": "large_bin", "title": "Download large binary file", "description": "Download a large (100mb) binary file. This demonstrated streaming directly to the client of large files.", "submit_title": "Download", @@ -17,7 +19,8 @@ "output": "raw", "fields": {} }, - "some_html": { + { + "name": "some_html", "title": "Show some HTML", "description": "This is the 'html' output type, which allows HTML in the output of scripts. This can be useful to refer to another form after this form is completed, for instance.", "submit_title": "Show HTML", @@ -25,7 +28,8 @@ "output": "html", "fields": {} }, - "escaped": { + { + "name": "escaped", "title": "Escaped contents (default)", "description": "This is the 'escaped' output type. It is the default. The HTML entities in the output are escaped properly, and is wrapped in PRE elements.", "submit_title": "Show HTML", @@ -33,6 +37,5 @@ "output": "escaped", "fields": {} } - - } + ] } diff --git a/examples/simple/simple.json b/examples/simple/simple.json index 3f94b81..25cbfb7 100644 --- a/examples/simple/simple.json +++ b/examples/simple/simple.json @@ -1,7 +1,8 @@ { "title": "Test server", - "forms": { - "import": { + "forms": [ + { + "name": "import", "title": "Import data", "description": "Import SQL into a database", "submit_title": "Import", @@ -23,7 +24,8 @@ } ] }, - "add_user": { + { + "name": "add_user", "title": "Add user", "description": "Add a user to the htaccess file or change their password", "submit_title": "Add user", @@ -46,5 +48,5 @@ } ] } - } + ] } diff --git a/examples/validate/validate.json b/examples/validate/validate.json index a9eb6c0..d4229cb 100644 --- a/examples/validate/validate.json +++ b/examples/validate/validate.json @@ -1,7 +1,8 @@ { "title": "Validation example", - "forms": { - "validate": { + "forms": [ + { + "name": "validate", "title": "Validated form", "description": "This form is heavily validated", "submit_title": "Validate it", @@ -76,5 +77,5 @@ } ] } - } + ] } diff --git a/src/scriptform.py b/src/scriptform.py index a6a1e08..727c36d 100755 --- a/src/scriptform.py +++ b/src/scriptform.py @@ -7,7 +7,7 @@ # - Default values for input fields. # - If there are errors in the form, its values are empties. # - Send responses using self.send_ if possible -# - Complain about no registered callback on startup instead of serving. +# - Maintain order of forms in form configuration. import sys import optparse @@ -190,7 +190,8 @@ class ScriptForm: if 'users' in config: users = config['users'] - for form_name, form in config['forms'].items(): + for form in config['forms']: + form_name = form['name'] if 'script' in form: script = os.path.join(self.basepath, form['script']) else: