Formatting.
[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", renderer="game_finder.mako")
145
146     config.add_route("game_index_json", "/games.json")
147     config.add_view(game_finder_json, route_name="game_index_json", renderer="jsonp")
148
149     # SERVER ROUTES
150     config.add_route("server_index", "/servers")
151     config.add_view(view=ServerIndex, route_name="server_index", attr="html",
152                     renderer="server_index.mako", accept="text/html")
153     config.add_view(view=ServerIndex, route_name="server_index", attr="json", renderer="json",
154                     accept="application/json")
155
156     config.add_route("server_top_maps", "/server/{id:\d+}/topmaps")
157     config.add_view(view=ServerTopMaps, route_name="server_top_maps", attr="html",
158                     renderer="server_top_maps.mako", accept="text/html")
159     config.add_view(view=ServerTopMaps, route_name="server_top_maps", attr="json", renderer="json",
160                     accept="application/json")
161
162     config.add_route("server_top_maps_json", "/server/{id:\d+}/topmaps.json")
163     config.add_view(view=ServerTopMaps, route_name="server_top_maps_json", attr="json",
164                     renderer="json")
165
166     config.add_route("server_top_active", "/server/{id:\d+}/topactive")
167     config.add_view(view=ServerTopPlayers, route_name="server_top_active", attr="html",
168                     renderer="server_top_active.mako", accept="text/html")
169     config.add_view(view=ServerTopPlayers, route_name="server_top_active", attr="json",
170                     renderer="json", accept="application/json")
171
172     config.add_route("server_top_active_json", "/server/{id:\d+}/topactive.json")
173     config.add_view(view=ServerTopPlayers, route_name="server_top_active_json", attr="json",
174                     renderer="json")
175
176     config.add_route("server_top_scorers", "/server/{id:\d+}/topscorers")
177     config.add_view(view=ServerTopScorers, route_name="server_top_scorers", attr="html",
178                     renderer="server_top_scorers.mako", accept="text/html")
179     config.add_view(view=ServerTopScorers, route_name="server_top_scorers", attr="json",
180                     renderer="json", accept="application/json")
181
182     config.add_route("server_top_scorers_json", "/server/{id:\d+}/topscorers.json")
183     config.add_view(view=ServerTopScorers, route_name="server_top_scorers_json", attr="json",
184                     renderer="json")
185
186     config.add_route("server_info", "/server/{id:\d+}")
187     config.add_view(view=ServerInfo, route_name="server_info", attr="html",
188                     renderer="server_info.mako", accept="text/html")
189     config.add_view(view=ServerInfo, route_name="server_info", attr="json", renderer="json",
190                     accept="application/json")
191
192     config.add_route("server_info_json", "/server/{id:\d+}.json")
193     config.add_view(view=ServerInfo, route_name="server_info_json", attr="json", renderer="json")
194
195     # MAP ROUTES
196     config.add_route("map_index", "/maps")
197     config.add_view(view=MapIndex, route_name="map_index", attr="html",
198                     renderer="map_index.mako", accept="text/html")
199     config.add_view(view=MapIndex, route_name="map_index", attr="json", renderer="json",
200                     accept="application/json")
201
202     config.add_route("map_index_json", "/maps.json")
203     config.add_view(view=MapIndex, route_name="map_index_json", attr="json", renderer="json")
204
205     config.add_route("map_top_scorers", "/map/{id:\d+}/topscorers")
206     config.add_view(view=MapTopScorers, route_name="map_top_scorers", attr="html",
207                     renderer="map_top_scorers.mako", accept="text/html")
208     config.add_view(view=MapTopScorers, route_name="map_top_scorers_json", attr="json",
209                     renderer="json", accept="application/json")
210
211     config.add_route("map_top_scorers_json", "/map/{id:\d+}/topscorers.json")
212     config.add_view(view=MapTopScorers, route_name="map_top_scorers", attr="json", renderer="json")
213
214     config.add_route("map_top_active", "/map/{id:\d+}/topactive")
215     config.add_view(view=MapTopPlayers, route_name="map_top_active", attr="html",
216                     renderer="map_top_active.mako", accept="text/html")
217     config.add_view(view=MapTopPlayers, route_name="map_top_active", attr="json",
218                     renderer="json", accept="application/json")
219
220     config.add_route("map_top_active_json", "/map/{id:\d+}/topactive.json")
221     config.add_view(view=MapTopPlayers, route_name="map_top_active_json", attr="json",
222                     renderer="json")
223
224     config.add_route("map_top_servers", "/map/{id:\d+}/topservers")
225     config.add_view(view=MapTopServers, route_name="map_top_servers", attr="html",
226                     renderer="map_top_servers.mako", accept="text/html")
227     config.add_view(view=MapTopServers, route_name="map_top_servers", attr="json",
228                     renderer="json", accept="application/json")
229
230     config.add_route("map_top_servers_json", "/map/{id:\d+}/topservers.json")
231     config.add_view(view=MapTopServers, route_name="map_top_servers_json", attr="json",
232                     renderer="json")
233
234     config.add_route("map_info", "/map/{id:\d+}")
235     config.add_view(map_info, route_name="map_info", accept="text/html", renderer="map_info.mako")
236     config.add_view(map_info_json, route_name="map_info", accept="application/json",
237                     renderer="json")
238
239     config.add_route("map_info_json", "/map/{id:\d+}.json")
240     config.add_view(map_info_json, route_name="map_info_json", renderer="jsonp")
241
242     config.add_route("map_captimes", "/map/{id:\d+}/captimes")
243     config.add_view(map_captimes, route_name="map_captimes", accept="text/html",
244                     renderer="map_captimes.mako")
245     config.add_view(map_captimes_json, route_name="map_captimes", accept="application/json",
246                     renderer="json")
247
248     config.add_route("map_captimes_json", "/map/{id:\d+}/captimes.json")
249     config.add_view(map_captimes_json, route_name="map_captimes_json", renderer="jsonp")
250
251     # SEARCH ROUTES
252     config.add_route("search", "search")
253     config.add_view(search, route_name="search", accept="text/html", renderer="search.mako")
254     config.add_view(search_json, route_name="search", accept="application/json", renderer="jsonp")
255
256     config.add_route("search_json", "search.json")
257     config.add_view(search_json, route_name="search_json", renderer="jsonp")
258
259     # ADMIN ROUTES
260     config.add_forbidden_view(forbidden, renderer="forbidden.mako")
261
262     config.add_route("login", "/login")
263     config.add_view(login, route_name="login", check_csrf=True, renderer="json")
264
265     config.add_route("merge", "/admin/merge")
266     config.add_view(merge, route_name="merge", renderer="merge.mako", permission="merge")
267
268     return config.make_wsgi_app()