# HG changeset patch # User Dennis Fink # Date 2015-08-30 15:24:17 # Node ID 6d5132a446e9ddd77a07b08974da256911bd2b4b # Parent 3ec5be85acb4383f589fca99e613ba23d91070b5 Rewrote checking of ip and setting the country diff --git a/ennstatus/api/views.py b/ennstatus/api/views.py --- a/ennstatus/api/views.py +++ b/ennstatus/api/views.py @@ -70,19 +70,22 @@ def update(): ip = data['ip6'] else: ip = request.remote_addr + try: + temp_ip = ipaddress.ip_address(ip) + except ipaddress.AddressValueError: + return 'IP not allowed!\n', 403, {'Content-Type': 'text/plain'} + else: + if temp_ip.version == 4: + data['ip'] = ip + elif temp_ip.verison == 6: + data['ip6'] = ip try: - ip = ipaddress.ip_address(ip) - except ipaddress.AddressValueError: - return 'IP not allowed!\n', 403, {'Content-Type': 'text/plain'} + country = gi4.country_name_by_addr(ip) + except pygeoip.GeoIPError: + country = gi6.country_name_by_addr(ip) - if ip.version == 4: - data['country'] = gi4.country_name_by_addr(str(ip)) - elif ip.version == 6: - data['country'] = gi6.country_name_by_addr(str(ip)) - else: - data['country'] = None - + data['country'] = country data['last_updated'] = strict_rfc3339.timestamp_to_rfc3339_utcoffset( datetime.utcnow().timestamp() )