Use mixins to avoid duplication.
[xonotic/xonstat.git] / xonstat / models / player.py
index 564c18da225ab31d50160cf3bb68952ea664f653..2564dd3317acd666735a562607d10ff3a34c0905 100644 (file)
@@ -4,26 +4,22 @@ Models related to players.
 
 from calendar import timegm
 
-from xonstat.util import html_colors, strip_colors, pretty_date, qfont_decode
+from xonstat.models.mixins import FuzzyDateMixin, EpochMixin, NickColorsMixin
+from xonstat.util import strip_colors, pretty_date, qfont_decode
 
 
-class Player(object):
+class Player(EpochMixin, NickColorsMixin):
     """
     A player, which can represent either a human or a bot.
     """
 
-    def nick_html_colors(self, limit=None):
-        if self.nick is None:
-            return "Anonymous Player"
-        else:
-            return html_colors(self.nick, limit)
-
     def nick_strip_colors(self):
         if self.nick is None:
             return "Anonymous Player"
         else:
             return strip_colors(self.nick)
 
+    # TODO: use FuzzyDateMixin instead, but change the method calls
     def joined_pretty_date(self):
         return pretty_date(self.create_dt)
 
@@ -40,9 +36,6 @@ class Player(object):
             'stripped_nick': qfont_decode(self.stripped_nick),
         }
 
-    def epoch(self):
-        return timegm(self.create_dt.timetuple())
-
 
 class Achievement(object):
     """
@@ -134,17 +127,11 @@ class PlayerElo(object):
         }
 
 
-class PlayerRank(object):
+class PlayerRank(NickColorsMixin):
     """
     A player's rank for a given game type.
     """
 
-    def nick_html_colors(self, limit=None):
-        if self.nick is None:
-            return "Anonymous Player"
-        else:
-            return html_colors(self.nick, limit)
-
     def __repr__(self):
         return ("<PlayerRank(pid={0.player_id}, gametype={0.game_type_cd}, rank={0.rank})>"
                 .format(self))
@@ -157,7 +144,7 @@ class PlayerRank(object):
         }
 
 
-class PlayerCaptime(object):
+class PlayerCaptime(FuzzyDateMixin, EpochMixin):
     """
     A flag capture time for a player on a given map.
     """
@@ -172,12 +159,6 @@ class PlayerCaptime(object):
     def __repr__(self):
         return "<PlayerCaptime(pid={0.player_id}, map_id={0.map_id}, mod={0.mod})>".format(self)
 
-    def fuzzy_date(self):
-        return pretty_date(self.create_dt)
-
-    def epoch(self):
-        return timegm(self.create_dt.timetuple())
-
 
 class PlayerGroups(object):
     """