from flask import Flask, url_for, flash, request, redirect, Response from flask import render_template import os import inspect from mg.error import * from mg import application as m from mg import pool import mg.logger import mg.tasks import queue # TODO: need universal ajax wrapper for errors. app = Flask(__name__) app.debug = True app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT' app.mg = m @app.context_processor def inject_stores(): return dict(global_stores = m.magedir.list(instances=True), tasks_queue=queue.tasks_queue_items) from modules import stores, store, docs, install_store, dists app.register_blueprint(stores.blueprint, url_prefix='/stores') app.register_blueprint(docs.blueprint, url_prefix='/readme') app.register_blueprint(store.blueprint, url_prefix='/store') app.register_blueprint(install_store.blueprint, url_prefix='/install_store') app.register_blueprint(dists.blueprint, url_prefix='/dists') try: from api import api api = api(app, prefix='/api/v1') except ImportError as e: mg.logger.warning("Can't initialize RESTFul api: %s" % e) @app.route('/') def index(): return redirect(url_for('stores.index')) @app.route('/queue') def tasks_queue(): return render_template('queue.html', queue=queue.tasks_queue_items) def flash_errors(form): for field, errors in form.errors.items(): for error in errors: flash(u"%s: %s" % ( getattr(form, field).label.text, error ))