From dc0f484ff79047648076ea57f9f7f18f2afbf9f7 Mon Sep 17 00:00:00 2001 From: Ben Firshman Date: Thu, 18 Jul 2019 11:18:46 +0100 Subject: [PATCH 1/2] Use UTC for email sending tasks This used to be indeterminite depending on what timezone you were in, because some tasks were in UTC and some were in local time. Email sending should use location timezone, but this will fix problem for now. See #587 --- modernomad/core/tasks.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/modernomad/core/tasks.py b/modernomad/core/tasks.py index cc9b9469..1e4f0ff4 100644 --- a/modernomad/core/tasks.py +++ b/modernomad/core/tasks.py @@ -4,7 +4,6 @@ from modernomad.log import catch_exceptions from django.contrib.sites.models import Site import datetime -from django.utils import timezone import requests import json from django.core import urlresolvers @@ -39,7 +38,7 @@ def send_guest_welcome(): did_send_email = False for location in locations: - soon = datetime.datetime.today() + datetime.timedelta(days=location.welcome_email_days_ahead) + soon = datetime.date.today() + datetime.timedelta(days=location.welcome_email_days_ahead) upcoming = Use.objects.filter(location=location).filter(arrive=soon).filter(status='confirmed') for booking in upcoming: guest_welcome(booking) @@ -58,7 +57,7 @@ def send_departure_email(): # get all bookings departing today locations = Location.objects.all() for location in locations: - today = timezone.localtime(timezone.now()) + today = datetime.date.today() departing = Use.objects.filter(location=location).filter(depart=today).filter(status='confirmed') for use in departing: goodbye_email(use) @@ -70,7 +69,7 @@ def send_departure_email(): @catch_exceptions def generate_subscription_bills(): logger.info("Running task: generate_subscription_bills") - today = timezone.localtime(timezone.now()).date() + today = datetime.date.today() # using the exclude is an easier way to filter for subscriptions with an # end date of None *or* in the future. locations = Location.objects.all() @@ -119,7 +118,7 @@ def slack_embassysf_daily(): logger.info("Running task: slack_embassysf_daily") webhook = "https://hooks.slack.com/services/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" - today = timezone.localtime(timezone.now()) + today = datetime.date.today() location = Location.objects.get(slug="embassysf") arriving_today = Use.objects.filter(location=location).filter(arrive=today).filter(status='confirmed') departing_today = Use.objects.filter(location=location).filter(depart=today).filter(status='confirmed') From 3fb939c0552328974cf28d7996643368ece0902e Mon Sep 17 00:00:00 2001 From: Ben Firshman Date: Thu, 18 Jul 2019 11:20:26 +0100 Subject: [PATCH 2/2] Freeze time in email sending tests This will stop these tests flaking. --- modernomad/core/tests/test_emails.py | 13 +++++++------ requirements.txt | 1 + 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/modernomad/core/tests/test_emails.py b/modernomad/core/tests/test_emails.py index 9363c2be..3b9745db 100644 --- a/modernomad/core/tests/test_emails.py +++ b/modernomad/core/tests/test_emails.py @@ -5,10 +5,12 @@ from modernomad.core.models import Payment, Use, Booking, UserProfile, LocationEmailTemplate from modernomad.core.emails.messages import new_booking_notify, send_booking_receipt, updated_booking_notify, admin_daily_update from modernomad.core.tasks import send_departure_email, send_guest_welcome, guests_residents_daily_update -from django.utils import timezone from datetime import datetime, timedelta, date +from freezegun import freeze_time +TODAY = date(2019, 3, 14) +@freeze_time(TODAY) class EmailsTestCase(TestCase): def create_booking(self, user, rate=100, status=Use.PENDING, arrive=date(4016, 1, 13), depart=date(4016, 1, 23)): @@ -44,14 +46,13 @@ class MockResponse(): self.admin = self.create_user('admin1', admin=True, email='admin1@bob.com') self.booking = self.create_booking(user=self.guest1) - today = timezone.localtime(timezone.now()) - yesterday = today + timedelta(days=-1) - in_two_days = today + timedelta(days=2) + yesterday = TODAY + timedelta(days=-1) + in_two_days = TODAY + timedelta(days=2) after_that = in_two_days + timedelta(days=1) - self.departing_today = self.create_booking(arrive=yesterday, depart=today, user=self.guest1, status="confirmed") + self.departing_today = self.create_booking(arrive=yesterday, depart=TODAY, user=self.guest1, status="confirmed") self.arriving_in_two_days = self.create_booking(arrive=in_two_days, depart=after_that, user=self.guest2, status="confirmed") - self.arriving_today = self.create_booking(arrive=today, depart=after_that, user=self.guest3, status="confirmed") + self.arriving_today = self.create_booking(arrive=TODAY, depart=after_that, user=self.guest3, status="confirmed") def tearDown(self): self.mock_mailgun_send.stop() diff --git a/requirements.txt b/requirements.txt index 0dd8496c..85d0cf1a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,6 +11,7 @@ django-uuidfield==0.5.0 django-webpack-loader==0.6.0 Django==1.11.20 djangorestframework==3.6.4 +freezegun==0.3.12 graphene-django==2.2.0 graphene==2.1.6 graphql-core==2.1.0