75d3bd6 by Anon Ray at 2014-02-19 |
1 |
# -*- coding: utf-8 -*- |
|
2 |
""" |
|
3 |
__init__.py |
|
4 |
""" |
|
5 |
|
|
6 |
from flask import Flask, request, jsonify, render_template, make_response, g |
|
7 |
import os |
|
8 |
|
|
9 |
from classes.database import db |
|
10 |
from config import DefaultConfig |
|
11 |
from classes import views |
|
12 |
|
|
13 |
#from classes import models |
|
14 |
|
|
15 |
__all__ = ['create_app', 'getDBInstance'] |
|
16 |
|
|
17 |
DEFAULT_APP_NAME = __name__ |
|
18 |
|
|
19 |
DEFAULT_MODULES = ( |
|
20 |
(views.frontend, ''), |
|
21 |
(views.api, '/api') |
|
22 |
) |
|
23 |
|
|
24 |
|
|
25 |
def create_app(config=None, app_name=None, modules=None): |
|
26 |
|
|
27 |
if app_name is None: |
|
28 |
app_name = DEFAULT_APP_NAME |
|
29 |
|
|
30 |
if modules is None: |
|
31 |
modules = DEFAULT_MODULES |
|
32 |
|
|
33 |
app = Flask(app_name) |
|
34 |
|
|
35 |
configure_app(app, config) |
|
36 |
|
|
37 |
configure_logging(app) |
|
38 |
configure_errorhandlers(app) |
|
39 |
configure_extensions(app) |
|
40 |
#configure_beforehandlers(app) |
|
41 |
configure_modules(app, modules) |
|
42 |
|
|
43 |
return app |
|
44 |
|
|
45 |
|
|
46 |
def configure_app(app, config): |
|
47 |
|
|
48 |
app.config.from_object(DefaultConfig()) |
|
49 |
|
|
50 |
if config is not None: |
|
51 |
app.config.from_object(config) |
|
52 |
|
|
53 |
app.config.from_envvar('APP_CONFIG', silent=True) |
|
54 |
|
|
55 |
|
|
56 |
def configure_modules(app, modules): |
|
57 |
for module, url_prefix in modules: |
|
58 |
app.register_module(module, url_prefix=url_prefix) |
|
59 |
|
|
60 |
|
|
61 |
def configure_extensions(app): |
|
62 |
|
|
63 |
db.init_app(app) |
|
64 |
db.app = app |
|
65 |
|
|
66 |
# return the current db instance |
|
67 |
# TODO: is this needed so much? |
|
68 |
def getDBInstance(): |
|
69 |
return db |
|
70 |
|
|
71 |
|
|
72 |
def configure_errorhandlers(app): |
|
73 |
|
|
74 |
if app.testing: |
|
75 |
return |
|
76 |
|
|
77 |
# TODO: with all these request can we send back the respective HTTP status |
|
78 |
# codes instead of 200? |
|
79 |
@app.errorhandler(404) |
|
80 |
def not_found(error): |
|
81 |
response = make_response() |
|
82 |
response.status_code = 404 |
|
83 |
|
|
84 |
if request.is_xhr: |
|
85 |
response.data = jsonify(error=error) |
|
86 |
else: |
|
87 |
response.data = render_template('errors/404.html') |
|
88 |
|
|
89 |
return response |
|
90 |
|
|
91 |
@app.errorhandler(403) |
|
92 |
def forbidden(error): |
|
93 |
response = make_response() |
|
94 |
response.status_code = 403 |
|
95 |
|
|
96 |
if request.is_xhr: |
|
97 |
response.data = jsonify(error=error) |
|
98 |
else: |
|
99 |
response.data = render_template('errors/403.html') |
|
100 |
|
|
101 |
return response |
|
102 |
|
|
103 |
@app.errorhandler(401) |
|
104 |
def unauthorized(error): |
|
105 |
response = make_response() |
|
106 |
response.status_code = 401 |
|
107 |
|
|
108 |
if request.is_xhr: |
|
109 |
response.data = jsonify(error=error) |
|
110 |
else: |
|
111 |
response.data = render_template('errors/401.html') |
|
112 |
|
|
113 |
return response |
|
114 |
|
|
115 |
@app.errorhandler(400) |
|
116 |
def bad_request(error): |
|
117 |
response = make_response() |
|
118 |
response.status_code = 400 |
|
119 |
|
|
120 |
# Check if we have any custom error messages |
|
121 |
print 'g.error:' |
|
122 |
print g.error |
|
123 |
if g.error: |
|
124 |
error = g.error |
|
125 |
|
|
126 |
if request.is_xhr: |
|
127 |
response.data = jsonify(error=error) |
|
128 |
else: |
|
129 |
response.data = render_template('errors/400.html', error=error) |
|
130 |
|
|
131 |
return response |
|
132 |
|
|
133 |
@app.errorhandler(500) |
|
134 |
def server_error(error): |
|
135 |
response = make_response() |
|
136 |
response.status_code = 500 |
|
137 |
|
|
138 |
if request.is_xhr: |
|
139 |
response.data = jsonify(error=error) |
|
140 |
else: |
|
141 |
response.data = render_template('errors/500.html') |
|
142 |
|
|
143 |
return response |
|
144 |
|
|
145 |
|
|
146 |
def configure_logging(app): |
|
147 |
#TODO: implement |
|
148 |
pass |
|
149 |
|