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):
|
||||
"""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)
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
ratings = generic.GenericRelation(Rating)
|
||||
favorites = generic.GenericRelation(to='favorites.Favorites')
|
||||
carousels = generic.GenericRelation(to='main.Carousel')
|
||||
agenda = models.ForeignKey('news.Agenda', blank=True, null=True,
|
||||
on_delete=models.SET_NULL,
|
||||
verbose_name=_('agenda'))
|
||||
|
|
|
|||
|
|
@ -13,4 +13,4 @@ urlpatterns = [
|
|||
name='gallery-list'),
|
||||
path('<int:pk>/gallery/<int:image_id>/', views.NewsBackOfficeGalleryCreateDestroyView.as_view(),
|
||||
name='gallery-create-destroy'),
|
||||
]
|
||||
]
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ class JWTGenericViewMixin:
|
|||
value=cookie.value,
|
||||
secure=cookie.secure,
|
||||
httponly=cookie.http_only,
|
||||
max_age=cookie.max_age,)
|
||||
max_age=cookie.max_age, )
|
||||
return response
|
||||
|
||||
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)
|
||||
|
||||
|
||||
class FavoritesCreateDestroyMixinView(generics.CreateAPIView,
|
||||
generics.DestroyAPIView):
|
||||
"""Favorites Create Destroy mixin."""
|
||||
class BaseCreateDestroyMixinView(generics.CreateAPIView, generics.DestroyAPIView):
|
||||
"""Base Create Destroy mixin."""
|
||||
|
||||
_model = None
|
||||
serializer_class = None
|
||||
|
|
@ -137,16 +136,6 @@ class FavoritesCreateDestroyMixinView(generics.CreateAPIView,
|
|||
def get_base_object(self):
|
||||
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):
|
||||
es_update(self.get_base_object())
|
||||
|
||||
|
|
@ -159,6 +148,34 @@ class FavoritesCreateDestroyMixinView(generics.CreateAPIView,
|
|||
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
|
||||
class BindObjectMixin:
|
||||
"""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
|
||||
# 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/
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user