From bdf9099df8c2a4636b0ad0e710b73330877eef37 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Fri, 24 Oct 2014 19:59:05 +0200 Subject: Very cleaner, much class, such readable, wow --- wikibania/ban/Ban.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 wikibania/ban/Ban.py (limited to 'wikibania/ban/Ban.py') 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 @@ +from datetime import datetime + +import pygeoip + + +ISO_TIMESTAMP = "%Y-%m-%dT%H:%M:%SZ" + + +class Ban: + def __init__(self, geoip_looker, user=None, timestamp=None, expiry=None, timestamp_format=ISO_TIMESTAMP): + self.geoip_looker = geoip_looker + self.timestamp_format = timestamp_format + + self.user = user + self.timestamp = timestamp + self.expiry = expiry + + def items(self): + return { + "user": self.user, + "timestamp": self.timestamp.strftime(ISO_TIMESTAMP), + "expiry": self.expiry.strftime(ISO_TIMESTAMP), + } + + def hydrate(self, ban_dict): + self.user = ban_dict["user"] + self.timestamp = datetime.strptime(ban_dict["timestamp"], ISO_TIMESTAMP) + self.expiry = datetime.strptime(ban_dict["expiry"], ISO_TIMESTAMP) + + def calc_duration(self): + return (self.expiry - self.timestamp).days + + def lookup_country_code(self): + try: + return self.geoip_looker.country_code_by_addr(self.user).lower() + except pygeoip.GeoIPError: + return "UNKNOWN" -- cgit v1.2.3