Get rid of joined_pretty_date in favor of the mixin.
[xonotic/xonstat.git] / xonstat / __init__.py
1 import sqlahelper
2 from pyramid_beaker import set_cache_regions_from_settings
3 from pyramid.authentication import AuthTktAuthenticationPolicy
4 from pyramid.config import Configurator
5 from pyramid.httpexceptions import HTTPNotFound
6 from pyramid.renderers import JSONP
7 from sqlalchemy import engine_from_config
8 from xonstat.models import initialize_db
9 from xonstat.views import *
10 from xonstat.security import *
11
12
13 def main(global_config, **settings):
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     # Mozilla Persona as the login verifier. It defines default
32     # authentication and authorization policies.
33     config.include('pyramid_persona')
34
35     # override the authn policy to provide a callback
36     secret = settings.get('persona.secret', None)
37     authn_policy = AuthTktAuthenticationPolicy(secret, callback=groupfinder, hashalg='sha512')
38     config.set_authentication_policy(authn_policy)
39
40     # for json-encoded responses
41     config.add_renderer('jsonp', JSONP(param_name='callback'))
42
43     # for static assets
44     config.add_static_view('static', 'xonstat:static')
45
46     # robots
47     config.add_route("robots", "robots.txt")
48     config.add_view(robots, route_name="robots")
49
50     # for 404s
51     config.add_view(notfound, context=HTTPNotFound, renderer="404.mako")
52
53     # ROOT ROUTE
54     config.add_route("main_index", "/")
55     config.add_view(main_index, route_name="main_index", renderer="main_index.mako")
56
57     # MAIN SUBMISSION ROUTE
58     config.add_route("submit_stats", "stats/submit")
59     config.add_view(submit_stats, route_name="submit_stats", renderer="submit_stats.mako")
60
61     # PLAYER ROUTES
62     config.add_route("player_game_index",      "/player/{player_id:\d+}/games")
63     config.add_view(player_game_index,      route_name="player_game_index",      renderer="player_game_index.mako")
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",      renderer="player_info.mako")
70
71     config.add_route("player_info_json", "/player/{id:\d+}.json")
72     config.add_view(player_info_json, route_name="player_info_json", renderer="jsonp")
73
74     config.add_route("player_hashkey_info_text", "/player/me")
75     config.add_view(player_hashkey_info_text, route_name="player_hashkey_info_text", renderer="player_hashkey_info_text.mako")
76
77     config.add_route("player_hashkey_info_json", "/player/me.json")
78     config.add_view(player_hashkey_info_json, route_name="player_hashkey_info_json", renderer="jsonp")
79
80     config.add_route("player_elo_info_text", "/player/{hashkey}/elo.txt")
81     config.add_view(player_elo_info_text, route_name="player_elo_info_text", renderer="player_elo_info_text.mako")
82
83     # FIXME - needs an additional method to convert to JSON
84     config.add_route("player_elo_info_json", "/player/{hashkey}/elo.json")
85     config.add_view(player_elo_info_json, route_name="player_elo_info_json", renderer="jsonp")
86
87     config.add_route("player_accuracy",      "/player/{id:\d+}/accuracy")
88     config.add_view(player_accuracy_json, route_name="player_accuracy",      renderer="jsonp")
89
90     config.add_route("player_index",      "/players")
91     config.add_view(player_index,      route_name="player_index",      renderer="player_index.mako")
92
93     config.add_route("player_index_json", "/players.json")
94     config.add_view(player_index_json, route_name="player_index_json", renderer="jsonp")
95
96     config.add_route("player_captimes",      "/player/{player_id:\d+}/captimes")
97     config.add_view(player_captimes,      route_name="player_captimes",      renderer="player_captimes.mako")
98
99     config.add_route("player_captimes_json", "/player/{player_id:\d+}/captimes.json")
100     config.add_view(player_captimes_json, route_name="player_captimes_json", renderer="jsonp")
101
102     config.add_route("player_weaponstats_data_json", "/player/{id:\d+}/weaponstats.json")
103     config.add_view(player_weaponstats_data_json, route_name="player_weaponstats_data_json", renderer="jsonp")
104
105     config.add_route("top_players_index", "/topactive")
106     config.add_view(top_players_index, route_name="top_players_index", renderer="top_players_index.mako")
107
108     config.add_route("top_servers_index", "/topservers")
109     config.add_view(top_servers_index, route_name="top_servers_index", renderer="top_servers_index.mako")
110
111     config.add_route("top_maps_index", "/topmaps")
112     config.add_view(top_maps_index, route_name="top_maps_index", renderer="top_maps_index.mako")
113
114     config.add_route("player_versus", "/versus")
115     config.add_view(player_versus, route_name="player_versus", renderer="player_versus.mako")
116
117     # GAME ROUTES
118     config.add_route("game_info",      "/game/{id:\d+}")
119     config.add_view(game_info,      route_name="game_info",      renderer="game_info.mako")
120
121     config.add_route("game_info_json", "/game/{id:\d+}.json")
122     config.add_view(game_info_json, route_name="game_info_json", renderer="jsonp")
123
124     config.add_route("rank_index", "/ranks/{game_type_cd}")
125     config.add_view(rank_index,      route_name="rank_index",      renderer="rank_index.mako")
126
127     config.add_route("rank_index_json", "/ranks/{game_type_cd}.json")
128     config.add_view(rank_index_json, route_name="rank_index_json", renderer="jsonp")
129
130     config.add_route("game_index", "/games")
131     config.add_view(game_finder, route_name="game_index", renderer="game_finder.mako")
132
133     config.add_route("game_index_json", "/games.json")
134     config.add_view(game_finder_json, route_name="game_index_json", renderer="jsonp")
135
136     # SERVER ROUTES
137     config.add_route("server_index", "/servers")
138     config.add_view(view=ServerIndex, route_name="server_index", attr="html",
139                     renderer="server_index.mako", accept="text/html")
140     config.add_view(view=ServerIndex, route_name="server_index", attr="json", renderer="json",
141                     accept="application/json")
142
143     config.add_route("server_top_maps", "/server/{id:\d+}/topmaps")
144     config.add_view(view=ServerTopMaps, route_name="server_top_maps", attr="html",
145                     renderer="server_top_maps.mako", accept="text/html")
146     config.add_view(view=ServerTopMaps, route_name="server_top_maps", attr="json", renderer="json",
147                     accept="application/json")
148
149     config.add_route("server_top_active", "/server/{id:\d+}/topactive")
150     config.add_view(view=ServerTopPlayers, route_name="server_top_active", attr="html",
151                     renderer="server_top_active.mako", accept="text/html")
152     config.add_view(view=ServerTopPlayers, route_name="server_top_active", attr="json",
153                     renderer="json", accept="application/json")
154
155     config.add_route("server_top_scorers", "/server/{id:\d+}/topscorers")
156     config.add_view(view=ServerTopScorers, route_name="server_top_scorers", attr="html",
157                     renderer="server_top_scorers.mako", accept="text/html")
158     config.add_view(view=ServerTopScorers, route_name="server_top_scorers", attr="json",
159                     renderer="json", accept="application/json")
160
161     config.add_route("server_info", "/server/{id:\d+}")
162     config.add_view(view=ServerInfo, route_name="server_info", attr="html",
163                     renderer="server_info.mako", accept="text/html")
164     config.add_view(view=ServerInfo, route_name="server_info", attr="json", renderer="json",
165                     accept="application/json")
166
167     # MAP ROUTES
168     config.add_route("map_index",      "/maps")
169     config.add_view(map_index,      route_name="map_index",      renderer="map_index.mako")
170
171     config.add_route("map_index_json", "/maps.json")
172     config.add_view(map_index_json, route_name="map_index_json", renderer="jsonp")
173
174     config.add_route("map_info",      "/map/{id:\d+}")
175     config.add_view(map_info,      route_name="map_info",      renderer="map_info.mako")
176
177     config.add_route("map_info_json", "/map/{id:\d+}.json")
178     config.add_view(map_info_json, route_name="map_info_json", renderer="jsonp")
179
180     config.add_route("map_captimes",      "/map/{id:\d+}/captimes")
181     config.add_view(map_captimes,      route_name="map_captimes",      renderer="map_captimes.mako")
182
183     config.add_route("map_captimes_json", "/map/{id:\d+}/captimes.json")
184     config.add_view(map_captimes_json, route_name="map_captimes_json", renderer="jsonp")
185
186     # SEARCH ROUTES
187     config.add_route("search",      "search")
188     config.add_view(search,      route_name="search",      renderer="search.mako")
189
190     config.add_route("search_json", "search.json")
191     config.add_view(search_json, route_name="search_json", renderer="jsonp")
192
193     # ADMIN ROUTES
194     config.add_forbidden_view(forbidden, renderer="forbidden.mako")
195
196     config.add_route("login", "/login")
197     config.add_view(login, route_name="login", check_csrf=True, renderer="json")
198
199     config.add_route("merge", "/admin/merge")
200     config.add_view(merge, route_name="merge", renderer="merge.mako", permission="merge")
201
202     return config.make_wsgi_app()