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.
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")
+import json
import logging
import math
import sqlalchemy
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,
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
\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
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