From 5ce9d605f7c8e86f1d58cd28634638b30166146b Mon Sep 17 00:00:00 2001 From: j Date: Tue, 14 Nov 2017 17:06:02 +0100 Subject: [PATCH] refactor geo lookup --- pandora/user/models.py | 35 ++++++++++------------------------- pandora/user/utils.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 25 deletions(-) create mode 100644 pandora/user/utils.py diff --git a/pandora/user/models.py b/pandora/user/models.py index 1d86b8080..a62ffbd5a 100644 --- a/pandora/user/models.py +++ b/pandora/user/models.py @@ -9,7 +9,6 @@ from django.contrib.auth.models import User, Group from django.db import models from django.db.models import Max from django.conf import settings -from django.contrib.gis.geoip2 import GeoIP2 from django.utils.encoding import python_2_unicode_compatible import ox @@ -23,6 +22,7 @@ import documentcollection.models from . import managers from . import tasks +from .utils import get_ip, get_location @python_2_unicode_compatible @@ -71,24 +71,14 @@ class SessionData(models.Model): def parse_data(self): self.parse_useragent() if self.ip: - try: - g = GeoIP2() - location = g.city(self.ip) - if location: - country = ox.get_country_name(location['country_code']) - if location['city']: - city = location['city'] - if isinstance(city, bytes): - city = city.decode('latin-1') - self.location = u'%s, %s' % (city, country) - self.location_sort = u'%s, %s' % (country, city) - else: - self.location_sort = self.location = country - else: - self.location_sort = self.location = None - except: - self.location_sort = self.location = None - pass + city, country = get_location(self.ip) + if city: + location = u'%s, %s' % (city, country) + location_sort = u'%s, %s' % (country, city) + else: + location = location_sort = country + self.location = location + self.location_sort = location_sort def save(self, *args, **kwargs): if self.user: @@ -117,12 +107,7 @@ class SessionData(models.Model): data, created = cls.objects.get_or_create(session_key=session_key) if request.user.is_authenticated(): data.user = request.user - if 'HTTP_X_FORWARDED_FOR' in request.META: - data.ip = request.META['HTTP_X_FORWARDED_FOR'].split(',')[0] - else: - data.ip = request.META['REMOTE_ADDR'] - if data.ip.startswith('::ffff:'): - data.ip = data.ip[len('::ffff:'):] + data.ip = get_ip(request) data.useragent = request.META.get('HTTP_USER_AGENT', '')[:4096] info = json.loads(request.POST.get('data', '{}')) if info and isinstance(info, dict): diff --git a/pandora/user/utils.py b/pandora/user/utils.py new file mode 100644 index 000000000..d32d0536c --- /dev/null +++ b/pandora/user/utils.py @@ -0,0 +1,29 @@ +from django.contrib.gis.geoip2 import GeoIP2 + +import ox + + +def get_ip(request): + if 'HTTP_X_FORWARDED_FOR' in request.META: + ip = request.META['HTTP_X_FORWARDED_FOR'].split(',')[0] + else: + ip = request.META['REMOTE_ADDR'] + if ip.startswith('::ffff:'): + ip = ip[len('::ffff:'):] + return ip + + +def get_location(ip): + country = city = None + try: + g = GeoIP2() + location = g.city(ip) + if location: + country = ox.get_country_name(location['country_code']) + if location['city']: + city = location['city'] + if isinstance(city, bytes): + city = city.decode('latin-1') + except: + country = city = None + return city, country