use ox.parse_useragent, fixed #697 and make undefined values null, fixes #699

This commit is contained in:
j 2012-03-21 14:44:43 +01:00
parent de57e52c55
commit 53441142e4

View file

@ -27,17 +27,17 @@ class SessionData(models.Model):
level = models.IntegerField(default=0) level = models.IntegerField(default=0)
timesseen = models.IntegerField(default=0) timesseen = models.IntegerField(default=0)
ip = models.CharField(default='', max_length=255) ip = models.CharField(max_length=255, null=True)
useragent = models.CharField(default='', max_length=255) useragent = models.CharField(max_length=255, null=True)
windowsize = models.CharField(default='', max_length=255) windowsize = models.CharField(max_length=255, null=True)
screensize = models.CharField(default='', max_length=255) screensize = models.CharField(max_length=255, null=True)
info = DictField(default={}) info = DictField(default={})
location = models.CharField(default='', max_length=255) location = models.CharField(max_length=255, null=True)
system = models.CharField(default='', max_length=255) system = models.CharField(max_length=255, null=True)
browser = models.CharField(default='', max_length=255) browser = models.CharField(max_length=255, null=True)
numberoflists = models.IntegerField(default=0) numberoflists = models.IntegerField(default=0, null=True)
objects = managers.SessionDataManager() objects = managers.SessionDataManager()
@ -48,42 +48,13 @@ class SessionData(models.Model):
def parse_data(self): def parse_data(self):
if self.useragent: if self.useragent:
self.browser = 'Unknown' ua = ox.parse_useragent(self.useragent)
for browser in ( self.browser= ua['browser']['string']
'Internet Explorer', self.system = ua['system']['string']
'Webkit', if not self.browser:
'Safari', self.browser = None
'MeeGo', if not self.system:
'Android', self.system = None
'Chrome',
'Firefox',
'Safari Mobile',
'Opera',
'Googlebot',
'bingbot',
):
if {
'Safari Mobile': 'Mobile/',
'Internet Explorer': 'MSIE',
'Bing': 'bingbot',
'Google': 'Googlebot',
}.get(browser, browser) in self.useragent:
self.browser = browser
for system in (
'Windows',
'Mac OS X',
'iOS',
'Linux',
'Andorid',
'MeeGo',
):
if {
}.get(system, system) in self.useragent:
self.system = system
if 'Mobile/' in self.useragent and 'Safari' in self.useragent and \
('iPhone' in self.useragent or 'iPad' in self.useragent):
self.system = 'iOS'
if self.ip: if self.ip:
try: try:
g = GeoIP() g = GeoIP()
@ -92,9 +63,9 @@ class SessionData(models.Model):
self.location = u'%s, %s' % (location['city'].decode('latin-1'), self.location = u'%s, %s' % (location['city'].decode('latin-1'),
location['country_name'].decode('latin-1')) location['country_name'].decode('latin-1'))
else: else:
self.location = '' self.location = None
except: except:
self.location = '' self.location = None
pass pass
def save(self, *args, **kwargs): def save(self, *args, **kwargs):