From: Ant Zucaro Date: Wed, 16 May 2012 02:37:38 +0000 (-0400) Subject: Add initial support for JSON on map_index view. X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fxonstat.git;a=commitdiff_plain;h=a772877fc61426eda6928c52967aa257c4ade111 Add initial support for JSON on map_index view. 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. --- diff --git a/xonstat/__init__.py b/xonstat/__init__.py index 15662a0..d3a6d98 100755 --- a/xonstat/__init__.py +++ b/xonstat/__init__.py @@ -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") diff --git a/xonstat/models.py b/xonstat/models.py index d53e1da..d5cd885 100755 --- a/xonstat/models.py +++ b/xonstat/models.py @@ -1,3 +1,4 @@ +import json import logging import math import sqlalchemy @@ -66,6 +67,9 @@ class Map(object): def __repr__(self): return "" % (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, diff --git a/xonstat/views/__init__.py b/xonstat/views/__init__.py index 06ab55a..5592f2c 100755 --- a/xonstat/views/__init__.py +++ b/xonstat/views/__init__.py @@ -2,7 +2,7 @@ from xonstat.views.submission import stats_submit from xonstat.views.player import player_index, player_info, player_game_index from xonstat.views.player import player_accuracy from xonstat.views.game import game_index, game_info, rank_index -from xonstat.views.map import map_info, map_index +from xonstat.views.map import map_info, map_index, map_index_json from xonstat.views.server import server_info, server_game_index, server_index from xonstat.views.search import * from xonstat.views.main import main_index diff --git a/xonstat/views/map.py b/xonstat/views/map.py index 66c524b..6b947eb 100755 --- a/xonstat/views/map.py +++ b/xonstat/views/map.py @@ -10,7 +10,7 @@ from xonstat.util import page_url log = logging.getLogger(__name__) -def map_index(request): +def _map_index_data(request): """ Provides a list of all the current maps. """ @@ -31,6 +31,24 @@ def map_index(request): return {'maps':maps, } +def map_index(request): + """ + Provides a list of all the current maps. + """ + return _map_index_data(request) + + +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']] + + return maps + + def map_info(request): """ List the information stored about a given map.