From 40357c20b6403e0efed74b0ccd329372101f08e4 Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Wed, 16 Oct 2019 14:11:07 +0300 Subject: [PATCH 1/6] Changes from gm-148 --- apps/tag/migrations/0004_merge_20191021_1138.py | 14 ++++++++++++++ apps/timetable/serialziers.py | 1 + project/templates/news/news_email.html | 12 ++++++------ 3 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 apps/tag/migrations/0004_merge_20191021_1138.py diff --git a/apps/tag/migrations/0004_merge_20191021_1138.py b/apps/tag/migrations/0004_merge_20191021_1138.py new file mode 100644 index 00000000..6298ce4f --- /dev/null +++ b/apps/tag/migrations/0004_merge_20191021_1138.py @@ -0,0 +1,14 @@ +# Generated by Django 2.2.4 on 2019-10-21 11:38 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('tag', '0003_auto_20191018_0758'), + ('tag', '0003_tag_priority'), + ] + + operations = [ + ] diff --git a/apps/timetable/serialziers.py b/apps/timetable/serialziers.py index 76a37257..babe33c1 100644 --- a/apps/timetable/serialziers.py +++ b/apps/timetable/serialziers.py @@ -78,6 +78,7 @@ class ScheduleCreateSerializer(ScheduleRUDSerializer): establishment.schedule.add(instance) return instance + class TimetableSerializer(serializers.ModelSerializer): """Serailzier for Timetable model.""" weekday_display = serializers.CharField(source='get_weekday_display', diff --git a/project/templates/news/news_email.html b/project/templates/news/news_email.html index 6fe14060..0227b9de 100644 --- a/project/templates/news/news_email.html +++ b/project/templates/news/news_email.html @@ -7,7 +7,7 @@ {{ title }} - +
@@ -24,19 +24,19 @@ -
+
{{ title }}
{% if not image_url is None %}
- +
{% endif %} -
+
{{ description | safe }}
- -
+ +
Go to news
From 47da9929cb5ef6c12881ac81fa9ef5a4d5ab0a2e Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Wed, 16 Oct 2019 14:11:07 +0300 Subject: [PATCH 2/6] Some fixes --- apps/search_indexes/documents/news.py | 1 - apps/tag/migrations/0004_merge_20191021_1138.py | 14 -------------- 2 files changed, 15 deletions(-) delete mode 100644 apps/tag/migrations/0004_merge_20191021_1138.py diff --git a/apps/search_indexes/documents/news.py b/apps/search_indexes/documents/news.py index 21c59e68..cd6fc089 100644 --- a/apps/search_indexes/documents/news.py +++ b/apps/search_indexes/documents/news.py @@ -24,7 +24,6 @@ class NewsDocument(Document): country = fields.ObjectField(properties={'id': fields.IntegerField(), 'code': fields.KeywordField()}) web_url = fields.KeywordField(attr='web_url') - preview_image_url = fields.TextField(attr='preview_image_url') tags = fields.ObjectField( properties={ 'id': fields.IntegerField(attr='id'), diff --git a/apps/tag/migrations/0004_merge_20191021_1138.py b/apps/tag/migrations/0004_merge_20191021_1138.py deleted file mode 100644 index 6298ce4f..00000000 --- a/apps/tag/migrations/0004_merge_20191021_1138.py +++ /dev/null @@ -1,14 +0,0 @@ -# Generated by Django 2.2.4 on 2019-10-21 11:38 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('tag', '0003_auto_20191018_0758'), - ('tag', '0003_tag_priority'), - ] - - operations = [ - ] From d5609e4cb89f724e60aae4b98d485de4e9004fc2 Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Wed, 16 Oct 2019 14:11:07 +0300 Subject: [PATCH 3/6] Works now filter first part --- .../migrations/0040_establishment_tz.py | 33 +++++++++++++++++++ apps/establishment/models.py | 9 ++--- apps/establishment/serializers/back.py | 1 + apps/location/models.py | 6 ---- apps/search_indexes/views.py | 5 ++- project/settings/base.py | 1 + requirements/base.txt | 1 + 7 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 apps/establishment/migrations/0040_establishment_tz.py diff --git a/apps/establishment/migrations/0040_establishment_tz.py b/apps/establishment/migrations/0040_establishment_tz.py new file mode 100644 index 00000000..d5a383d8 --- /dev/null +++ b/apps/establishment/migrations/0040_establishment_tz.py @@ -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), + ] diff --git a/apps/establishment/models.py b/apps/establishment/models.py index c01d8613..7de88d91 100644 --- a/apps/establishment/models.py +++ b/apps/establishment/models.py @@ -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 diff --git a/apps/establishment/serializers/back.py b/apps/establishment/serializers/back.py index 59725710..714dd62d 100644 --- a/apps/establishment/serializers/back.py +++ b/apps/establishment/serializers/back.py @@ -41,6 +41,7 @@ class EstablishmentListCreateSerializer(EstablishmentBaseSerializer): 'is_publish', 'guestonline_id', 'lastable_id', + 'tz', ] diff --git a/apps/location/models.py b/apps/location/models.py index 3591d4df..da645de6 100644 --- a/apps/location/models.py +++ b/apps/location/models.py @@ -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) diff --git a/apps/search_indexes/views.py b/apps/search_indexes/views.py index 913c2d95..25205bac 100644 --- a/apps/search_indexes/views.py +++ b/apps/search_indexes/views.py @@ -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, ] }, } diff --git a/project/settings/base.py b/project/settings/base.py index bec8fc2b..02df804e 100644 --- a/project/settings/base.py +++ b/project/settings/base.py @@ -95,6 +95,7 @@ EXTERNAL_APPS = [ 'rest_framework_simplejwt.token_blacklist', 'solo', 'phonenumber_field', + 'timezone_field', 'storages', 'sorl.thumbnail', 'timezonefinder' diff --git a/requirements/base.txt b/requirements/base.txt index dbb3b20e..c95055de 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -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 From ce5987de9528da4d891a9c4870b7c61eb632a551 Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Wed, 16 Oct 2019 14:11:07 +0300 Subject: [PATCH 4/6] Attach establishment timestamps as command --- apps/establishment/management/__init__.py | 0 .../management/commands/__init__.py | 0 .../commands/attach_establishments_tz.py | 23 +++++++++++++++++++ .../migrations/0040_establishment_tz.py | 12 ---------- 4 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 apps/establishment/management/__init__.py create mode 100644 apps/establishment/management/commands/__init__.py create mode 100644 apps/establishment/management/commands/attach_establishments_tz.py diff --git a/apps/establishment/management/__init__.py b/apps/establishment/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/establishment/management/commands/__init__.py b/apps/establishment/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/establishment/management/commands/attach_establishments_tz.py b/apps/establishment/management/commands/attach_establishments_tz.py new file mode 100644 index 00000000..58be04cb --- /dev/null +++ b/apps/establishment/management/commands/attach_establishments_tz.py @@ -0,0 +1,23 @@ +from django.core.management.base import BaseCommand +from pytz import timezone as py_tz +from timezonefinder import TimezoneFinder +from establishment.models import Establishment + + +class Command(BaseCommand): + help = 'Attach correct timestamps according to coordinates to existing establishments' + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.tf = TimezoneFinder(in_memory=True) + + def handle(self, *args, **options): + for establishment in Establishment.objects.prefetch_related('address').all(): + if establishment.address and establishment.address.latitude and establishment.address.longitude: + establishment.tz = py_tz(self.tf.certain_timezone_at(lng=establishment.address.longitude, + lat=establishment.address.latitude)) + establishment.save() + self.stdout.write(self.style.SUCCESS(f'Attached timezone {establishment.tz} to {establishment}')) + else: + self.stdout.write(self.style.WARNING(f'Establishment {establishment} has no coordinates' + f'passing...')) diff --git a/apps/establishment/migrations/0040_establishment_tz.py b/apps/establishment/migrations/0040_establishment_tz.py index d5a383d8..75f54c06 100644 --- a/apps/establishment/migrations/0040_establishment_tz.py +++ b/apps/establishment/migrations/0040_establishment_tz.py @@ -3,17 +3,6 @@ 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): @@ -29,5 +18,4 @@ class Migration(migrations.Migration): name='tz', field=timezone_field.fields.TimeZoneField(default=settings.TIME_ZONE), ), - migrations.RunPython(fill_timezones, migrations.RunPython.noop), ] From 711d536c98cf8a96473681101b3f80c44ab3b99d Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Wed, 16 Oct 2019 14:11:07 +0300 Subject: [PATCH 5/6] Update migration --- .../{0040_establishment_tz.py => 0041_establishment_tz.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename apps/establishment/migrations/{0040_establishment_tz.py => 0041_establishment_tz.py} (86%) diff --git a/apps/establishment/migrations/0040_establishment_tz.py b/apps/establishment/migrations/0041_establishment_tz.py similarity index 86% rename from apps/establishment/migrations/0040_establishment_tz.py rename to apps/establishment/migrations/0041_establishment_tz.py index 75f54c06..a06bbad9 100644 --- a/apps/establishment/migrations/0040_establishment_tz.py +++ b/apps/establishment/migrations/0041_establishment_tz.py @@ -9,7 +9,7 @@ class Migration(migrations.Migration): dependencies = [ - ('establishment', '0039_establishmentsubtype_index_name'), + ('establishment', '0040_employee_tags'), ] operations = [ From 07ac06f405f0a4473cd2a00b5d15e9d88dff60ea Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Wed, 16 Oct 2019 14:11:07 +0300 Subject: [PATCH 6/6] Update migration --- .../{0041_establishment_tz.py => 0042_establishment_tz.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename apps/establishment/migrations/{0041_establishment_tz.py => 0042_establishment_tz.py} (88%) diff --git a/apps/establishment/migrations/0041_establishment_tz.py b/apps/establishment/migrations/0042_establishment_tz.py similarity index 88% rename from apps/establishment/migrations/0041_establishment_tz.py rename to apps/establishment/migrations/0042_establishment_tz.py index a06bbad9..e804242f 100644 --- a/apps/establishment/migrations/0041_establishment_tz.py +++ b/apps/establishment/migrations/0042_establishment_tz.py @@ -9,7 +9,7 @@ class Migration(migrations.Migration): dependencies = [ - ('establishment', '0040_employee_tags'), + ('establishment', '0041_auto_20191023_0920'), ] operations = [