diff options
author | Pacien TRAN-GIRARD | 2014-10-24 18:05:48 +0000 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2014-10-24 18:05:48 +0000 |
commit | 4403fda939ef42aeffeccb343d74f3dc3b840f91 (patch) | |
tree | 63fd704f15f3030f1455aad0ef92403c5d093c70 /wikibania/ban/Ban.py | |
parent | 16529a0d212e1387eacd590c0e5e1b1a13dc2641 (diff) | |
parent | bdf9099df8c2a4636b0ad0e710b73330877eef37 (diff) | |
download | wikistats-4403fda939ef42aeffeccb343d74f3dc3b840f91.tar.gz |
Merge branch 'refactor' into 'master'
Refactor
See merge request !1
Diffstat (limited to 'wikibania/ban/Ban.py')
-rw-r--r-- | wikibania/ban/Ban.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/wikibania/ban/Ban.py b/wikibania/ban/Ban.py new file mode 100644 index 0000000..e06ca89 --- /dev/null +++ b/wikibania/ban/Ban.py | |||
@@ -0,0 +1,37 @@ | |||
1 | from datetime import datetime | ||
2 | |||
3 | import pygeoip | ||
4 | |||
5 | |||
6 | ISO_TIMESTAMP = "%Y-%m-%dT%H:%M:%SZ" | ||
7 | |||
8 | |||
9 | class Ban: | ||
10 | def __init__(self, geoip_looker, user=None, timestamp=None, expiry=None, timestamp_format=ISO_TIMESTAMP): | ||
11 | self.geoip_looker = geoip_looker | ||
12 | self.timestamp_format = timestamp_format | ||
13 | |||
14 | self.user = user | ||
15 | self.timestamp = timestamp | ||
16 | self.expiry = expiry | ||
17 | |||
18 | def items(self): | ||
19 | return { | ||
20 | "user": self.user, | ||
21 | "timestamp": self.timestamp.strftime(ISO_TIMESTAMP), | ||
22 | "expiry": self.expiry.strftime(ISO_TIMESTAMP), | ||
23 | } | ||
24 | |||
25 | def hydrate(self, ban_dict): | ||
26 | self.user = ban_dict["user"] | ||
27 | self.timestamp = datetime.strptime(ban_dict["timestamp"], ISO_TIMESTAMP) | ||
28 | self.expiry = datetime.strptime(ban_dict["expiry"], ISO_TIMESTAMP) | ||
29 | |||
30 | def calc_duration(self): | ||
31 | return (self.expiry - self.timestamp).days | ||
32 | |||
33 | def lookup_country_code(self): | ||
34 | try: | ||
35 | return self.geoip_looker.country_code_by_addr(self.user).lower() | ||
36 | except pygeoip.GeoIPError: | ||
37 | return "UNKNOWN" | ||