75d3bd6 by Anon Ray at 2014-02-19 1
# -*-  coding: utf-8 -*-
2
"""
3
    __init__.py
4
"""
5
6
import os
8fe0bbc by Anon Ray at 2014-05-10 7
import logging
ce66e4e by Anon Ray at 2014-05-11 8
from logging.handlers import RotatingFileHandler
75d3bd6 by Anon Ray at 2014-02-19 9
cffc26d by Anon Ray at 2014-06-10 10
from flask import Flask, request, jsonify, render_template, make_response
11
75d3bd6 by Anon Ray at 2014-02-19 12
from classes.database import db
13
from config import DefaultConfig
14
from classes import views
15
#from classes import models
f867edb by Anon Ray at 2014-04-10 16
from classes import oauth
75d3bd6 by Anon Ray at 2014-02-19 17
18
__all__ = ['create_app', 'getDBInstance']
19
20
DEFAULT_APP_NAME = __name__
21
22
DEFAULT_MODULES = (
23
    (views.frontend, ''),
d120774 by Anon Ray at 2014-03-08 24
    (views.api, '/api'),
25
    (views.user, '/users'),
26
    (views.context, '/contexts'),
8fe0bbc by Anon Ray at 2014-05-10 27
    (views.sweet, '/sweets'),
f867edb by Anon Ray at 2014-04-10 28
    (views.app, '/apps'),
29
    (views.Oauth, '/oauth')
75d3bd6 by Anon Ray at 2014-02-19 30
)
31
32
33
def create_app(config=None, app_name=None, modules=None):
34
35
    if app_name is None:
36
        app_name = DEFAULT_APP_NAME
37
38
    if modules is None:
39
        modules = DEFAULT_MODULES
40
41
    app = Flask(app_name)
42
43
    configure_app(app, config)
44
45
    configure_logging(app)
46
    configure_errorhandlers(app)
47
    configure_extensions(app)
48
    #configure_beforehandlers(app)
49
    configure_modules(app, modules)
50
51
    return app
52
53
54
def configure_app(app, config):
55
56
    app.config.from_object(DefaultConfig())
57
58
    if config is not None:
59
        app.config.from_object(config)
60
61
    app.config.from_envvar('APP_CONFIG', silent=True)
62
63
64
def configure_modules(app, modules):
65
    for module, url_prefix in modules:
66
        app.register_module(module, url_prefix=url_prefix)
67
68
69
def configure_extensions(app):
70
    db.init_app(app)
71
    db.app = app
f867edb by Anon Ray at 2014-04-10 72
    oauth.init_app(app)
75d3bd6 by Anon Ray at 2014-02-19 73
cffc26d by Anon Ray at 2014-06-10 74
75d3bd6 by Anon Ray at 2014-02-19 75
# return the current db instance
76
# TODO: is this needed so much?
77
def getDBInstance():
78
    return db
79
80
81
def configure_errorhandlers(app):
82
83
    if app.testing:
84
        return
85
86
    # TODO: with all these request can we send back the respective HTTP status
87
    # codes instead of 200?
88
    @app.errorhandler(404)
89
    def not_found(error):
90
        response = make_response()
91
        response.status_code = 404
92
93
        if request.is_xhr:
94
            response.data = jsonify(error=error)
95
        else:
96
            response.data = render_template('errors/404.html')
97
98
        return response
99
100
    @app.errorhandler(403)
101
    def forbidden(error):
102
        response = make_response()
103
        response.status_code = 403
104
105
        if request.is_xhr:
106
            response.data = jsonify(error=error)
107
        else:
108
            response.data = render_template('errors/403.html')
109
110
        return response
111
112
    @app.errorhandler(401)
113
    def unauthorized(error):
114
        response = make_response()
115
        response.status_code = 401
116
117
        if request.is_xhr:
118
            response.data = jsonify(error=error)
119
        else:
120
            response.data = render_template('errors/401.html')
121
122
        return response
123
124
    @app.errorhandler(400)
125
    def bad_request(error):
126
        response = make_response()
127
        response.status_code = 400
128
129
        # Check if we have any custom error messages
d120774 by Anon Ray at 2014-03-08 130
        #if g.error:
131
        #    print 'g.error:'
132
        #    print g.error
133
        #    error = g.error
75d3bd6 by Anon Ray at 2014-02-19 134
135
        if request.is_xhr:
136
            response.data = jsonify(error=error)
137
        else:
138
            response.data = render_template('errors/400.html', error=error)
139
140
        return response
141
142
    @app.errorhandler(500)
143
    def server_error(error):
144
        response = make_response()
145
        response.status_code = 500
146
147
        if request.is_xhr:
148
            response.data = jsonify(error=error)
149
        else:
150
            response.data = render_template('errors/500.html')
151
152
        return response
153
154
155
def configure_logging(app):
8fe0bbc by Anon Ray at 2014-05-10 156
157
    formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s '
158
                                  '[in %(pathname)s:%(lineno)d]')
159
160
    # Also error can be sent out via email. So we can also have a SMTPHandler?
d574b02 by Anon Ray at 2014-06-09 161
    log_file = os.path.join(os.path.dirname(__file__), '..',
162
                            app.config['LOG_FILE'])
163
164
    max_size = 1024 * 1024 * 20  # Max Size for a log file: 20MB
ce66e4e by Anon Ray at 2014-05-11 165
    log_handler = RotatingFileHandler(log_file, maxBytes=max_size,
166
                                      backupCount=10)
8fe0bbc by Anon Ray at 2014-05-10 167
cffc26d by Anon Ray at 2014-06-10 168
    if 'LOG_LEVEL' in app.config:
8fe0bbc by Anon Ray at 2014-05-10 169
        log_level = app.config['LOG_LEVEL'] or 'ERROR'
170
    else:
171
        log_level = 'ERROR'
172
173
    log_handler.setLevel(log_level)
174
    log_handler.setFormatter(formatter)
175
176
    app.logger.addHandler(log_handler)