accept="application/json")
# MAP ROUTES
- config.add_route("map_index", "/maps")
- config.add_view(map_index, route_name="map_index", renderer="map_index.mako")
-
- config.add_route("map_index_json", "/maps.json")
- config.add_view(map_index_json, route_name="map_index_json", renderer="jsonp")
+ config.add_route("map_index", "/maps")
+ config.add_view(view=MapIndex, route_name="map_index", attr="html",
+ renderer="map_index.mako", accept="text/html")
+ config.add_view(view=MapIndex, route_name="map_index", attr="json", renderer="json",
+ accept="application/json")
config.add_route("map_info", "/map/{id:\d+}")
config.add_view(map_info, route_name="map_info", renderer="map_info.mako")
from xonstat.views.game import game_info_json, rank_index_json
from xonstat.views.game import game_finder, game_finder_json
-from xonstat.views.map import map_info, map_index
-from xonstat.views.map import map_info_json, map_index_json
-from xonstat.views.map import map_captimes, map_captimes_json
+from xonstat.views.map import MapIndex
+from xonstat.views.map import map_info, map_info_json
+from xonstat.views.map import map_captimes, map_captimes_json
from xonstat.views.server import ServerIndex, ServerTopMaps, ServerTopScorers, ServerTopPlayers
from xonstat.views.server import ServerInfo
import sqlalchemy.sql.expression as expr
import sqlalchemy.sql.functions as func
-from pyramid import httpexceptions
+from pyramid.httpexceptions import HTTPNotFound
from webhelpers.paginate import Page
from xonstat.models import DBSession, Server, Map, Game, PlayerGameStat, Player, PlayerCaptime
from xonstat.models.map import MapCapTime
log = logging.getLogger(__name__)
-def _map_index_data(request):
- if request.params.has_key('page'):
- current_page = request.params['page']
- else:
- current_page = 1
+# Defaults
+INDEX_COUNT = 20
- try:
- map_q = DBSession.query(Map).\
- order_by(Map.map_id.desc())
- maps = Page(map_q, current_page, items_per_page=25, url=page_url)
+class MapIndex(object):
+ """Returns a list of maps."""
- except Exception as e:
- maps = None
+ def __init__(self, request):
+ """Common parameter parsing."""
+ self.request = request
+ self.page = request.params.get("page", 1)
- return {'maps':maps, }
+ # all views share this data, so we'll precalculate
+ self.maps = self.map_index()
+ def map_index(self):
+ """Returns the raw data shared by all renderers."""
+ try:
+ map_q = DBSession.query(Map).order_by(Map.map_id.desc())
+ maps = Page(map_q, self.page, items_per_page=INDEX_COUNT, url=page_url)
-def map_index(request):
- """
- Provides a list of all the current maps.
- """
- return _map_index_data(request)
+ except Exception as e:
+ log.debug(e)
+ raise HTTPNotFound
+ return maps
-def map_index_json(request):
- """
- Provides a JSON-serialized list of all the current maps.
- """
- view_data = _map_index_data(request)
-
- maps = [m.to_dict() for m in view_data['maps']]
+ def html(self):
+ """For rendering this data using something HTML-based."""
+ return {
+ 'maps': self.maps,
+ }
- return maps
+ def json(self):
+ """For rendering this data using JSON."""
+ return {
+ 'maps': [m.to_dict() for m in self.maps],
+ }
def _map_info_data(request):