Merge remote-tracking branch 'refs/remotes/spaetz/trac_475_email_notification_checkbox'
[mediagoblin.git] / mediagoblin / app.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import os
18 import logging
19
20 from mediagoblin.routing import url_map, view_functions, add_route
21
22 from werkzeug.wrappers import Request
23 from werkzeug.exceptions import HTTPException, NotFound
24
25 from mediagoblin import meddleware, __version__
26 from mediagoblin.tools import common, translate, template
27 from mediagoblin.tools.response import render_404
28 from mediagoblin.tools.theme import register_themes
29 from mediagoblin.tools import request as mg_request
30 from mediagoblin.mg_globals import setup_globals
31 from mediagoblin.init.celery import setup_celery_from_config
32 from mediagoblin.init.plugins import setup_plugins
33 from mediagoblin.init import (get_jinja_loader, get_staticdirector,
34 setup_global_and_app_config, setup_workbench, setup_database,
35 setup_storage, setup_beaker_cache)
36 from mediagoblin.tools.pluginapi import PluginManager
37
38
39 _log = logging.getLogger(__name__)
40
41
42 class MediaGoblinApp(object):
43 """
44 WSGI application of MediaGoblin
45
46 ... this is the heart of the program!
47 """
48 def __init__(self, config_path, setup_celery=True):
49 """
50 Initialize the application based on a configuration file.
51
52 Arguments:
53 - config_path: path to the configuration file we're opening.
54 - setup_celery: whether or not to setup celery during init.
55 (Note: setting 'celery_setup_elsewhere' also disables
56 setting up celery.)
57 """
58 _log.info("GNU MediaGoblin %s main server starting", __version__)
59 _log.debug("Using config file %s", config_path)
60 ##############
61 # Setup config
62 ##############
63
64 # Open and setup the config
65 global_config, app_config = setup_global_and_app_config(config_path)
66
67 ##########################################
68 # Setup other connections / useful objects
69 ##########################################
70
71 # Set up plugins -- need to do this early so that plugins can
72 # affect startup.
73 _log.info("Setting up plugins.")
74 setup_plugins()
75
76 # Set up the database
77 self.connection, self.db = setup_database()
78
79 # Register themes
80 self.theme_registry, self.current_theme = register_themes(app_config)
81
82 # Get the template environment
83 self.template_loader = get_jinja_loader(
84 app_config.get('local_templates'),
85 self.current_theme,
86 PluginManager().get_template_paths()
87 )
88
89 # Set up storage systems
90 self.public_store, self.queue_store = setup_storage()
91
92 # set up routing
93 self.url_map = url_map
94
95 for route in PluginManager().get_routes():
96 _log.debug('adding plugin route: {0}'.format(route))
97 add_route(*route)
98
99 # set up staticdirector tool
100 self.staticdirector = get_staticdirector(app_config)
101
102 # set up caching
103 self.cache = setup_beaker_cache()
104
105 # Setup celery, if appropriate
106 if setup_celery and not app_config.get('celery_setup_elsewhere'):
107 if os.environ.get('CELERY_ALWAYS_EAGER', 'false').lower() == 'true':
108 setup_celery_from_config(
109 app_config, global_config,
110 force_celery_always_eager=True)
111 else:
112 setup_celery_from_config(app_config, global_config)
113
114 #######################################################
115 # Insert appropriate things into mediagoblin.mg_globals
116 #
117 # certain properties need to be accessed globally eg from
118 # validators, etc, which might not access to the request
119 # object.
120 #######################################################
121
122 setup_globals(app=self)
123
124 # Workbench *currently* only used by celery, so this only
125 # matters in always eager mode :)
126 setup_workbench()
127
128 # instantiate application meddleware
129 self.meddleware = [common.import_component(m)(self)
130 for m in meddleware.ENABLED_MEDDLEWARE]
131
132 def call_backend(self, environ, start_response):
133 request = Request(environ)
134
135 ## Compatibility webob -> werkzeug
136 request.GET = request.args
137 request.accept_language = request.accept_languages
138 request.accept = request.accept_mimetypes
139
140 ## Routing / controller loading stuff
141 map_adapter = self.url_map.bind_to_environ(request.environ)
142
143 # By using fcgi, mediagoblin can run under a base path
144 # like /mediagoblin/. request.path_info contains the
145 # path inside mediagoblin. If the something needs the
146 # full path of the current page, that should include
147 # the basepath.
148 # Note: urlgen and routes are fine!
149 request.full_path = environ["SCRIPT_NAME"] + request.path
150 # python-routes uses SCRIPT_NAME. So let's use that too.
151 # The other option would be:
152 # request.full_path = environ["SCRIPT_URL"]
153
154 # Fix up environ for urlgen
155 # See bug: https://bitbucket.org/bbangert/routes/issue/55/cache_hostinfo-breaks-on-https-off
156 if environ.get('HTTPS', '').lower() == 'off':
157 environ.pop('HTTPS')
158
159 ## Attach utilities to the request object
160 # Do we really want to load this via middleware? Maybe?
161 request.session = request.environ['beaker.session']
162 # Attach self as request.app
163 # Also attach a few utilities from request.app for convenience?
164 request.app = self
165
166 request.db = self.db
167 request.staticdirect = self.staticdirector
168
169 request.locale = translate.get_locale_from_request(request)
170 request.template_env = template.get_jinja_env(
171 self.template_loader, request.locale)
172
173 def build_proxy(endpoint, **kw):
174 try:
175 qualified = kw.pop('qualified')
176 except KeyError:
177 qualified = False
178
179 return map_adapter.build(
180 endpoint,
181 values=dict(**kw),
182 force_external=qualified)
183
184 request.urlgen = build_proxy
185
186 mg_request.setup_user_in_request(request)
187
188 try:
189 endpoint, url_values = map_adapter.match()
190 request.matchdict = url_values
191 except NotFound as exc:
192 return render_404(request)(environ, start_response)
193 except HTTPException as exc:
194 # Support legacy webob.exc responses
195 return exc(environ, start_response)
196
197 view_func = view_functions[endpoint]
198
199 _log.debug('endpoint: {0} view_func: {1}'.format(
200 endpoint,
201 view_func))
202
203 # import the endpoint, or if it's already a callable, call that
204 if isinstance(view_func, unicode) \
205 or isinstance(view_func, str):
206 controller = common.import_component(view_func)
207 else:
208 controller = view_func
209
210 # pass the request through our meddleware classes
211 for m in self.meddleware:
212 response = m.process_request(request, controller)
213 if response is not None:
214 return response(environ, start_response)
215
216 request.start_response = start_response
217
218 # get the response from the controller
219 response = controller(request)
220
221 # pass the response through the meddleware
222 for m in self.meddleware[::-1]:
223 m.process_response(request, response)
224
225 return response(environ, start_response)
226
227 def __call__(self, environ, start_response):
228 ## If more errors happen that look like unclean sessions:
229 # self.db.check_session_clean()
230
231 try:
232 return self.call_backend(environ, start_response)
233 finally:
234 # Reset the sql session, so that the next request
235 # gets a fresh session
236 self.db.reset_after_request()
237
238
239 def paste_app_factory(global_config, **app_config):
240 configs = app_config['config'].split()
241 mediagoblin_config = None
242 for config in configs:
243 if os.path.exists(config) and os.access(config, os.R_OK):
244 mediagoblin_config = config
245 break
246
247 if not mediagoblin_config:
248 raise IOError("Usable mediagoblin config not found.")
249
250 mgoblin_app = MediaGoblinApp(mediagoblin_config)
251
252 return mgoblin_app