This repository has been archived by the owner on Apr 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
120 lines (81 loc) · 3.13 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
from flask import Flask, g, session, request, redirect, flash, abort
from flask_mail import Mail
from shotglass2 import shotglass
from shotglass2.takeabeltof.database import Database
from shotglass2.takeabeltof.jinja_filters import register_jinja_filters
from shotglass2.users.admin import Admin
# Create app
# setting static_folder to None allows me to handle loading myself
app = Flask(__name__, instance_relative_config=True,
static_folder=None)
app.config.from_pyfile('site_settings.py', silent=True)
# work around some web servers that mess up root path
from werkzeug.contrib.fixers import CGIRootFix
if app.config['CGI_ROOT_FIX_APPLY'] == True:
fixPath = app.config.get("CGI_ROOT_FIX_PATH","/")
app.wsgi_app = CGIRootFix(app.wsgi_app, app_root=fixPath)
register_jinja_filters(app)
mail = Mail(app)
def init_db(db=None):
# to support old code
initalize_all_tables(db)
def initalize_all_tables(db=None):
"""Place code here as needed to initialze all the tables for this site"""
if not db:
db = get_db()
shotglass.initalize_user_tables(db)
### setup any other tables you need here....
def get_db(filespec=None):
"""Return a connection to the database.
If the db path does not exist, create it and initialize the db"""
if not filespec:
filespec = app.config['DATABASE_PATH']
# This is probobly a good place to change the
# filespec if you want to use a different database
# for the current request.
# test the path, if not found, create it
initialize = shotglass.make_db_path(filespec)
g.db = Database(filespec).connect()
if initialize:
initalize_all_tables(g.db)
return g.db
@app.before_request
def _before():
# Force all connections to be secure
if app.config['REQUIRE_SSL'] and not request.is_secure :
return redirect(request.url.replace("http://", "https://"))
#ensure that nothing is served from the instance directory
if 'instance' in request.url:
abort(404)
#import pdb;pdb.set_trace()
shotglass.get_app_config(app)
shotglass.set_template_dirs(app)
get_db()
# Is the user signed in?
g.user = None
if 'user' in session:
g.user = session['user']
g.admin = Admin(g.db) # This is where user access rules are stored
shotglass.user_setup() # g.admin now holds access rules Users, Prefs and Roles
@app.teardown_request
def _teardown(exception):
if 'db' in g:
g.db.close()
@app.errorhandler(404)
def page_not_found(error):
return shotglass.page_not_found(error)
@app.errorhandler(500)
def server_error(error):
return shotglass.server_error(error)
#Register the static route
app.add_url_rule('/static/<path:filename>','static',shotglass.static)
## Setup the routes for users
shotglass.register_users(app)
# setup www.routes...
shotglass.register_www(app)
if __name__ == '__main__':
with app.app_context():
# create the default database if needed
initalize_all_tables()
#app.run(host='localhost', port=8000)
app.run()