Add the fuzzy and epoch dates to the maps JSON response.
[xonotic/xonstat.git] / xonstat / models / map.py
1 """
2 Models related to maps.
3 """
4
5 from calendar import timegm
6
7 from xonstat.models.mixins import FuzzyDateMixin, EpochMixin
8 from xonstat.util import pretty_date, strip_colors, html_colors
9
10
11 class Map(FuzzyDateMixin, EpochMixin):
12     """
13     A playable map. Roughly equivalent to a pk3 file, but distinguished by name instead.
14     """
15
16     def __init__(self, name=None):
17         self.name = name
18
19     def __repr__(self):
20         return "<Map({0.map_id}, {0.name}, {0.version})>".format(self)
21
22     def to_dict(self):
23         return {
24             'map_id': self.map_id,
25             'name': self.name,
26             'version': self.version,
27             'create_dt_fuzzy': self.fuzzy_date(),
28             'create_dt_epoch': self.epoch(),
29         }
30
31
32 # TODO: investigate if this model is truly a model, or really just a query (i.e. namedtuple)
33 class MapCapTime(object):
34     """
35     Fastest flag capture times per map.
36     """
37
38     def __init__(self, row):
39         self.fastest_cap = row.fastest_cap
40         self.create_dt = row.create_dt
41         self.create_dt_epoch = timegm(row.create_dt.timetuple())
42         self.create_dt_fuzzy = pretty_date(row.create_dt)
43         self.player_id = row.player_id
44         self.player_nick = row.player_nick
45         self.player_nick_stripped = strip_colors(row.player_nick)
46         self.player_nick_html = html_colors(row.player_nick)
47         self.game_id = row.game_id
48         self.server_id = row.server_id
49         self.server_name = row.server_name
50
51     def to_dict(self):
52         return {
53             "fastest_cap": self.fastest_cap.total_seconds(),
54             "create_dt_epoch": self.create_dt_epoch,
55             "create_dt_fuzzy": self.create_dt_fuzzy,
56             "player_id": self.player_id,
57             "player_nick": self.player_nick,
58             "player_nick_stripped": self.player_nick_stripped,
59             "game_id": self.game_id,
60             "server_id": self.server_id,
61             "server_name": self.server_name,
62         }