Skip to content

Instantly share code, notes, and snippets.

@molszewski
Last active August 29, 2015 13:57
Show Gist options
  • Save molszewski/9365372 to your computer and use it in GitHub Desktop.
Save molszewski/9365372 to your computer and use it in GitHub Desktop.
#!venv/bin/python
# Requirements:
# - virtualenv in venv dir (virtualenv venv)
# - Flask: venv/bin/pip install flask
# - formencode: venv/bin/pip install formencode
# - FormEncode-Jinja2: venv/bin/pip install FormEncode-Jinja2
from flask import render_template_string, flash, redirect, request
from formencode import Invalid, variabledecode, Schema, ForEach, NoDefault
from formencode.validators import Int
from flask import Flask
import formencode_jinja2
##############################################################
# App configuration
app = Flask(__name__)
app.jinja_env.add_extension(formencode_jinja2.formfill)
app.config.update(
DEBUG=True,
SECRET_KEY='so key much secret'
)
##############################################################
# Validation schemas
class YourOptions(Schema):
your_options = ForEach(Int(not_empty=True, strip=True),
if_missing=NoDefault,
not_empty=True,
messages={
"missing": "Error - you didn't select any option"
})
##############################################################
# Template
YOUR_CHOICE_TEMPLATE = '''
<html>
<head>
<title>Select your options</title>
</head>
<body>
<h1>Select your favourite numbers</h1>
<form action="" method="post">
<p>
{%- formfill form_data with errors -%}
{% for option in [100, 200, 300] %}
<input id="your_options_{{ loop.index }}" name="your_options-{{ loop.index }}" value="{{ option }}" type="checkbox"> {{ option }}<br />
{% endfor %}
{%- endformfill -%}
</p>
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>
'''
INDEX_TEMPLATE = '''
<html>
<head>
<title>Multiple choice example</title>
</head>
<body>
{% with messages = get_flashed_messages() %}
{% if messages %}
<p>You selected following:</p>
<ul>
{% for message in messages %}
<li>{{ message }} </li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
<h2>Hello, please go to <a href="/your_options">a multiple choice page</a></h2>
</body>
</html>
'''
##############################################################
# App routes
@app.route('/')
@app.route('/index')
def index():
return render_template_string(INDEX_TEMPLATE)
@app.route('/your_options', methods=['GET'])
def schema_get():
return render_template_string(YOUR_CHOICE_TEMPLATE, form_data={})
@app.route('/your_options', methods=['POST'])
def schema_post():
form_data = request.form
try:
decoded = variabledecode.variable_decode(form_data)
result = YourOptions().to_python(decoded)
for option in result['your_options']:
flash(str(option))
return redirect('/index')
except Invalid, e:
unpacked_errors = e.unpack_errors(variabledecode.variable_encode)
return render_template_string(YOUR_CHOICE_TEMPLATE,
form_data=form_data,
errors=unpacked_errors)
##############################################################
# App start
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment