-
Notifications
You must be signed in to change notification settings - Fork 87
/
app.py
124 lines (94 loc) · 3.44 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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import bson
import datetime
import logging
import os
from os.path import abspath, join, dirname, isfile, exists
from flask import Flask, abort, send_from_directory, json
from flask.ext.cache import Cache
APP_NAME = __name__.split('.')[0]
ROOT_DIR = abspath(join(dirname(__file__)))
log = logging.getLogger(__name__)
cache = Cache()
class UDataApp(Flask):
debug_log_format = '[%(levelname)s][%(name)s:%(lineno)d] %(message)s'
def send_static_file(self, filename):
'''
Override default static handling:
- raises 404 if not debug
- handle static aliases
'''
if not self.debug:
self.logger.error('Static files are only served in debug')
abort(404)
cache_timeout = self.get_send_file_max_age(filename)
# Default behavior
if isfile(join(self.static_folder, filename)):
return send_from_directory(self.static_folder, filename, cache_timeout=cache_timeout)
# Handle aliases
for prefix, directory in self.config.get('STATIC_DIRS', tuple()):
if filename.startswith(prefix):
real_filename = filename[len(prefix):]
if real_filename.startswith('/'):
real_filename = real_filename[1:]
if isfile(join(directory, real_filename)):
return send_from_directory(directory, real_filename, cache_timeout=cache_timeout)
abort(404)
class UDataJsonEncoder(json.JSONEncoder):
"""A C{json.JSONEncoder} subclass to encode documents that have fields of
type C{bson.objectid.ObjectId}, C{datetime.datetime}
"""
def default(self, obj):
if isinstance(obj, bson.objectid.ObjectId):
return str(obj)
elif isinstance(obj, datetime.datetime):
return obj.isoformat()
return super(UDataJsonEncoder, self).default(obj)
def create_app(config='udata.settings.Defaults'):
'''Factory for a minimal application'''
app = UDataApp(APP_NAME)
app.config.from_object(config)
app.config.from_envvar('UDATA_SETTINGS', silent=True)
custom_settings = join(os.getcwd(), 'udata.cfg')
if exists(custom_settings):
app.config.from_pyfile(custom_settings)
app.json_encoder = UDataJsonEncoder
app.debug = app.config['DEBUG'] and not app.config['TESTING']
init_logging(app)
register_extensions(app)
return app
def standalone(app):
'''Factory for an all in one application'''
from udata import admin, api, core, frontend
core.init_app(app)
frontend.init_app(app)
api.init_app(app)
admin.init_app(app)
from udata import ext
ext.init_app(app)
return app
def init_logging(app):
log_level = logging.DEBUG if app.debug else logging.WARNING
app.logger.setLevel(log_level)
loggers = [
logging.getLogger('elasticsearch'),
logging.getLogger('requests')
]
for name in app.config['PLUGINS']:
logging.getLogger('udata_{0}'.format(name)).setLevel(log_level)
for logger in loggers:
logger.setLevel(logging.WARNING)
return app
def register_extensions(app):
from udata import models, routing, tasks, mail, i18n, auth
i18n.init_app(app)
models.init_app(app)
routing.init_app(app)
auth.init_app(app)
cache.init_app(app)
tasks.init_app(app)
mail.init_app(app)
from udata import search
search.init_app(app)
return app