update geoip feature

This commit is contained in:
evgeniy-st 2019-11-22 16:00:06 +03:00
parent 429825804a
commit f95a082db3
6 changed files with 25 additions and 62 deletions

View File

@ -56,12 +56,11 @@ class EstablishmentRecentReviewListView(EstablishmentListView):
def get_queryset(self):
"""Overridden method 'get_queryset'."""
qs = super().get_queryset()
user_ip = methods.get_user_ip(self.request)
query_params = self.request.query_params
if 'longitude' in query_params and 'latitude' in query_params:
longitude, latitude = query_params.get('longitude'), query_params.get('latitude')
else:
longitude, latitude = methods.determine_coordinates(user_ip)
longitude, latitude = methods.determine_coordinates(self.request)
if not longitude or not latitude:
return qs.none()
point = Point(x=float(longitude), y=float(latitude), srid=settings.GEO_DEFAULT_SRID)

View File

@ -28,31 +28,25 @@ def get_user_ip(request):
return ip
def determine_country_code(ip_addr):
def determine_country_code(request):
"""Determine country code."""
country_code = None
if ip_addr:
try:
geoip = GeoIP2()
country_code = geoip.country_code(ip_addr)
country_code = country_code.lower()
except GeoIP2Exception as ex:
logger.info(f'GEOIP Exception: {ex}. ip: {ip_addr}')
except Exception as ex:
logger.error(f'GEOIP Base exception: {ex}')
return country_code
META = request.META
country_code = META.get('X-GeoIP-Country-Code',
META.get('HTTP_X_GEOIP_COUNTRY_CODE'))
if isinstance(country_code, str):
return country_code.lower()
def determine_coordinates(ip_addr: str) -> Tuple[Optional[float], Optional[float]]:
if ip_addr:
try:
geoip = GeoIP2()
return geoip.coords(ip_addr)
except GeoIP2Exception as ex:
logger.warning(f'GEOIP Exception: {ex}. ip: {ip_addr}')
except Exception as ex:
logger.warning(f'GEOIP Base exception: {ex}')
return None, None
def determine_coordinates(request):
META = request.META
longitude = META.get('X-GeoIP-Longitude',
META.get('HTTP_X_GEOIP_LONGITUDE'))
latitude = META.get('X-GeoIP-Latitude',
META.get('HTTP_X_GEOIP_LATITUDE'))
try:
return float(longitude), float(latitude)
except (TypeError, ValueError):
return None, None
def determine_user_site_url(country_code):
@ -76,15 +70,11 @@ def determine_user_site_url(country_code):
return site.site_url
def determine_user_city(ip_addr: str) -> Optional[City]:
try:
geoip = GeoIP2()
return geoip.city(ip_addr)
except GeoIP2Exception as ex:
logger.warning(f'GEOIP Exception: {ex}. ip: {ip_addr}')
except Exception as ex:
logger.warning(f'GEOIP Base exception: {ex}')
return None
def determine_user_city(request):
META = request.META
city = META.get('X-GeoIP-City',
META.get('HTTP_X_GEOIP_CITY'))
return city
def determine_subdivision(

View File

@ -86,9 +86,8 @@ class DetermineLocation(generics.GenericAPIView):
serializer_class = EmptySerializer
def get(self, request, *args, **kwargs):
user_ip = methods.get_user_ip(request)
longitude, latitude = methods.determine_coordinates(user_ip)
city = methods.determine_user_city(user_ip)
longitude, latitude = methods.determine_coordinates(request)
city = methods.determine_user_city(request)
if longitude and latitude and city:
return Response(data={'latitude': latitude, 'longitude': longitude, 'city': city})
else:

View File

@ -14,8 +14,7 @@ class DetermineSiteView(generics.GenericAPIView):
serializer_class = EmptySerializer
def get(self, request, *args, **kwargs):
user_ip = methods.get_user_ip(request)
country_code = methods.determine_country_code(user_ip)
country_code = methods.determine_country_code(request)
url = methods.determine_user_site_url(country_code)
return Response(data={'url': url})

View File

@ -1,23 +0,0 @@
#!/bin/bash
DB_CITY_URL="https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz"
DB_COUNTRY_URL="https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz"
DIR_PATH="geoip_db"
ARCH_PATH="archive"
mkdir -p $DIR_PATH
cd $DIR_PATH
mkdir -p $ARCH_PATH
find . -not -path "./$ARCH_PATH/*" -type f -name "*.mmdb" -exec mv -t "./$ARCH_PATH/" {} \+
filename=$(basename $DB_CITY_URL)
wget -O $filename $DB_CITY_URL
tar xzvf "$filename"
filename=$(basename $DB_COUNTRY_URL)
wget -O $filename $DB_COUNTRY_URL
tar xzvf "$filename"
find . -mindepth 1 -type f -name "*.mmdb" -not -path "./$ARCH_PATH/*" -exec mv -t . {} \+

View File

@ -487,7 +487,6 @@ LIMITING_QUERY_OBJECTS = QUERY_OUTPUT_OBJECTS * 3
# GEO
# A Spatial Reference System Identifier
GEO_DEFAULT_SRID = 4326
GEOIP_PATH = os.path.join(PROJECT_ROOT, 'geoip_db')
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/