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