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