add geo info to user table
This commit is contained in:
parent
c31647358e
commit
3de8bbe367
5 changed files with 65 additions and 1 deletions
3
README
3
README
|
@ -7,7 +7,8 @@ python, bazaar, pip and virtualenv and several other python modules:
|
||||||
* Packages
|
* Packages
|
||||||
apt-get install bzr git subversion mercurial \
|
apt-get install bzr git subversion mercurial \
|
||||||
python-setuptools python-pip python-virtualenv ipython \
|
python-setuptools python-pip python-virtualenv ipython \
|
||||||
python-dev python-imaging python-numpy python-psycopg2
|
python-dev python-imaging python-numpy python-psycopg2 \
|
||||||
|
python-geoip
|
||||||
|
|
||||||
* Pan.do/ra
|
* Pan.do/ra
|
||||||
Get code from bazzar
|
Get code from bazzar
|
||||||
|
|
|
@ -100,6 +100,14 @@ def update_static():
|
||||||
if not os.path.exists(image):
|
if not os.path.exists(image):
|
||||||
shutil.copyfile(pandora, image)
|
shutil.copyfile(pandora, image)
|
||||||
|
|
||||||
|
#download geo data
|
||||||
|
path = os.path.join(settings.GEOIP_PATH, 'GeoLiteCity.dat')
|
||||||
|
if not os.path.exists(path):
|
||||||
|
url = 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz'
|
||||||
|
print 'download', url
|
||||||
|
ox.net.saveUrl(url, "%s.gz"%path)
|
||||||
|
os.system('gunzip "%s.gz"' % path)
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
load_config()
|
load_config()
|
||||||
thread.start_new_thread(reloader_thread, ())
|
thread.start_new_thread(reloader_thread, ())
|
||||||
|
|
|
@ -43,6 +43,7 @@ APPEND_SLASH = False
|
||||||
# Example: "/home/media/media.lawrence.com/"
|
# Example: "/home/media/media.lawrence.com/"
|
||||||
MEDIA_ROOT = normpath(join(PROJECT_ROOT, '..', 'data'))
|
MEDIA_ROOT = normpath(join(PROJECT_ROOT, '..', 'data'))
|
||||||
STATIC_ROOT = normpath(join(PROJECT_ROOT, '..', 'static'))
|
STATIC_ROOT = normpath(join(PROJECT_ROOT, '..', 'static'))
|
||||||
|
GEOIP_PATH = normpath(join(PROJECT_ROOT, '..', 'data', 'geo'))
|
||||||
|
|
||||||
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
|
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
|
||||||
# trailing slash if there is a path component (optional in other cases).
|
# trailing slash if there is a path component (optional in other cases).
|
||||||
|
|
|
@ -7,6 +7,7 @@ from django.contrib.auth.models import User
|
||||||
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.utils import GeoIP
|
||||||
|
|
||||||
import ox
|
import ox
|
||||||
from ox.django.fields import DictField
|
from ox.django.fields import DictField
|
||||||
|
@ -31,12 +32,40 @@ class SessionData(models.Model):
|
||||||
screensize = models.CharField(default='', max_length=255)
|
screensize = models.CharField(default='', max_length=255)
|
||||||
info = DictField(default={})
|
info = DictField(default={})
|
||||||
|
|
||||||
|
location = models.CharField(default='', max_length=255)
|
||||||
|
system = models.CharField(default='', max_length=255)
|
||||||
|
browser = models.CharField(default='', max_length=255)
|
||||||
|
|
||||||
objects = managers.SessionDataManager()
|
objects = managers.SessionDataManager()
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return u"%s" % self.session_key
|
return u"%s" % self.session_key
|
||||||
|
|
||||||
|
def parse_data(self):
|
||||||
|
if self.useragent:
|
||||||
|
self.browser = 'Unknown'
|
||||||
|
for browser in ('Webkit', 'Safari', 'Chrome', 'Firefox', 'Safari Mobile', 'Opera'):
|
||||||
|
if {
|
||||||
|
'Safari Mobile': 'Mobile/',
|
||||||
|
|
||||||
|
}.get(browser, browser) in self.useragent:
|
||||||
|
self.browser = browser
|
||||||
|
for system in ('Windows', 'Mac OS X', 'Andorid', 'iOS', 'Linux'):
|
||||||
|
if {
|
||||||
|
}.get(system, system) in self.useragent:
|
||||||
|
self.system = system
|
||||||
|
if self.ip:
|
||||||
|
try:
|
||||||
|
g = GeoIP()
|
||||||
|
location = g.city(self.ip)
|
||||||
|
if location:
|
||||||
|
self.location = u'%s, %s' % (location['city'].decode('latin-1'),
|
||||||
|
location['country_name'].decode('latin-1'))
|
||||||
|
else:
|
||||||
|
self.location = ''
|
||||||
|
except:
|
||||||
|
self.location = ''
|
||||||
|
pass
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
if self.user:
|
if self.user:
|
||||||
self.username = self.user.username
|
self.username = self.user.username
|
||||||
|
@ -44,6 +73,7 @@ class SessionData(models.Model):
|
||||||
self.firstseen = self.user.date_joined
|
self.firstseen = self.user.date_joined
|
||||||
else:
|
else:
|
||||||
self.level = 0
|
self.level = 0
|
||||||
|
self.parse_data()
|
||||||
super(SessionData, self).save(*args, **kwargs)
|
super(SessionData, self).save(*args, **kwargs)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -75,6 +105,7 @@ class SessionData(models.Model):
|
||||||
|
|
||||||
def json(self, keys=None, user=None):
|
def json(self, keys=None, user=None):
|
||||||
j = {
|
j = {
|
||||||
|
'browser': self.browser,
|
||||||
'disabled': False,
|
'disabled': False,
|
||||||
'email': '',
|
'email': '',
|
||||||
'firstseen': self.firstseen,
|
'firstseen': self.firstseen,
|
||||||
|
@ -82,9 +113,11 @@ class SessionData(models.Model):
|
||||||
'id': self.get_id(),
|
'id': self.get_id(),
|
||||||
'lastseen': self.lastseen,
|
'lastseen': self.lastseen,
|
||||||
'level': 'guest',
|
'level': 'guest',
|
||||||
|
'location': self.location,
|
||||||
'notes': '',
|
'notes': '',
|
||||||
'numberoflists': 0,
|
'numberoflists': 0,
|
||||||
'screensize': self.screensize,
|
'screensize': self.screensize,
|
||||||
|
'system': self.system,
|
||||||
'timesseen': self.timesseen,
|
'timesseen': self.timesseen,
|
||||||
'username': self.username or '',
|
'username': self.username or '',
|
||||||
'useragent': self.useragent,
|
'useragent': self.useragent,
|
||||||
|
|
|
@ -189,6 +189,27 @@ pandora.ui.usersDialog = function() {
|
||||||
visible: true,
|
visible: true,
|
||||||
width: 90
|
width: 90
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: 'location',
|
||||||
|
operator: '+',
|
||||||
|
title: 'Location',
|
||||||
|
visible: true,
|
||||||
|
width: 120
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'browser',
|
||||||
|
operator: '+',
|
||||||
|
title: 'Browser',
|
||||||
|
visible: true,
|
||||||
|
width: 60
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'system',
|
||||||
|
operator: '+',
|
||||||
|
title: 'System',
|
||||||
|
visible: true,
|
||||||
|
width: 60
|
||||||
|
},
|
||||||
{
|
{
|
||||||
id: 'useragent',
|
id: 'useragent',
|
||||||
operator: '+',
|
operator: '+',
|
||||||
|
|
Loading…
Reference in a new issue