]> git.rm.cloudns.org Git - xonotic/xonstat.git/commitdiff
Refactor the map_index views.
authorAnt Zucaro <azucaro@gmail.com>
Sun, 18 Dec 2016 23:04:29 +0000 (18:04 -0500)
committerAnt Zucaro <azucaro@gmail.com>
Sun, 18 Dec 2016 23:04:29 +0000 (18:04 -0500)
xonstat/__init__.py
xonstat/views/__init__.py
xonstat/views/map.py

index aca302daadba45bd1236a20a9f599989084d1f8a..1d2e0750b5001e955e3351857af2ba198a52e93d 100644 (file)
@@ -165,11 +165,11 @@ def main(global_config, **settings):
                     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")
index 5b34b878f52ae8da089608608e56fdb5322dc56d..5d7997b8956ebd74b692e6ae7fe88c23d7dd5b52 100644 (file)
@@ -13,9 +13,9 @@ from xonstat.views.game   import game_info, rank_index
 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
index 9a9b4e76fba3c6d7d7fd044a511311c07dd0ad8d..f1daf4aabe7f57c385ce1a0a1511fa9fe8e22773 100644 (file)
@@ -4,7 +4,7 @@ from datetime import datetime, timedelta
 
 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
@@ -13,40 +13,44 @@ from xonstat.views.helpers import RecentGame, recent_games_q
 
 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):