From eb0f8bda09dbec3ffd22868e7ee0480e2467e966 Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Fri, 12 Jul 2013 09:45:42 -0400 Subject: [PATCH] Ensure the last_reminder datatetime is tz-aware I don't fully understand the need for this, but I have seen some tracebacks on app that complain: File "/home/humbug/humbug-deployments/2013-07-11-19-28-10/zephyr/lib/actions.py", line 1289, in handle_missedmessage_emails timestamp - user_profile.last_reminder < waitperiod): TypeError: can't subtract offset-naive and offset-aware datetimes Since timestamp in this case comes from timestamp_to_datetime that explicitly sets the tzinfo, we know it's tz-aware. The only other possibility is that user_profile.last_reminder is **not** tz-aware, though I am not sure why that would be the case. (imported from commit 67e33f4510e91fa9de504f0c610515581312c98b) --- zephyr/lib/actions.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/zephyr/lib/actions.py b/zephyr/lib/actions.py index 63a2089be3..7abf1dcb01 100644 --- a/zephyr/lib/actions.py +++ b/zephyr/lib/actions.py @@ -15,6 +15,7 @@ from django.core.exceptions import ValidationError from django.utils.importlib import import_module from django.template import loader from django.core.mail import EmailMultiAlternatives +from django.utils.timezone import utc, is_naive from confirmation.models import Confirmation @@ -1284,6 +1285,12 @@ def handle_missedmessage_emails(user_profile_id, missed_email_events): message__id__in=message_ids, flags=~UserMessage.flags.read)] + last_reminder = user_profile.last_reminder + if last_reminder is not None and is_naive(last_reminder): + logging.warning("Loaded a user_profile.last_reminder for user %s that's not tz-aware: %s" + % (user_profile.user.email, last_reminder)) + last_reminder = last_reminder.replace(tzinfo=utc) + waitperiod = datetime.timedelta(hours=UserProfile.EMAIL_REMINDER_WAITPERIOD) if len(messages) == 0 or (user_profile.last_reminder and \ timestamp - user_profile.last_reminder < waitperiod):