Merge branch 'changes-from-gm-148' into 'develop'

Changes from gm 148

See merge request gm/gm-backend!63
This commit is contained in:
Sergey Melihov 2019-10-23 14:57:34 +00:00
commit f3caf16d2c
13 changed files with 61 additions and 20 deletions

View File

@ -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...'))

View File

@ -0,0 +1,21 @@
# 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
class Migration(migrations.Migration):
dependencies = [
('establishment', '0041_auto_20191023_0920'),
]
operations = [
migrations.AddField(
model_name='establishment',
name='tz',
field=timezone_field.fields.TimeZoneField(default=settings.TIME_ZONE),
),
]

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 import timezone
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from phonenumber_field.modelfields import PhoneNumberField from phonenumber_field.modelfields import PhoneNumberField
from pytz import timezone as py_tz
from collection.models import Collection from collection.models import Collection
from location.models import Address from location.models import Address
@ -22,6 +21,7 @@ from main.models import Award
from review.models import Review from review.models import Review
from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin, from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin,
TranslatedFieldsMixin, BaseAttributes) TranslatedFieldsMixin, BaseAttributes)
from timezone_field import TimeZoneField
# todo: establishment type&subtypes check # todo: establishment type&subtypes check
@ -354,6 +354,7 @@ class Establishment(ProjectBaseMixin, URLImageMixin, TranslatedFieldsMixin):
blank=True, null=True, default=None) blank=True, null=True, default=None)
slug = models.SlugField(unique=True, max_length=255, null=True, slug = models.SlugField(unique=True, max_length=255, null=True,
verbose_name=_('Establishment slug')) verbose_name=_('Establishment slug'))
tz = TimeZoneField(default=settings.TIME_ZONE)
awards = generic.GenericRelation(to='main.Award', related_query_name='establishment') awards = generic.GenericRelation(to='main.Award', related_query_name='establishment')
tags = models.ManyToManyField('tag.Tag', related_name='establishments', tags = models.ManyToManyField('tag.Tag', related_name='establishments',
@ -429,13 +430,13 @@ class Establishment(ProjectBaseMixin, URLImageMixin, TranslatedFieldsMixin):
""" Used for indexing working by day """ """ Used for indexing working by day """
return [ret.weekday for ret in self.schedule.all() if ret.works_at_afternoon] return [ret.weekday for ret in self.schedule.all() if ret.works_at_afternoon]
# @property @property
def works_now(self): def works_now(self):
""" Is establishment working now """ """ 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() current_week = now_at_est_tz.weekday()
schedule_for_today = self.schedule.filter(weekday=current_week).first() 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 return False
time_at_est_tz = now_at_est_tz.time() time_at_est_tz = now_at_est_tz.time()
return schedule_for_today.closed_at > time_at_est_tz > schedule_for_today.opening_at 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', 'is_publish',
'guestonline_id', 'guestonline_id',
'lastable_id', 'lastable_id',
'tz',
] ]

View File

@ -6,7 +6,6 @@ from django.db.transaction import on_commit
from django.dispatch import receiver from django.dispatch import receiver
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from timezonefinder import TimezoneFinder
from translation.models import Language from translation.models import Language
from utils.models import ProjectBaseMixin, SVGImageMixin, TranslatedFieldsMixin, TJSONField from utils.models import ProjectBaseMixin, SVGImageMixin, TranslatedFieldsMixin, TJSONField
@ -114,11 +113,6 @@ class Address(models.Model):
def get_street_name(self): def get_street_name(self):
return self.street_name_1 or self.street_name_2 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 @property
def latitude(self): def latitude(self):
return self.coordinates.y if self.coordinates else float(0) return self.coordinates.y if self.coordinates else float(0)

View File

@ -24,7 +24,6 @@ class NewsDocument(Document):
country = fields.ObjectField(properties={'id': fields.IntegerField(), country = fields.ObjectField(properties={'id': fields.IntegerField(),
'code': fields.KeywordField()}) 'code': fields.KeywordField()})
web_url = fields.KeywordField(attr='web_url') web_url = fields.KeywordField(attr='web_url')
preview_image_url = fields.TextField(attr='preview_image_url')
tags = fields.ObjectField( tags = fields.ObjectField(
properties={ properties={
'id': fields.IntegerField(attr='id'), 'id': fields.IntegerField(attr='id'),

View File

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

View File

@ -78,6 +78,7 @@ class ScheduleCreateSerializer(ScheduleRUDSerializer):
establishment.schedule.add(instance) establishment.schedule.add(instance)
return instance return instance
class TimetableSerializer(serializers.ModelSerializer): class TimetableSerializer(serializers.ModelSerializer):
"""Serailzier for Timetable model.""" """Serailzier for Timetable model."""
weekday_display = serializers.CharField(source='get_weekday_display', weekday_display = serializers.CharField(source='get_weekday_display',

View File

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

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700|PT+Serif&display=swap&subset=cyrillic" type="text/css"> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700|PT+Serif&display=swap&subset=cyrillic" type="text/css">
<title>{{ title }}</title> <title>{{ title }}</title>
</head> </head>
<body style="box-sizing: border-box;margin: 0;font-family: 'Open Sans', sans-serif;font-size: 0.875rem;"> <body style="box-sizing: border-box;margin: 0;font-family: &quot;Open Sans&quot;, sans-serif;font-size: 0.875rem;">
<div style="dispaly: none"> <div style="dispaly: none">
</div> </div>
@ -24,7 +24,7 @@
</div> </div>
<div class="letter__sublogo" style="font-size: 21px;line-height: 1;letter-spacing: 0;color: #bcbcbc;text-transform: uppercase;">france</div> <div class="letter__sublogo" style="font-size: 21px;line-height: 1;letter-spacing: 0;color: #bcbcbc;text-transform: uppercase;">france</div>
</div> </div>
<div class="letter__title" style="font-family:"Open-Sans",sans-serif; font-size: 1.5rem;margin: 0 0 10px;padding: 0 0 6px;border-bottom: 4px solid #ffee29;"> <div class="letter__title" style="font-family:&quot;Open-Sans&quot;,sans-serif; font-size: 1.5rem;margin: 0 0 10px;padding: 0 0 6px;border-bottom: 4px solid #ffee29;">
<span class="letter__title-txt">{{ title }}</span> <span class="letter__title-txt">{{ title }}</span>
</div> </div>
{% if not image_url is None %} {% if not image_url is None %}
@ -32,11 +32,11 @@
<img src="{{ image_url }}" alt="" class="letter__photo" style="display: block; width: 100%;height: 100%;"> <img src="{{ image_url }}" alt="" class="letter__photo" style="display: block; width: 100%;height: 100%;">
</div> </div>
{% endif %} {% endif %}
<div class="letter__text" style="margin: 0 0 30px; font-family:"Open-Sans",sans-serif; font-size: 14px; line-height: 21px;letter-spacing: -0.34px; overflow-x: hidden;"> <div class="letter__text" style="margin: 0 0 30px; font-family:&quot;Open-Sans&quot;,sans-serif; font-size: 14px; line-height: 21px;letter-spacing: -0.34px; overflow-x: hidden;">
{{ description | safe }} {{ description | safe }}
</div> </div>
<a href="https://{{ country_code }}.{{ domain_uri }}/news/{{ slug }}" style="text-decoration: none;" target="_blank"> <a href="https://{{ country_code }}.{{ domain_uri }}/news/{{ slug }}" style="text-decoration: none; display: block; text-align-center" target="_blank">
<div class="letter__button" style="display: block;margin: 30px auto;padding: 0;font-family: &quot;pt serif&quot: ;, sans-serif: ;font-size: 1.25rem;letter-spacing: 1px;text-align: center;color: #000;background: #ffee29;border: 2px solid #ffee29;text-transform: uppercase;min-width: 238px;height: 46px;background-color: #ffee29;"> <div class="letter__button" style="display: inline-block;margin: 30px auto;padding: 11px;font-size: 1.25rem;letter-spacing: 1px;text-align: center;color: #000;background: #ffee29;border: 2px solid #ffee29;text-transform: uppercase;min-width: 238px;background-color: #ffee29;">
Go to news Go to news
</div> </div>
</a> </a>

View File

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