Works now filter first part

This commit is contained in:
Kuroshini 2019-10-16 14:11:07 +03:00
parent 47da9929cb
commit d5609e4cb8
7 changed files with 43 additions and 13 deletions

View File

@ -0,0 +1,33 @@
# Generated by Django 2.2.4 on 2019-10-21 17:33
import timezone_field.fields
from django.db import migrations
from django.conf import settings
from timezonefinder import TimezoneFinder
from establishment.models import Establishment
def fill_timezones(apps, schema_editor):
tf = TimezoneFinder(in_memory=True)
for establishment in Establishment.objects.prefetch_related('address').all():
if establishment.address and establishment.address.latitude and establishment.address.longitude:
establishment.tz = tf.certain_timezone_at(lng=establishment.address.longitude,
lat=establishment.address.latitude)
establishment.save()
class Migration(migrations.Migration):
dependencies = [
('establishment', '0039_establishmentsubtype_index_name'),
]
operations = [
migrations.AddField(
model_name='establishment',
name='tz',
field=timezone_field.fields.TimeZoneField(default=settings.TIME_ZONE),
),
migrations.RunPython(fill_timezones, migrations.RunPython.noop),
]

View File

@ -14,7 +14,6 @@ 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
@ -22,6 +21,7 @@ from main.models import Award
from review.models import Review
from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin,
TranslatedFieldsMixin, BaseAttributes)
from timezone_field import TimeZoneField
# todo: establishment type&subtypes check
@ -354,6 +354,7 @@ class Establishment(ProjectBaseMixin, URLImageMixin, TranslatedFieldsMixin):
blank=True, null=True, default=None)
slug = models.SlugField(unique=True, max_length=255, null=True,
verbose_name=_('Establishment slug'))
tz = TimeZoneField(default=settings.TIME_ZONE)
awards = generic.GenericRelation(to='main.Award', related_query_name='establishment')
tags = models.ManyToManyField('tag.Tag', related_name='establishments',
@ -429,13 +430,13 @@ 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
@property
def works_now(self):
""" Is establishment working now """
now_at_est_tz = datetime.now(tz=py_tz(self.address.tz_name))
now_at_est_tz = datetime.now(tz=self.tz)
current_week = now_at_est_tz.weekday()
schedule_for_today = self.schedule.filter(weekday=current_week).first()
if schedule_for_today is None:
if schedule_for_today is None or schedule_for_today.closed_at is None or schedule_for_today.opening_at 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

View File

@ -41,6 +41,7 @@ class EstablishmentListCreateSerializer(EstablishmentBaseSerializer):
'is_publish',
'guestonline_id',
'lastable_id',
'tz',
]

View File

@ -6,7 +6,6 @@ from django.db.transaction import on_commit
from django.dispatch import receiver
from django.utils.translation import gettext_lazy as _
from timezonefinder import TimezoneFinder
from translation.models import Language
from utils.models import ProjectBaseMixin, SVGImageMixin, TranslatedFieldsMixin, TJSONField
@ -114,11 +113,6 @@ 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)

View File

@ -133,14 +133,13 @@ class EstablishmentDocumentViewSet(BaseDocumentViewSet):
'works_evening': {
'field': 'works_evening',
'lookups': [
constants.LOOKUP_FILTER_TERM,
constants.LOOKUP_QUERY_IN,
],
},
'works_now': {
'field': 'works_now',
'lookups': [
constants.LOOKUP_FILTER_EXISTS,
constants.LOOKUP_QUERY_IN,
constants.LOOKUP_FILTER_TERM,
]
},
}

View File

@ -95,6 +95,7 @@ EXTERNAL_APPS = [
'rest_framework_simplejwt.token_blacklist',
'solo',
'phonenumber_field',
'timezone_field',
'storages',
'sorl.thumbnail',
'timezonefinder'

View File

@ -18,6 +18,7 @@ django-filter==2.1.0
djangorestframework-xml
geoip2==2.9.0
django-phonenumber-field[phonenumbers]==2.1.0
django-timezone-field==3.1
# auth socials
django-rest-framework-social-oauth2==1.1.0