-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
77 lines (52 loc) · 1.72 KB
/
manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from flask import Flask, g, session, jsonify, flash, redirect, url_for
from config import config, AppConfig
from flask_script import Manager, Server, Shell
from flask_migrate import Migrate, MigrateCommand
import os
from api.models import User
from flask_github import GitHubError
from api.utils.db import db
from application import create_app
app = create_app(os.getenv('FLASK_ENV', 'development'))
migrate = Migrate(app, db)
manager = Manager(app)
@app.errorhandler(GitHubError)
def jumpship(error):
session.pop('user_id', None)
flash('Something bad happened. Please try again?', 'error')
return redirect(url_for('user_app.index'))
@app.before_request
def before_request():
g.user = None
if 'user_id' in session:
user_id = session['user_id']
get_user = lambda user_id: User.query.filter_by(username=user_id).first() or\
User.query.filter_by(github_login=user_id).first()
g.user = get_user(user_id) if user_id else None
@app.after_request
def after_request(response):
db.session.remove()
return response
def _make_context():
return dict(app=app, db=db)
@manager.command
def test():
pytest.main(["-s", "tests/"])
@manager.command
def test_coverage():
pytest.main(["--cov=.", "tests/"])
@manager.command
def test_cov_report():
pytest.main(["--cov-report", "html:cov_html",
"--cov=.", "tests/"])
# Turn on reloader
manager.add_command('runserver', Server(
use_reloader=True,
host=os.getenv('IP', '0.0.0.0'),
port=int(os.getenv('PORT', 5000))
))
# Migrations
manager.add_command('db', MigrateCommand)
manager.add_command("shell", Shell(make_context=_make_context))
if __name__ == '__main__':
manager.run()