refactor geo lookup

This commit is contained in:
j 2017-11-14 17:06:02 +01:00
parent 96bd3d6df8
commit 5ce9d605f7
2 changed files with 39 additions and 25 deletions

View file

@ -9,7 +9,6 @@ from django.contrib.auth.models import User, Group
from django.db import models from django.db import models
from django.db.models import Max from django.db.models import Max
from django.conf import settings from django.conf import settings
from django.contrib.gis.geoip2 import GeoIP2
from django.utils.encoding import python_2_unicode_compatible from django.utils.encoding import python_2_unicode_compatible
import ox import ox
@ -23,6 +22,7 @@ import documentcollection.models
from . import managers from . import managers
from . import tasks from . import tasks
from .utils import get_ip, get_location
@python_2_unicode_compatible @python_2_unicode_compatible
@ -71,24 +71,14 @@ class SessionData(models.Model):
def parse_data(self): def parse_data(self):
self.parse_useragent() self.parse_useragent()
if self.ip: if self.ip:
try: city, country = get_location(self.ip)
g = GeoIP2() if city:
location = g.city(self.ip) location = u'%s, %s' % (city, country)
if location: location_sort = u'%s, %s' % (country, city)
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: else:
self.location_sort = self.location = country location = location_sort = country
else: self.location = location
self.location_sort = self.location = None self.location_sort = location_sort
except:
self.location_sort = self.location = None
pass
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if self.user: if self.user:
@ -117,12 +107,7 @@ class SessionData(models.Model):
data, created = cls.objects.get_or_create(session_key=session_key) data, created = cls.objects.get_or_create(session_key=session_key)
if request.user.is_authenticated(): if request.user.is_authenticated():
data.user = request.user data.user = request.user
if 'HTTP_X_FORWARDED_FOR' in request.META: data.ip = get_ip(request)
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.useragent = request.META.get('HTTP_USER_AGENT', '')[:4096] data.useragent = request.META.get('HTTP_USER_AGENT', '')[:4096]
info = json.loads(request.POST.get('data', '{}')) info = json.loads(request.POST.get('data', '{}'))
if info and isinstance(info, dict): if info and isinstance(info, dict):

29
pandora/user/utils.py Normal file
View file

@ -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