From f95a082db395b2668671c49e74d28542c4995890 Mon Sep 17 00:00:00 2001 From: evgeniy-st Date: Fri, 22 Nov 2019 16:00:06 +0300 Subject: [PATCH] update geoip feature --- apps/establishment/views/web.py | 3 +- apps/main/methods.py | 52 +++++++++++++-------------------- apps/main/views/common.py | 5 ++-- apps/main/views/web.py | 3 +- load_geiopdb.sh | 23 --------------- project/settings/base.py | 1 - 6 files changed, 25 insertions(+), 62 deletions(-) delete mode 100755 load_geiopdb.sh diff --git a/apps/establishment/views/web.py b/apps/establishment/views/web.py index 4f1fe07c..0b6f1ba0 100644 --- a/apps/establishment/views/web.py +++ b/apps/establishment/views/web.py @@ -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) diff --git a/apps/main/methods.py b/apps/main/methods.py index d5f307eb..f19d595a 100644 --- a/apps/main/methods.py +++ b/apps/main/methods.py @@ -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( diff --git a/apps/main/views/common.py b/apps/main/views/common.py index 18ee0d8d..674d045e 100644 --- a/apps/main/views/common.py +++ b/apps/main/views/common.py @@ -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: diff --git a/apps/main/views/web.py b/apps/main/views/web.py index e1dc32ef..3a634457 100644 --- a/apps/main/views/web.py +++ b/apps/main/views/web.py @@ -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}) diff --git a/load_geiopdb.sh b/load_geiopdb.sh deleted file mode 100755 index 48d16af1..00000000 --- a/load_geiopdb.sh +++ /dev/null @@ -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 . {} \+ diff --git a/project/settings/base.py b/project/settings/base.py index 2e29c92d..87f85065 100644 --- a/project/settings/base.py +++ b/project/settings/base.py @@ -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/