]> git.xonotic.org Git - xonotic/xonstat.git/commitdiff
Add initial support for JSON on map_index view.
authorAnt Zucaro <azucaro@gmail.com>
Wed, 16 May 2012 02:37:38 +0000 (22:37 -0400)
committerAnt Zucaro <azucaro@gmail.com>
Wed, 16 May 2012 02:37:38 +0000 (22:37 -0400)
This commit adds initial JSON support to the map_index view.
I am giving each model class a to_dict function that returns
a select number of its instance variables back in the form of
a dictionary, which can then be returned directly by a JSON
renderer. I'm not sure if this is the right way to go, but
it works for now.

I've also ripped out the main "data-calling" piece of each
view callable into a separate function so that the JSON-
enabled view callable can use it. The non-JSON one just
returns the result of that function, while the JSON one
massages the data to be able to make it fit better for
API-like consumption.

xonstat/__init__.py
xonstat/models.py
xonstat/views/__init__.py
xonstat/views/map.py

index 15662a01c1167ce86bd880e03d331c049dd576aa..d3a6d98a5ca8e1e07d1fe185658c06bbe583acd2 100755 (executable)
@@ -73,6 +73,10 @@ def main(global_config, **settings):
         renderer="server_info.mako")
 
     # MAP ROUTES
+    config.add_route("map_index_json", "/maps.json")
+    config.add_view(map_index_json, route_name="map_index_json",
+        renderer="json")
+
     config.add_route("map_index", "/maps")
     config.add_view(map_index, route_name="map_index",
         renderer="map_index.mako")
index d53e1daeb4fa4cf3bd187d4ec105b6021c02a335..d5cd88540db2b4b154626752620268daf6c16776 100755 (executable)
@@ -1,3 +1,4 @@
+import json
 import logging
 import math
 import sqlalchemy
@@ -66,6 +67,9 @@ class Map(object):
     def __repr__(self):
         return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
 
+    def to_dict(self):
+        return {'map_id':self.map_id, 'name':self.name}
+
 
 class Game(object):
     def __init__(self, game_id=None, start_dt=None, game_type_cd=None, 
index 06ab55a5fc304e15b46e0425462ac39b9187f5e7..5592f2cb4642027f90ea1d72530bd3c598db5b42 100755 (executable)
@@ -2,7 +2,7 @@ from xonstat.views.submission import stats_submit
 from xonstat.views.player import player_index, player_info, player_game_index\r
 from xonstat.views.player import player_accuracy\r
 from xonstat.views.game import game_index, game_info, rank_index\r
-from xonstat.views.map import map_info, map_index\r
+from xonstat.views.map import map_info, map_index, map_index_json\r
 from xonstat.views.server import server_info, server_game_index, server_index\r
 from xonstat.views.search import *\r
 from xonstat.views.main import main_index\r
index 66c524b880486f4ba147aaddf953e6eb91fd91c8..6b947eb61ce08c8a2abfc719b0c3dd742373ce22 100755 (executable)
@@ -10,7 +10,7 @@ from xonstat.util import page_url
 \r
 log = logging.getLogger(__name__)\r
 \r
-def map_index(request):\r
+def _map_index_data(request):\r
     """\r
     Provides a list of all the current maps. \r
     """\r
@@ -31,6 +31,24 @@ def map_index(request):
     return {'maps':maps, }\r
 \r
 \r
+def map_index(request):\r
+    """\r
+    Provides a list of all the current maps. \r
+    """\r
+    return _map_index_data(request)\r
+\r
+\r
+def map_index_json(request):\r
+    """\r
+    Provides a JSON-serialized list of all the current maps. \r
+    """\r
+    view_data =  _map_index_data(request)\r
+\r
+    maps = [m.to_dict() for m in view_data['maps']]\r
+\r
+    return maps\r
+\r
+\r
 def map_info(request):\r
     """\r
     List the information stored about a given map. \r