]> git.rm.cloudns.org Git - xonotic/xonstat.git/commitdiff
Applied feedback from Antibody, improved the *->json functionality
authorJan D. Behrens <zykure@web.de>
Mon, 24 Sep 2012 18:44:21 +0000 (20:44 +0200)
committerJan D. Behrens <zykure@web.de>
Mon, 24 Sep 2012 18:52:11 +0000 (20:52 +0200)
xonstat/models.py
xonstat/util.py
xonstat/views/player.py

index 6b92e10cff17c03e4ea796e2f6d5be7af20cc1c2..73caedd2c54744ada57eb1ddc38186381617817b 100644 (file)
@@ -37,11 +37,10 @@ class Player(object):
         return "<Player(%s, %s)>" % (self.player_id, self.nick.encode('utf-8'))
 
     def to_dict(self):
-        return {'player_id':self.player_id, 'name':self.nick.encode('utf-8'),
+        return {'player_id':self.player_id, 'nick':self.nick,
             'joined':self.create_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
             'active_ind':self.active_ind, 'location':self.location,
-            'stripped_nick':self.stripped_nick.encode('utf-8'),
-            'nick_html_colors':self.nick_html_colors().encode('utf-8')}
+            'stripped_nick':self.stripped_nick}
 
     def epoch(self):
         return timegm(self.create_dt.timetuple())
@@ -73,7 +72,7 @@ class Server(object):
         return "<Server(%s, %s)>" % (self.server_id, self.name.encode('utf-8'))
 
     def to_dict(self):
-        return {'server_id':self.server_id, 'name':self.name.encode('utf-8'),
+        return {'server_id':self.server_id, 'name':self.name,
             'ip_addr':self.ip_addr, 'location':self.location}
 
     def fuzzy_date(self):
@@ -91,8 +90,7 @@ class Map(object):
         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, 'version':self.version,
-            'pk3_name':self.pk3_name, 'curl_url':self.curl_url}
+        return {'map_id':self.map_id, 'name':self.name, 'version':self.version,}
 
     def fuzzy_date(self):
         return pretty_date(self.create_dt)
@@ -115,7 +113,8 @@ class Game(object):
         return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt, self.game_type_cd, self.server_id)
 
     def to_dict(self):
-        return {'game_id':self.game_id, 'start':self.start_dt.strftime('%Y-%m-%dT%H:%M:%SZ'), 'game_type_cd':self.game_type_cd, 'server_id':self.server_id}
+        return {'game_id':self.game_id, 'start':self.start_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
+                'game_type_cd':self.game_type_cd, 'server_id':self.server_id}
 
     def fuzzy_date(self):
         return pretty_date(self.start_dt)
index 1f440ed8abecb90b42bbdb4c6ab3b988bf3f581e..72270f3cfd4e6279c0fe8235efb250c88fe7c592 100644 (file)
@@ -3,6 +3,7 @@ from colorsys import rgb_to_hls, hls_to_rgb
 from cgi import escape as html_escape
 from datetime import datetime, timedelta
 from decimal import Decimal
+from collections import namedtuple
 
 # Map of special chars to ascii from Darkplace's console.c.
 _qfont_table = [
@@ -156,22 +157,26 @@ def pretty_date(time=False):
 def datetime_seconds(td):
     return float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
 
-def namedtuple_to_dict(tup):
-    result = {}
-    for key,value in tup._asdict().items():
-        result[key] = value
-    return result
-
-def fix_json_types(data):
+def to_json(data):
+    if not type(data) == dict:
+        # assume it's a named tuple
+        data = data._asdict()
     result = {}
     for key,value in data.items():
-        if type(value) == Decimal:
+        if value == None:
+            result[key] = None
+        elif type(value) in [bool,int,long,float,complex,str]:
+            result[key] = value
+        elif type(value) == unicode:
+            result[key] = value.encode('utf-8')
+        elif type(value) == Decimal:
             result[key] = float(value)
         elif type(value) == datetime:
             result[key] = value.strftime('%Y-%m-%dT%H:%M:%SZ')
         elif type(value) == timedelta:
             result[key] = datetime_seconds(value)
         else:
-            result[key] = value
+            print key,value
+            result[key] = to_json(value.to_dict())
     return result
 
index 9cfb0dbff9fb337ef2a57282dacb397d504d3c7b..805b8634fe652fe547a0ebea4ebb033591aa6e6d 100644 (file)
@@ -11,7 +11,7 @@ from pyramid.url import current_route_url
 from sqlalchemy import desc, distinct
 from webhelpers.paginate import Page, PageURL
 from xonstat.models import *
-from xonstat.util import page_url, namedtuple_to_dict, fix_json_types
+from xonstat.util import page_url, to_json
 
 log = logging.getLogger(__name__)
 
@@ -557,30 +557,27 @@ def player_info_json(request):
 
     games_played = {}
     for game in player_info['games_played']:
-        games_played[game.game_type_cd] = namedtuple_to_dict(game)
+        games_played[game.game_type_cd] = to_json(game)
     
     overall_stats = {}
     for gt,stats in player_info['overall_stats'].items():
-        overall_stats[gt] = fix_json_types(namedtuple_to_dict(stats))
+        overall_stats[gt] = to_json(stats)
     
     elos = {}
     for gt,elo in player_info['elos'].items():
-        elos[gt] = fix_json_types(elo.to_dict())
+        elos[gt] = to_json(elo.to_dict())
     
     ranks = {}
     for gt,rank in player_info['ranks'].items():
-        ranks[gt] = namedtuple_to_dict(rank)
+        ranks[gt] = to_json(rank)
     
     fav_maps = {}
-    for gt,stats in player_info['fav_maps'].items():
-        fav_maps[gt] = namedtuple_to_dict(stats)
+    for gt,mapinfo in player_info['fav_maps'].items():
+        fav_maps[gt] = to_json(mapinfo)
      
     recent_games = []
     for game in player_info['recent_games']:
-        entry = {}
-        for key,value in namedtuple_to_dict(game).items():
-            entry[key] = fix_json_types(value.to_dict())
-        recent_games.append(entry)
+        recent_games.append(to_json(game))
     
     recent_weapons = player_info['recent_weapons']