You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.6 KiB
Python
95 lines
2.6 KiB
Python
![]()
7 years ago
|
import flask
|
||
|
import flask_bootstrap
|
||
|
import redis
|
||
![]()
6 years ago
|
import json
|
||
![]()
7 years ago
|
import os
|
||
|
import jinja2
|
||
![]()
6 years ago
|
import uuid
|
||
![]()
6 years ago
|
import string
|
||
|
import random
|
||
![]()
6 years ago
|
|
||
![]()
7 years ago
|
|
||
|
app = flask.Flask(__name__)
|
||
|
flask_bootstrap.Bootstrap(app)
|
||
![]()
6 years ago
|
db = redis.StrictRedis(host='redis', port=6379, db=0)
|
||
|
|
||
|
|
||
![]()
6 years ago
|
def render_flavor(flavor, template, data):
|
||
![]()
6 years ago
|
return flask.render_template(
|
||
|
os.path.join(flavor, template),
|
||
![]()
6 years ago
|
**data
|
||
|
)
|
||
|
|
||
|
|
||
![]()
6 years ago
|
@app.add_template_global
|
||
![]()
6 years ago
|
def secret(length=16):
|
||
|
charset = string.ascii_uppercase + string.digits
|
||
|
return ''.join(
|
||
|
random.SystemRandom().choice(charset)
|
||
|
for _ in range(length)
|
||
![]()
6 years ago
|
)
|
||
![]()
7 years ago
|
|
||
|
|
||
![]()
6 years ago
|
def build_app(path):
|
||
|
|
||
|
versions = [
|
||
|
version for version in os.listdir(path)
|
||
|
if os.path.isdir(os.path.join(path, version))
|
||
|
]
|
||
![]()
7 years ago
|
|
||
![]()
6 years ago
|
app.jinja_env.trim_blocks = True
|
||
|
app.jinja_env.lstrip_blocks = True
|
||
|
|
||
![]()
6 years ago
|
@app.context_processor
|
||
|
def app_context():
|
||
|
return dict(versions=versions)
|
||
![]()
6 years ago
|
|
||
![]()
6 years ago
|
@app.route("/")
|
||
|
def index():
|
||
|
return flask.redirect(flask.url_for('{}.wizard'.format(versions[-1])))
|
||
![]()
7 years ago
|
|
||
![]()
6 years ago
|
for version in versions:
|
||
![]()
7 years ago
|
bp = flask.Blueprint(version, __name__)
|
||
|
bp.jinja_loader = jinja2.ChoiceLoader([
|
||
![]()
6 years ago
|
jinja2.FileSystemLoader(os.path.join(path, version, "templates")),
|
||
|
jinja2.FileSystemLoader(os.path.join(path, version, "flavors"))
|
||
![]()
7 years ago
|
])
|
||
![]()
7 years ago
|
|
||
![]()
6 years ago
|
@bp.context_processor
|
||
|
def bp_context(version=version):
|
||
|
return dict(version=version)
|
||
|
|
||
![]()
7 years ago
|
@bp.route("/")
|
||
|
def wizard():
|
||
|
return flask.render_template('wizard.html')
|
||
![]()
7 years ago
|
|
||
![]()
6 years ago
|
@bp.route("/submit", methods=["POST"])
|
||
|
def submit():
|
||
![]()
6 years ago
|
data = flask.request.form.copy()
|
||
![]()
6 years ago
|
data['uid'] = str(uuid.uuid4())
|
||
|
db.set(data['uid'], json.dumps(data))
|
||
|
return flask.redirect(flask.url_for('.setup', uid=data['uid']))
|
||
![]()
7 years ago
|
|
||
![]()
6 years ago
|
@bp.route("/setup/<uid>", methods=["GET"])
|
||
|
def setup(uid):
|
||
|
data = json.loads(db.get(uid))
|
||
|
flavor = data.get("flavor", "compose")
|
||
![]()
6 years ago
|
rendered = render_flavor(flavor, "setup.html", data)
|
||
![]()
7 years ago
|
return flask.render_template("setup.html", contents=rendered)
|
||
|
|
||
![]()
6 years ago
|
@bp.route("/file/<uid>/<filepath>", methods=["GET"])
|
||
|
def file(uid, filepath):
|
||
|
data = json.loads(db.get(uid))
|
||
|
flavor = data.get("flavor", "compose")
|
||
|
return flask.Response(
|
||
![]()
6 years ago
|
render_flavor(flavor, filepath, data),
|
||
![]()
6 years ago
|
mimetype="application/text"
|
||
|
)
|
||
|
|
||
![]()
7 years ago
|
app.register_blueprint(bp, url_prefix="/{}".format(version))
|
||
|
|
||
![]()
7 years ago
|
|
||
|
if __name__ == "__main__":
|
||
![]()
7 years ago
|
build_app("/tmp/mailutest")
|
||
![]()
7 years ago
|
app.run(debug=True)
|