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