Created
April 5, 2018 13:58
-
-
Save gowhari/07d55199e7f90b0b38a03091345842a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import config | |
import logging | |
import database as db | |
from functools import wraps | |
from flask import Flask, request, session, redirect, g | |
app = Flask(__name__, | |
static_url_path=config.flask.static_url_path, | |
static_folder=config.flask.static_folder) | |
app.secret_key = config.flask.secret_key | |
class ErrorToClient(Exception): | |
pass | |
@app.errorhandler(ErrorToClient) | |
def error_to_client(error): | |
return json.dumps({'message': error.args[0], 'args': error.args[1:]}), 400 | |
@app.before_request | |
def before_request(): | |
g.db_session = db.Session() | |
g.user_id = session.get('user_id', None) | |
@app.teardown_request | |
def teardown_request(exception): | |
db_session = getattr(g, 'db_session', None) | |
if db_session is not None: | |
db_session.close() | |
def return_json(f): | |
@wraps(f) | |
def inner(*a, **k): | |
r = f(*a, **k) | |
return json.dumps(r) | |
return inner |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment