From d63d68fb490e62f8879e8af23edf5afd0e1fda61 Mon Sep 17 00:00:00 2001 From: Ferry Boender Date: Mon, 23 Mar 2015 17:17:11 +0100 Subject: [PATCH] Native example which show how to use Python functions as callbacks. --- examples/native/README.md | 5 ++++ examples/native/native.json | 48 +++++++++++++++++++++++++++++++++++++ examples/native/native.py | 29 ++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 examples/native/README.md create mode 100644 examples/native/native.json create mode 100755 examples/native/native.py diff --git a/examples/native/README.md b/examples/native/README.md new file mode 100644 index 0000000..e0a3a74 --- /dev/null +++ b/examples/native/README.md @@ -0,0 +1,5 @@ +ScriptForm native example +========================= + +This example shows how to create two simple forms with python functions as +backends. diff --git a/examples/native/native.json b/examples/native/native.json new file mode 100644 index 0000000..640af06 --- /dev/null +++ b/examples/native/native.json @@ -0,0 +1,48 @@ +{ + "title": "Test server", + "forms": { + "import": { + "title": "Import data", + "description": "Import CSV data into a database", + "submit_title": "Import", + "fields": [ + { + "name": "target_db", + "title": "Database to import to", + "type": "select", + "options": [ + ["devtest", "Dev Test db"], + ["prodtest", "Prod Test db"] + ] + }, + { + "name": "csv_file", + "title": "CSV file", + "type": "file" + } + ] + }, + "add_user": { + "title": "Add user", + "description": "Add a user to the htaccess file or change their password", + "submit_title": "Add user", + "fields": [ + { + "name": "username", + "title": "Username", + "type": "string" + }, + { + "name": "password1", + "title": "Password", + "type": "password" + }, + { + "name": "password2", + "title": "Password (Repear)", + "type": "password" + } + ] + } + } +} diff --git a/examples/native/native.py b/examples/native/native.py new file mode 100755 index 0000000..3416e8f --- /dev/null +++ b/examples/native/native.py @@ -0,0 +1,29 @@ +#!/usr/bin/python + +import scriptform + +def job_import(values): + return "Importing into database '{}'".format(values['target_db']) + +def job_add_user(values): + username = values['username'] + password1 = values['password1'] + password2 = values['password2'] + + if not password1: + return "Empty password specified." + + if password1 != password2: + return "Passwords do not match." + + # We do some stuff here. + + return "User created" + +if __name__ == "__main__": + callbacks = { + 'import': job_import, + 'add_user': job_add_user + } + sf = scriptform.ScriptForm('native.json', callbacks) + sf.run(listen_port=8080)