update geoip feature
This commit is contained in:
parent
429825804a
commit
f95a082db3
|
|
@ -56,12 +56,11 @@ class EstablishmentRecentReviewListView(EstablishmentListView):
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
"""Overridden method 'get_queryset'."""
|
"""Overridden method 'get_queryset'."""
|
||||||
qs = super().get_queryset()
|
qs = super().get_queryset()
|
||||||
user_ip = methods.get_user_ip(self.request)
|
|
||||||
query_params = self.request.query_params
|
query_params = self.request.query_params
|
||||||
if 'longitude' in query_params and 'latitude' in query_params:
|
if 'longitude' in query_params and 'latitude' in query_params:
|
||||||
longitude, latitude = query_params.get('longitude'), query_params.get('latitude')
|
longitude, latitude = query_params.get('longitude'), query_params.get('latitude')
|
||||||
else:
|
else:
|
||||||
longitude, latitude = methods.determine_coordinates(user_ip)
|
longitude, latitude = methods.determine_coordinates(self.request)
|
||||||
if not longitude or not latitude:
|
if not longitude or not latitude:
|
||||||
return qs.none()
|
return qs.none()
|
||||||
point = Point(x=float(longitude), y=float(latitude), srid=settings.GEO_DEFAULT_SRID)
|
point = Point(x=float(longitude), y=float(latitude), srid=settings.GEO_DEFAULT_SRID)
|
||||||
|
|
|
||||||
|
|
@ -28,30 +28,24 @@ def get_user_ip(request):
|
||||||
return ip
|
return ip
|
||||||
|
|
||||||
|
|
||||||
def determine_country_code(ip_addr):
|
def determine_country_code(request):
|
||||||
"""Determine country code."""
|
"""Determine country code."""
|
||||||
country_code = None
|
META = request.META
|
||||||
if ip_addr:
|
country_code = META.get('X-GeoIP-Country-Code',
|
||||||
try:
|
META.get('HTTP_X_GEOIP_COUNTRY_CODE'))
|
||||||
geoip = GeoIP2()
|
if isinstance(country_code, str):
|
||||||
country_code = geoip.country_code(ip_addr)
|
return country_code.lower()
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def determine_coordinates(ip_addr: str) -> Tuple[Optional[float], Optional[float]]:
|
def determine_coordinates(request):
|
||||||
if ip_addr:
|
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:
|
try:
|
||||||
geoip = GeoIP2()
|
return float(longitude), float(latitude)
|
||||||
return geoip.coords(ip_addr)
|
except (TypeError, ValueError):
|
||||||
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
|
return None, None
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -76,15 +70,11 @@ def determine_user_site_url(country_code):
|
||||||
return site.site_url
|
return site.site_url
|
||||||
|
|
||||||
|
|
||||||
def determine_user_city(ip_addr: str) -> Optional[City]:
|
def determine_user_city(request):
|
||||||
try:
|
META = request.META
|
||||||
geoip = GeoIP2()
|
city = META.get('X-GeoIP-City',
|
||||||
return geoip.city(ip_addr)
|
META.get('HTTP_X_GEOIP_CITY'))
|
||||||
except GeoIP2Exception as ex:
|
return city
|
||||||
logger.warning(f'GEOIP Exception: {ex}. ip: {ip_addr}')
|
|
||||||
except Exception as ex:
|
|
||||||
logger.warning(f'GEOIP Base exception: {ex}')
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def determine_subdivision(
|
def determine_subdivision(
|
||||||
|
|
|
||||||
|
|
@ -86,9 +86,8 @@ class DetermineLocation(generics.GenericAPIView):
|
||||||
serializer_class = EmptySerializer
|
serializer_class = EmptySerializer
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
user_ip = methods.get_user_ip(request)
|
longitude, latitude = methods.determine_coordinates(request)
|
||||||
longitude, latitude = methods.determine_coordinates(user_ip)
|
city = methods.determine_user_city(request)
|
||||||
city = methods.determine_user_city(user_ip)
|
|
||||||
if longitude and latitude and city:
|
if longitude and latitude and city:
|
||||||
return Response(data={'latitude': latitude, 'longitude': longitude, 'city': city})
|
return Response(data={'latitude': latitude, 'longitude': longitude, 'city': city})
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,7 @@ class DetermineSiteView(generics.GenericAPIView):
|
||||||
serializer_class = EmptySerializer
|
serializer_class = EmptySerializer
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
user_ip = methods.get_user_ip(request)
|
country_code = methods.determine_country_code(request)
|
||||||
country_code = methods.determine_country_code(user_ip)
|
|
||||||
url = methods.determine_user_site_url(country_code)
|
url = methods.determine_user_site_url(country_code)
|
||||||
return Response(data={'url': url})
|
return Response(data={'url': url})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 . {} \+
|
|
||||||
|
|
@ -487,7 +487,6 @@ LIMITING_QUERY_OBJECTS = QUERY_OUTPUT_OBJECTS * 3
|
||||||
# GEO
|
# GEO
|
||||||
# A Spatial Reference System Identifier
|
# A Spatial Reference System Identifier
|
||||||
GEO_DEFAULT_SRID = 4326
|
GEO_DEFAULT_SRID = 4326
|
||||||
GEOIP_PATH = os.path.join(PROJECT_ROOT, 'geoip_db')
|
|
||||||
|
|
||||||
# Static files (CSS, JavaScript, Images)
|
# Static files (CSS, JavaScript, Images)
|
||||||
# https://docs.djangoproject.com/en/2.2/howto/static-files/
|
# https://docs.djangoproject.com/en/2.2/howto/static-files/
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user