Native example which show how to use Python functions as callbacks.

pull/7/head
Ferry Boender 10 years ago
parent 275e53a2c5
commit d63d68fb49
  1. 5
      examples/native/README.md
  2. 48
      examples/native/native.json
  3. 29
      examples/native/native.py

@ -0,0 +1,5 @@
ScriptForm native example
=========================
This example shows how to create two simple forms with python functions as
backends.

@ -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"
}
]
}
}
}

@ -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)
Loading…
Cancel
Save