[geo ip] determine country using geoip; added country info in session and cookies
This commit is contained in:
parent
72a4b83cb6
commit
2078d52762
4 changed files with 26 additions and 16 deletions
BIN
data/GeoIP.dat
Normal file
BIN
data/GeoIP.dat
Normal file
Binary file not shown.
|
|
@ -259,6 +259,8 @@ class CookieManager:
|
|||
webnotes.cookies[b'sid'] = webnotes.session['sid'].encode('utf-8')
|
||||
webnotes.cookies[b'sid'][b'expires'] = expires.encode('utf-8')
|
||||
|
||||
webnotes.cookies[b'country'] = webnotes.session.get("session_country")
|
||||
|
||||
def set_remember_me(self):
|
||||
from webnotes.utils import cint
|
||||
|
||||
|
|
|
|||
|
|
@ -115,10 +115,7 @@ class Session:
|
|||
self.data['data']['session_ip'] = os.environ.get('REMOTE_ADDR')
|
||||
self.data['data']['last_updated'] = webnotes.utils.now()
|
||||
self.data['data']['session_expiry'] = self.get_expiry_period()
|
||||
|
||||
# get ipinfo
|
||||
if webnotes.conn.get_global('get_ip_info'):
|
||||
self.get_ipinfo()
|
||||
self.data['data']['session_country'] = get_geo_ip_country(os.environ.get('REMOTE_ADDR'))
|
||||
|
||||
# insert session
|
||||
webnotes.conn.begin()
|
||||
|
|
@ -131,7 +128,6 @@ class Session:
|
|||
|
||||
# set cookies to write
|
||||
webnotes.session = self.data
|
||||
webnotes.cookie_manager.set_cookies()
|
||||
|
||||
def insert_session_record(self):
|
||||
webnotes.conn.sql("""insert into tabSessions
|
||||
|
|
@ -255,15 +251,17 @@ class Session:
|
|||
exp_sec = "2:00:00"
|
||||
|
||||
return exp_sec
|
||||
|
||||
def get_geo_ip_country(ip_addr):
|
||||
try:
|
||||
import pygeoip
|
||||
except ImportError:
|
||||
return
|
||||
|
||||
import os
|
||||
from webnotes.utils import get_base_path
|
||||
|
||||
def get_ipinfo(self):
|
||||
import os
|
||||
|
||||
try:
|
||||
import pygeoip
|
||||
except:
|
||||
return
|
||||
|
||||
gi = pygeoip.GeoIP('data/GeoIP.dat')
|
||||
self.data['data']['ipinfo'] = {'countryName': gi.country_name_by_addr(os.environ.get('REMOTE_ADDR'))}
|
||||
|
||||
geo_ip_file = os.path.join(get_base_path(), "lib", "data", "GeoIP.dat")
|
||||
geo_ip = pygeoip.GeoIP(geo_ip_file, pygeoip.MEMORY_CACHE)
|
||||
|
||||
return geo_ip.country_name_by_addr(ip_addr)
|
||||
10
webnotes/tests/test_geo_ip.py
Normal file
10
webnotes/tests/test_geo_ip.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import webnotes
|
||||
import unittest
|
||||
|
||||
class TestGeoIP(unittest.TestCase):
|
||||
def test_geo_ip(self):
|
||||
from webnotes.sessions import get_geo_ip_country
|
||||
self.assertEquals(get_geo_ip_country("5.10.83.223"), "India")
|
||||
self.assertEquals(get_geo_ip_country("223.29.223.255"), "India")
|
||||
self.assertEquals(get_geo_ip_country("4.18.32.80"), "United States")
|
||||
self.assertEquals(get_geo_ip_country("217.194.147.25"), "United States")
|
||||
Loading…
Add table
Reference in a new issue