-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
138 lines (113 loc) · 4.46 KB
/
app.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from flask import Flask, render_template, request, session, redirect, url_for, Blueprint
from flask.ext.babel import Babel
from flask.ext.mail import Mail
from flask.ext.bcrypt import *
from flask.ext.admin import Admin, BaseView, expose
from flask.ext.admin.base import MenuLink
from flask.ext.admin.contrib.sqla import ModelView
from flask.ext.admin.contrib.fileadmin import FileAdmin
from flask.ext.assets import Environment, Bundle
import os.path as op
from decorators import async
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.script import Shell, Manager
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin, LoginForm, \
RegisterForm, ForgotPasswordForm, current_user, login_required, url_for_security, roles_required
from flask.ext.security.forms import ChangePasswordForm
# Create app
app = Flask(__name__)
app.config.from_object('config.config')
app.config.from_object('env.env') #overwrites config.config for production server
app.config.from_object('env.email') #overwrites config.config for production server
custom = Blueprint('custom', __name__, static_folder='custom', static_url_path='/custom')
app.register_blueprint(custom)
assets = Environment(app)
js = Bundle('custom/custom.js', filters='jsmin', output='gen/packed.js')
css = Bundle('custom/style.css', filters='cssmin', output='gen/packed.css')
assets.register('js_all', js)
assets.register('css_all', css)
# Setup mail extension
mail = Mail(app)
# Setup babel
babel = Babel(app)
# useful for a separate production servers
def my_app(environ, start_response):
path = environ["PATH_INFO"]
if path == "/":
return app(environ, start_response)
else:
return app(environ, start_response)
@babel.localeselector
def get_locale():
override = request.args.get('lang')
if override:
session['lang'] = override
rv = session.get('lang', 'en')
return rv
@app.context_processor
def inject_userForms():
return dict(login_form=LoginForm(), register_user_form=RegisterForm(), \
forgot_password_form=ForgotPasswordForm(), change_password_form=ChangePasswordForm())
# Create database connection object
def _make_context():
return dict(app=app, mail=mail, db=db, babel=babel, security=security, admin=admin)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
manager.add_command("shell", Shell(make_context=_make_context))
# Define models
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True)
password = db.Column(db.String(255))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
roles = db.relationship('Role', secondary=roles_users,
backref=db.backref('users', lazy='dynamic'))
def __str__(self):
return '<User id=%s email=%s>' % (self.id, self.email)
# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
@async
def send_security_email(msg):
with app.app_context():
mail.send(msg)
@security.send_mail_task
def async_security_email(msg):
send_security_email(msg)
# db.create_all()
# Views
@app.route('/')
def index():
return render_template('index.html')
@app.route('/profile')
@login_required
def profile():
return render_template('profile.html')
admin = Admin(app)
# Admin Views
class MyModelView(ModelView):
def is_accessible(self):
return True # remove
# return current_user.has_role('admin') # uncomment to lock down admin
class MyFileView(FileAdmin):
def is_accessible(self):
return True # remove
# return current_user.has_role('admin') # uncomment to lock down admin
admin.add_view(MyModelView(User, db.session))
admin.add_view(MyModelView(Role, db.session))
path = op.join(op.dirname(__file__), 'static')
admin.add_view(MyFileView(path, '/static/', name='Static Files'))
admin.add_link(MenuLink(name='Back Home', url='/'))
if __name__ == '__main__':
manager.run()