]> de.git.xonotic.org Git - xonotic/xonstat.git/commitdiff
Add fuzzy dates.
authorAnt Zucaro <azucaro@gmail.com>
Wed, 1 Jun 2011 14:57:45 +0000 (10:57 -0400)
committerAnt Zucaro <azucaro@gmail.com>
Wed, 1 Jun 2011 14:57:45 +0000 (10:57 -0400)
xonstat/models.py
xonstat/templates/player_info.mako
xonstat/util.py [changed mode: 0644->0755]

index bcebb9c128069a13e7320acb657c0fa3983e4d1c..79453626a0d88dab5cecf95b6c034ab524d4cb13 100755 (executable)
@@ -3,7 +3,7 @@ from sqlalchemy.orm import mapper
 from sqlalchemy.orm import scoped_session
 from sqlalchemy.orm import sessionmaker
 from sqlalchemy.ext.declarative import declarative_base
-from xonstat.util import strip_colors, html_colors
+from xonstat.util import strip_colors, html_colors, pretty_date
 
 DBSession = scoped_session(sessionmaker())
 Base = declarative_base()
@@ -72,6 +72,9 @@ class Game(object):
         return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt, 
                 self.game_type_cd, self.server_id)
 
+    def fuzzy_date(self):
+        return pretty_date(self.start_dt)
+
 
 class PlayerGameStat(object):
     def __init__(self, create_dt=None):
index 69829118b6f0de54b76b1f5d64d75de5f55fc5de..ad29f121fe61a15baa75ecc7203d37d31435c603 100755 (executable)
@@ -68,23 +68,16 @@ ${accuracy(weapon_stats)}
 % endif
 
 
-##### RECENT GAMES #####
+##### RECENT GAMES (v2) ####
 % if recent_games:
 <h2>Recent Games</h2>
-% for (gamestat, game, server, map) in recent_games:
-   <a href="${request.route_url("game_info", id=game.game_id)}" name="Game info page for game #${game.game_id}">#${game.game_id}:</a> <a href="${request.route_url("map_info", id=map.map_id)}" name="Map info page for ${map.name}">${map.name}</a> on <a href="${request.route_url("server_info", id=server.server_id)}" name="Server info page for ${server.name}">${server.name}</a>
-<br />
-% endfor
-<a href="${request.route_url("player_game_index", player_id=player.player_id, page=1)}" title="Game index for ${player.nick}">More games</a> played by ${player.nick_html_colors()}...
-% endif
-
-##### RECENT GAMES (v2) ####
 <table class="accuracy-table" border="1" cellpadding="3" align="center">
-<tr>
+<tr class='accuracy-table-header'>
    <td>Game Type</td>
    <td>Map</td>
    <td>Result</td>
    <td>Played</td>
+   <td>Permalink</td>
 </tr>
 % for (gamestat, game, server, map) in recent_games:
 <tr>
@@ -97,6 +90,10 @@ ${accuracy(weapon_stats)}
    Loss
    % endif
    </td>
-   <td>${game.start_dt}</td>
+   <td>${game.fuzzy_date()}</td>
+   <td><a href="${request.route_url("game_info", id=game.game_id)}" name="Game info page for game #${game.game_id}">View</a></td>
 </tr>
 % endfor
+</table>
+<a href="${request.route_url("player_game_index", player_id=player.player_id, page=1)}" title="Game index for ${player.nick}">More games</a> played by ${player.nick_html_colors()}...
+% endif
old mode 100644 (file)
new mode 100755 (executable)
index 1b54426..e9d179a
@@ -1,10 +1,12 @@
 import re
+from datetime import datetime
 
 def strip_colors(str=None):
     str = re.sub(r'\^x\w\w\w', '', str)
     str = re.sub(r'\^\d', '', str)
     return str
 
+
 def html_colors(str=None):
     orig = str
     str = re.sub(r'\^x(\w)(\w)(\w)', 
@@ -25,5 +27,59 @@ def html_colors(str=None):
 
     return str
 
+
 def page_url(page):
     return current_route_url(request, page=page, _query=request.GET)
+
+
+def pretty_date(time=False):
+    """
+    Get a datetime object or a int() Epoch timestamp and return a
+    pretty string like 'an hour ago', 'Yesterday', '3 months ago',
+    'just now', etc
+    """
+    now = datetime.now()
+    if type(time) is int:
+        diff = now - datetime.fromtimestamp(time)
+    elif isinstance(time,datetime):
+        diff = now - time 
+    elif not time:
+        diff = now - now
+    second_diff = diff.seconds
+    day_diff = diff.days
+
+    if day_diff < 0:
+        return ''
+
+    if day_diff == 0:
+        if second_diff < 10:
+            return "just now"
+        if second_diff < 60:
+            return str(second_diff) + " seconds ago"
+        if second_diff < 120:
+            return  "a minute ago"
+        if second_diff < 3600:
+            return str( second_diff / 60 ) + " minutes ago"
+        if second_diff < 7200:
+            return "an hour ago"
+        if second_diff < 86400:
+            return str( second_diff / 3600 ) + " hours ago"
+    if day_diff == 1:
+        return "Yesterday"
+    if day_diff < 7:
+        return str(day_diff) + " days ago"
+    if day_diff < 31:
+        if day_diff/7 == 1:
+            return "a week ago"
+        else:
+            return str(day_diff/7) + " weeks ago"
+    if day_diff < 365:
+        if day_diff/30 == 1:
+            return "a month ago"
+        else:
+            return str(day_diff/30) + " months ago"
+    else:
+        if day_diff/365 == 1:
+            return "a year ago"
+        else:
+            return str(day_diff/365) + " years ago"