Refactor routing in app.py.
[mediagoblin.git] / mediagoblin / routing.py
CommitLineData
8e1e744d 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
e5572c60
ML
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
48cf435d
E
17import logging
18
7742dcc1 19from werkzeug.routing import Map, Rule
48cf435d
E
20from mediagoblin.tools.common import import_component
21from mediagoblin.tools.pluginapi import PluginManager
22
23
24_log = logging.getLogger(__name__)
31a8ff42 25
7742dcc1 26url_map = Map()
71454fd3 27
d56e8263 28view_functions = {}
24181820 29
48cf435d
E
30
31def endpoint_to_controller(endpoint):
32 view_func = view_functions[endpoint]
33
34 _log.debug('endpoint: {0} view_func: {1}'.format(endpoint, view_func))
35
36 # import the endpoint, or if it's already a callable, call that
37 if isinstance(view_func, basestring):
38 view_func = import_component(view_func)
39 view_functions[endpoint] = view_func
40
41 return view_func
42
43
7742dcc1 44def add_route(endpoint, url, controller):
0d857844
CAW
45 """
46 Add a route to the url mapping
47 """
5b60ec41
JW
48 # XXX: We cannot use this, since running tests means that the plugin
49 # routes will be populated over and over over the same session.
50 #
51 # assert endpoint not in view_functions.keys(), 'Trying to overwrite a rule'
d56e8263 52
7742dcc1 53 view_functions.update({endpoint: controller})
31a8ff42 54
7742dcc1 55 url_map.add(Rule(url, endpoint=endpoint))
4bd65f69 56
48cf435d 57
0d857844
CAW
58def mount(mountpoint, routes):
59 """
60 Mount a bunch of routes to this mountpoint
61 """
62 for endpoint, url, controller in routes:
63 url = "%s/%s" % (mountpoint.rstrip('/'), url.lstrip('/'))
64 add_route(endpoint, url, controller)
65
48cf435d
E
66
67def get_url_map():
68 for route in PluginManager().get_routes():
69 add_route(*route)
70
71 return url_map
72
73
7742dcc1 74add_route('index', '/', 'mediagoblin.views:root_view')
0f63a944 75
5b60ec41
JW
76from mediagoblin.admin.routing import admin_routes
77from mediagoblin.auth.routing import auth_routes
78mount('/auth', auth_routes)
79mount('/a', admin_routes)
80
7742dcc1
JW
81import mediagoblin.submit.routing
82import mediagoblin.user_pages.routing
7742dcc1
JW
83import mediagoblin.edit.routing
84import mediagoblin.webfinger.routing
85import mediagoblin.listings.routing