Merge branch 'develop' into feature/crop-params
This commit is contained in:
commit
99f01464b7
|
|
@ -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})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -196,6 +196,7 @@ class News(GalleryModelMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixi
|
||||||
views_count = models.OneToOneField('rating.ViewCount', blank=True, null=True, on_delete=models.SET_NULL)
|
views_count = models.OneToOneField('rating.ViewCount', blank=True, null=True, on_delete=models.SET_NULL)
|
||||||
ratings = generic.GenericRelation(Rating)
|
ratings = generic.GenericRelation(Rating)
|
||||||
favorites = generic.GenericRelation(to='favorites.Favorites')
|
favorites = generic.GenericRelation(to='favorites.Favorites')
|
||||||
|
carousels = generic.GenericRelation(to='main.Carousel')
|
||||||
agenda = models.ForeignKey('news.Agenda', blank=True, null=True,
|
agenda = models.ForeignKey('news.Agenda', blank=True, null=True,
|
||||||
on_delete=models.SET_NULL,
|
on_delete=models.SET_NULL,
|
||||||
verbose_name=_('agenda'))
|
verbose_name=_('agenda'))
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@ class JWTGenericViewMixin:
|
||||||
value=cookie.value,
|
value=cookie.value,
|
||||||
secure=cookie.secure,
|
secure=cookie.secure,
|
||||||
httponly=cookie.http_only,
|
httponly=cookie.http_only,
|
||||||
max_age=cookie.max_age,)
|
max_age=cookie.max_age, )
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def _get_tokens_from_cookies(self, request, cookies: dict = None):
|
def _get_tokens_from_cookies(self, request, cookies: dict = None):
|
||||||
|
|
@ -126,9 +126,8 @@ class CreateDestroyGalleryViewMixin(generics.CreateAPIView,
|
||||||
return Response(status=status.HTTP_204_NO_CONTENT)
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||||
|
|
||||||
|
|
||||||
class FavoritesCreateDestroyMixinView(generics.CreateAPIView,
|
class BaseCreateDestroyMixinView(generics.CreateAPIView, generics.DestroyAPIView):
|
||||||
generics.DestroyAPIView):
|
"""Base Create Destroy mixin."""
|
||||||
"""Favorites Create Destroy mixin."""
|
|
||||||
|
|
||||||
_model = None
|
_model = None
|
||||||
serializer_class = None
|
serializer_class = None
|
||||||
|
|
@ -137,16 +136,6 @@ class FavoritesCreateDestroyMixinView(generics.CreateAPIView,
|
||||||
def get_base_object(self):
|
def get_base_object(self):
|
||||||
return get_object_or_404(self._model, slug=self.kwargs['slug'])
|
return get_object_or_404(self._model, slug=self.kwargs['slug'])
|
||||||
|
|
||||||
def get_object(self):
|
|
||||||
"""
|
|
||||||
Returns the object the view is displaying.
|
|
||||||
"""
|
|
||||||
obj = self.get_base_object()
|
|
||||||
favorites = get_object_or_404(obj.favorites.filter(user=self.request.user))
|
|
||||||
# May raise a permission denied
|
|
||||||
self.check_object_permissions(self.request, favorites)
|
|
||||||
return favorites
|
|
||||||
|
|
||||||
def es_update_base_object(self):
|
def es_update_base_object(self):
|
||||||
es_update(self.get_base_object())
|
es_update(self.get_base_object())
|
||||||
|
|
||||||
|
|
@ -159,6 +148,34 @@ class FavoritesCreateDestroyMixinView(generics.CreateAPIView,
|
||||||
self.es_update_base_object()
|
self.es_update_base_object()
|
||||||
|
|
||||||
|
|
||||||
|
class FavoritesCreateDestroyMixinView(BaseCreateDestroyMixinView):
|
||||||
|
"""Favorites Create Destroy mixin."""
|
||||||
|
|
||||||
|
def get_object(self):
|
||||||
|
"""
|
||||||
|
Returns the object the view is displaying.
|
||||||
|
"""
|
||||||
|
obj = self.get_base_object()
|
||||||
|
favorites = get_object_or_404(obj.favorites.filter(user=self.request.user))
|
||||||
|
# May raise a permission denied
|
||||||
|
self.check_object_permissions(self.request, favorites)
|
||||||
|
return favorites
|
||||||
|
|
||||||
|
|
||||||
|
class CarouselCreateDestroyMixinView(BaseCreateDestroyMixinView):
|
||||||
|
"""Carousel Create Destroy mixin."""
|
||||||
|
|
||||||
|
def get_object(self):
|
||||||
|
"""
|
||||||
|
Returns the object the view is displaying.
|
||||||
|
"""
|
||||||
|
obj = self.get_base_object()
|
||||||
|
carousels = get_object_or_404(obj.carousels.filter(user=self.request.user))
|
||||||
|
# May raise a permission denied
|
||||||
|
self.check_object_permissions(self.request, carousels)
|
||||||
|
return carousels
|
||||||
|
|
||||||
|
|
||||||
# BackOffice user`s views & viewsets
|
# BackOffice user`s views & viewsets
|
||||||
class BindObjectMixin:
|
class BindObjectMixin:
|
||||||
"""Bind object mixin."""
|
"""Bind object mixin."""
|
||||||
|
|
|
||||||
|
|
@ -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 . {} \+
|
|
||||||
|
|
@ -494,7 +494,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