-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from praekeltfoundation/feature/issue-2-Impleme…
…nt-geolocation Implement geolocation
- Loading branch information
Showing
9 changed files
with
97 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from django.contrib.gis.geoip2 import GeoIP2 | ||
from molo.globalsite.models import CountrySite | ||
|
||
|
||
def get_country_site(request): | ||
try: | ||
return CountrySite.objects.get( | ||
code=get_country_code(request)).site_url | ||
except CountrySite.DoesNotExist: | ||
return None | ||
|
||
|
||
def get_client_ip(request): | ||
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') | ||
if x_forwarded_for: | ||
return x_forwarded_for.split(',')[0] | ||
else: | ||
return request.META.get('REMOTE_ADDR') | ||
|
||
|
||
def get_country_code(request): | ||
try: | ||
return GeoIP2().country_code(get_client_ip(request)) | ||
except: | ||
return None |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
molo.core<6.0.0,>=5.21.2 | ||
geoip2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
from .base import MIDDLEWARE_CLASSES # noqa | ||
from os.path import abspath, dirname, join | ||
from os import environ | ||
|
||
MIDDLEWARE_CLASSES = MIDDLEWARE_CLASSES + [ | ||
'molo.globalsite.middleware.CountrySiteRedirectMiddleware', | ||
] | ||
|
||
# Global site settings | ||
GLOBAL_SITE_URL = environ.get('GLOBAL_SITE_URL', '') | ||
GEOIP_PATH = join(dirname(dirname(abspath(__file__))), 'geoip_db') |