Remove more refs to sqlahelper.
[xonotic/xonstat.git] / xonstat / __init__.py
1 from pyramid_beaker import set_cache_regions_from_settings
2 from pyramid.config import Configurator
3 from pyramid.httpexceptions import HTTPNotFound
4 from pyramid.renderers import JSONP
5 from sqlalchemy import engine_from_config
6 from xonstat.models import initialize_db
7 from xonstat.views import *
8 from xonstat.security import *
9
10
11 def main(global_config, **settings):
12     """
13     This function returns a Pyramid WSGI application.
14     """
15     # setup the database engine
16     engine = engine_from_config(settings, 'sqlalchemy.', pool_size=5)
17
18     # initialize database structures
19     initialize_db(engine)
20
21     # set up beaker cache
22     set_cache_regions_from_settings(settings)
23
24     config = Configurator(settings=settings, root_factory=ACLFactory)
25
26     # mako for templating
27     config.include('pyramid_mako')
28
29     # for json-encoded responses
30     config.add_renderer('jsonp', JSONP(param_name='callback'))
31
32     # for static assets
33     config.add_static_view('static', 'xonstat:static')
34
35     # robots
36     config.add_route("robots", "robots.txt")
37     config.add_view(robots, route_name="robots")
38
39     # for 404s
40     config.add_view(notfound, context=HTTPNotFound, renderer="404.mako")
41
42     # ROOT ROUTE
43     config.add_route("main_index", "/")
44     config.add_view(main_index, route_name="main_index", renderer="main_index.mako")
45
46     config.add_route("summary_stats_json", "/summary")
47     config.add_view(view=summary_stats_json, route_name="summary_stats_json", renderer="json",
48                     accept="application/json")
49
50     config.add_route("summary_stats_json_suffix", "/summary.json")
51     config.add_view(summary_stats_json, route_name="summary_stats_json_suffix", renderer="json")
52
53     # MAIN SUBMISSION ROUTE
54     config.add_route("submit_stats", "stats/submit")
55     config.add_view(submit_stats, route_name="submit_stats", renderer="submit_stats.mako")
56
57     # PLAYER ROUTES
58     config.add_route("player_game_index", "/player/{player_id:\d+}/games")
59     config.add_view(player_game_index, route_name="player_game_index", accept="text/html",
60                     renderer="player_game_index.mako")
61     config.add_view(player_game_index_json, route_name="player_game_index",
62                     accept="application/json", renderer="jsonp")
63
64     config.add_route("player_game_index_json", "/player/{player_id:\d+}/games.json")
65     config.add_view(player_game_index_json, route_name="player_game_index_json", renderer="jsonp")
66
67     config.add_route("player_info", "/player/{id:\d+}")
68     config.add_view(player_info, route_name="player_info", accept="text/html",
69                     renderer="player_info.mako")
70     config.add_view(player_info_json, route_name="player_info", accept="application/json",
71                     renderer="jsonp")
72
73     config.add_route("player_info_json", "/player/{id:\d+}.json")
74     config.add_view(player_info_json, route_name="player_info_json", renderer="jsonp")
75
76     config.add_route("player_hashkey_info_text", "/player/me")
77     config.add_view(player_hashkey_info_text, route_name="player_hashkey_info_text",
78                     renderer="player_hashkey_info_text.mako")
79
80     config.add_route("player_hashkey_info_json", "/player/me.json")
81     config.add_view(player_hashkey_info_json, route_name="player_hashkey_info_json",
82                     renderer="jsonp")
83
84     config.add_route("player_elo_info_text", "/player/{hashkey}/elo.txt")
85     config.add_view(player_elo_info_text, route_name="player_elo_info_text",
86                     renderer="player_elo_info_text.mako")
87
88     # FIXME - needs an additional method to convert to JSON
89     config.add_route("player_elo_info_json", "/player/{hashkey}/elo.json")
90     config.add_view(player_elo_info_json, route_name="player_elo_info_json", renderer="jsonp")
91
92     config.add_route("player_accuracy", "/player/{id:\d+}/accuracy")
93     config.add_view(player_accuracy_json, route_name="player_accuracy", renderer="jsonp")
94
95     config.add_route("player_index", "/players")
96     config.add_view(player_index, route_name="player_index", accept="text/html",
97                     renderer="player_index.mako")
98     config.add_view(player_index_json, route_name="player_index", accept="application/json",
99                     renderer="jsonp")
100
101     config.add_route("player_index_json", "/players.json")
102     config.add_view(player_index_json, route_name="player_index_json", renderer="jsonp")
103
104     config.add_route("player_captimes", "/player/{player_id:\d+}/captimes")
105     config.add_view(player_captimes, route_name="player_captimes", accept="text/html",
106                     renderer="player_captimes.mako")
107     config.add_view(player_captimes_json, route_name="player_captimes", accept="application/json",
108                     renderer="jsonp")
109
110     config.add_route("player_captimes_json", "/player/{player_id:\d+}/captimes.json")
111     config.add_view(player_captimes_json, route_name="player_captimes_json", renderer="jsonp")
112
113     config.add_route("player_weaponstats_data_json", "/player/{id:\d+}/weaponstats.json")
114     config.add_view(player_weaponstats_data_json, route_name="player_weaponstats_data_json",
115                     renderer="jsonp")
116
117     config.add_route("top_players_index", "/topactive")
118     config.add_view(top_players_index, route_name="top_players_index",
119                     renderer="top_players_index.mako")
120
121     config.add_route("top_servers_index", "/topservers")
122     config.add_view(top_servers_index, route_name="top_servers_index",
123                     renderer="top_servers_index.mako")
124
125     config.add_route("top_maps_index", "/topmaps")
126     config.add_view(top_maps_index, route_name="top_maps_index", renderer="top_maps_index.mako")
127
128     config.add_route("player_versus", "/versus")
129     config.add_view(player_versus, route_name="player_versus", renderer="player_versus.mako")
130
131     # GAME ROUTES
132     config.add_route("game_info", "/game/{id:\d+}")
133     config.add_view(game_info, route_name="game_info", accept="text/html",
134                     renderer="game_info.mako")
135     config.add_view(game_info_json, route_name="game_info", accept="application/json",
136                     renderer="jsonp")
137
138     config.add_route("game_info_json", "/game/{id:\d+}.json")
139     config.add_view(game_info_json, route_name="game_info_json", renderer="jsonp")
140
141     config.add_route("game_index", "/games")
142     config.add_view(game_finder, route_name="game_index", accept="text/html",
143                     renderer="game_finder.mako")
144     config.add_view(game_finder_json, route_name="game_index", accept="application/json",
145                     renderer="json")
146
147     config.add_route("game_index_json", "/games.json")
148     config.add_view(game_finder_json, route_name="game_index_json", renderer="jsonp")
149
150     # SERVER ROUTES
151     config.add_route("server_index", "/servers")
152     config.add_view(view=ServerIndex, route_name="server_index", attr="html",
153                     renderer="server_index.mako", accept="text/html")
154     config.add_view(view=ServerIndex, route_name="server_index", attr="json", renderer="json",
155                     accept="application/json")
156
157     config.add_route("server_top_maps", "/server/{id:\d+}/topmaps")
158     config.add_view(view=ServerTopMaps, route_name="server_top_maps", attr="html",
159                     renderer="server_top_maps.mako", accept="text/html")
160     config.add_view(view=ServerTopMaps, route_name="server_top_maps", attr="json", renderer="json",
161                     accept="application/json")
162
163     config.add_route("server_top_maps_json", "/server/{id:\d+}/topmaps.json")
164     config.add_view(view=ServerTopMaps, route_name="server_top_maps_json", attr="json",
165                     renderer="json")
166
167     config.add_route("server_top_active", "/server/{id:\d+}/topactive")
168     config.add_view(view=ServerTopPlayers, route_name="server_top_active", attr="html",
169                     renderer="server_top_active.mako", accept="text/html")
170     config.add_view(view=ServerTopPlayers, route_name="server_top_active", attr="json",
171                     renderer="json", accept="application/json")
172
173     config.add_route("server_top_active_json", "/server/{id:\d+}/topactive.json")
174     config.add_view(view=ServerTopPlayers, route_name="server_top_active_json", attr="json",
175                     renderer="json")
176
177     config.add_route("server_top_scorers", "/server/{id:\d+}/topscorers")
178     config.add_view(view=ServerTopScorers, route_name="server_top_scorers", attr="html",
179                     renderer="server_top_scorers.mako", accept="text/html")
180     config.add_view(view=ServerTopScorers, route_name="server_top_scorers", attr="json",
181                     renderer="json", accept="application/json")
182
183     config.add_route("server_top_scorers_json", "/server/{id:\d+}/topscorers.json")
184     config.add_view(view=ServerTopScorers, route_name="server_top_scorers_json", attr="json",
185                     renderer="json")
186
187     config.add_route("server_info", "/server/{id:\d+}")
188     config.add_view(view=ServerInfo, route_name="server_info", attr="html",
189                     renderer="server_info.mako", accept="text/html")
190     config.add_view(view=ServerInfo, route_name="server_info", attr="json", renderer="json",
191                     accept="application/json")
192
193     config.add_route("server_info_json", "/server/{id:\d+}.json")
194     config.add_view(view=ServerInfo, route_name="server_info_json", attr="json", renderer="json")
195
196     # MAP ROUTES
197     config.add_route("map_index", "/maps")
198     config.add_view(view=MapIndex, route_name="map_index", attr="html",
199                     renderer="map_index.mako", accept="text/html")
200     config.add_view(view=MapIndex, route_name="map_index", attr="json", renderer="json",
201                     accept="application/json")
202
203     config.add_route("map_index_json", "/maps.json")
204     config.add_view(view=MapIndex, route_name="map_index_json", attr="json", renderer="json")
205
206     config.add_route("map_top_scorers", "/map/{id:\d+}/topscorers")
207     config.add_view(view=MapTopScorers, route_name="map_top_scorers", attr="html",
208                     renderer="map_top_scorers.mako", accept="text/html")
209     config.add_view(view=MapTopScorers, route_name="map_top_scorers_json", attr="json",
210                     renderer="json", accept="application/json")
211
212     config.add_route("map_top_scorers_json", "/map/{id:\d+}/topscorers.json")
213     config.add_view(view=MapTopScorers, route_name="map_top_scorers", attr="json", renderer="json")
214
215     config.add_route("map_top_active", "/map/{id:\d+}/topactive")
216     config.add_view(view=MapTopPlayers, route_name="map_top_active", attr="html",
217                     renderer="map_top_active.mako", accept="text/html")
218     config.add_view(view=MapTopPlayers, route_name="map_top_active", attr="json",
219                     renderer="json", accept="application/json")
220
221     config.add_route("map_top_active_json", "/map/{id:\d+}/topactive.json")
222     config.add_view(view=MapTopPlayers, route_name="map_top_active_json", attr="json",
223                     renderer="json")
224
225     config.add_route("map_top_servers", "/map/{id:\d+}/topservers")
226     config.add_view(view=MapTopServers, route_name="map_top_servers", attr="html",
227                     renderer="map_top_servers.mako", accept="text/html")
228     config.add_view(view=MapTopServers, route_name="map_top_servers", attr="json",
229                     renderer="json", accept="application/json")
230
231     config.add_route("map_top_servers_json", "/map/{id:\d+}/topservers.json")
232     config.add_view(view=MapTopServers, route_name="map_top_servers_json", attr="json",
233                     renderer="json")
234
235     config.add_route("map_info", "/map/{id:\d+}")
236     config.add_view(map_info, route_name="map_info", accept="text/html", renderer="map_info.mako")
237     config.add_view(map_info_json, route_name="map_info", accept="application/json",
238                     renderer="json")
239
240     config.add_route("map_info_json", "/map/{id:\d+}.json")
241     config.add_view(map_info_json, route_name="map_info_json", renderer="jsonp")
242
243     config.add_route("map_captimes", "/map/{id:\d+}/captimes")
244     config.add_view(map_captimes, route_name="map_captimes", accept="text/html",
245                     renderer="map_captimes.mako")
246     config.add_view(map_captimes_json, route_name="map_captimes", accept="application/json",
247                     renderer="json")
248
249     config.add_route("map_captimes_json", "/map/{id:\d+}/captimes.json")
250     config.add_view(map_captimes_json, route_name="map_captimes_json", renderer="jsonp")
251
252     # SEARCH ROUTES
253     config.add_route("search", "search")
254     config.add_view(search, route_name="search", accept="text/html", renderer="search.mako")
255     config.add_view(search_json, route_name="search", accept="application/json", renderer="jsonp")
256
257     config.add_route("search_json", "search.json")
258     config.add_view(search_json, route_name="search_json", renderer="jsonp")
259
260     # ADMIN ROUTES
261     config.add_forbidden_view(forbidden, renderer="forbidden.mako")
262
263     config.add_route("login", "/login")
264     config.add_view(login, route_name="login", check_csrf=True, renderer="json")
265
266     config.add_route("merge", "/admin/merge")
267     config.add_view(merge, route_name="merge", renderer="merge.mako", permission="merge")
268
269     return config.make_wsgi_app()