From b77c654c6ca71e215fcf9362c773fc3229794ea8 Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Fri, 18 Oct 2019 02:38:59 +0300 Subject: [PATCH] Open now filter --- apps/establishment/models.py | 13 +++++++++++++ apps/location/models.py | 9 ++++++++- apps/search_indexes/documents/establishment.py | 1 + apps/search_indexes/serializers.py | 1 + apps/search_indexes/views.py | 9 ++++++++- project/settings/base.py | 1 + requirements/base.txt | 1 + 7 files changed, 33 insertions(+), 2 deletions(-) diff --git a/apps/establishment/models.py b/apps/establishment/models.py index 5d50284c..3f61dbe9 100644 --- a/apps/establishment/models.py +++ b/apps/establishment/models.py @@ -1,4 +1,5 @@ """Establishment models.""" +from datetime import datetime from functools import reduce import elasticsearch_dsl @@ -13,6 +14,7 @@ from django.db.models import When, Case, F, ExpressionWrapper, Subquery, Q from django.utils import timezone from django.utils.translation import gettext_lazy as _ from phonenumber_field.modelfields import PhoneNumberField +from pytz import timezone as py_tz from collection.models import Collection from location.models import Address @@ -378,6 +380,17 @@ class Establishment(ProjectBaseMixin, URLImageMixin, TranslatedFieldsMixin): """ Used for indexing working by day """ return [ret.weekday for ret in self.schedule.all() if ret.works_at_afternoon] + # @property + def works_now(self): + """ Is establishment working now """ + now_at_est_tz = datetime.now(tz=py_tz(self.address.tz_name)) + current_week = now_at_est_tz.weekday() + schedule_for_today = self.schedule.filter(weekday=current_week).first() + if schedule_for_today is None: + return False + time_at_est_tz = now_at_est_tz.time() + return schedule_for_today.closed_at > time_at_est_tz > schedule_for_today.opening_at + @property def tags_indexing(self): return [{'id': tag.metadata.id, diff --git a/apps/location/models.py b/apps/location/models.py index 2298c28e..e05f5b26 100644 --- a/apps/location/models.py +++ b/apps/location/models.py @@ -5,8 +5,10 @@ from django.db.models.signals import post_save from django.db.transaction import on_commit from django.dispatch import receiver from django.utils.translation import gettext_lazy as _ -from utils.models import ProjectBaseMixin, SVGImageMixin, TranslatedFieldsMixin, TJSONField + +from timezonefinder import TimezoneFinder from translation.models import Language +from utils.models import ProjectBaseMixin, SVGImageMixin, TranslatedFieldsMixin, TJSONField class Country(TranslatedFieldsMixin, SVGImageMixin, ProjectBaseMixin): @@ -99,6 +101,11 @@ class Address(models.Model): def get_street_name(self): return self.street_name_1 or self.street_name_2 + @property + def tz_name(self): + tf = TimezoneFinder(in_memory=True) + return tf.certain_timezone_at(lng=self.latitude, lat=self.longitude) + @property def latitude(self): return self.coordinates.y if self.coordinates else float(0) diff --git a/apps/search_indexes/documents/establishment.py b/apps/search_indexes/documents/establishment.py index 4306eed4..dabc98bb 100644 --- a/apps/search_indexes/documents/establishment.py +++ b/apps/search_indexes/documents/establishment.py @@ -38,6 +38,7 @@ class EstablishmentDocument(Document): works_noon = fields.ListField(fields.IntegerField( attr='works_noon' )) + works_now = fields.BooleanField() tags = fields.ObjectField( properties={ 'id': fields.IntegerField(attr='id'), diff --git a/apps/search_indexes/serializers.py b/apps/search_indexes/serializers.py index 651205d7..535974a9 100644 --- a/apps/search_indexes/serializers.py +++ b/apps/search_indexes/serializers.py @@ -97,6 +97,7 @@ class EstablishmentDocumentSerializer(DocumentSerializer): 'schedule', 'works_noon', 'works_evening', + 'works_now', # 'collections', # 'establishment_type', # 'establishment_subtypes', diff --git a/apps/search_indexes/views.py b/apps/search_indexes/views.py index 0a3afe8e..913c2d95 100644 --- a/apps/search_indexes/views.py +++ b/apps/search_indexes/views.py @@ -133,9 +133,16 @@ class EstablishmentDocumentViewSet(BaseDocumentViewSet): 'works_evening': { 'field': 'works_evening', 'lookups': [ - constants.LOOKUP_QUERY_IN, + constants.LOOKUP_FILTER_TERM, ], }, + 'works_now': { + 'field': 'works_now', + 'lookups': [ + constants.LOOKUP_FILTER_EXISTS, + constants.LOOKUP_QUERY_IN, + ] + }, } geo_spatial_filter_fields = { diff --git a/project/settings/base.py b/project/settings/base.py index a64bedb9..95babea4 100644 --- a/project/settings/base.py +++ b/project/settings/base.py @@ -96,6 +96,7 @@ EXTERNAL_APPS = [ 'phonenumber_field', 'storages', 'sorl.thumbnail', + 'timezonefinder' ] diff --git a/requirements/base.txt b/requirements/base.txt index 856c70f3..dbb3b20e 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -9,6 +9,7 @@ fcm-django django-easy-select2 bootstrap-admin drf-yasg==1.16.0 +timezonefinder PySocks!=1.5.7,>=1.5.6; djangorestframework==3.9.4