Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use Django 1.4's timezone aware datetime.now if possible.

  • Loading branch information...
commit 0cf494986ee3c7e8f3fbf05e3bd230ee4be2d7db 1 parent 4c31494
@jezdez jezdez authored
View
10 threaded_messages/forms.py
@@ -5,9 +5,9 @@
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext_noop
from django.contrib.auth.models import User
-from models import *
-from fields import CommaSeparatedUserField
-from utils import reply_to_thread
+from .models import *
+from .fields import CommaSeparatedUserField
+from .utils import reply_to_thread, now
if sendgrid_settings.THREADED_MESSAGES_USE_SENDGRID:
from sendgrid_parse_api.utils import create_reply_email
@@ -47,7 +47,7 @@ def save(self, sender, send=True):
Participant.objects.create(thread=thread, user=recipient)
(sender_part, created) = Participant.objects.get_or_create(thread=thread, user=sender)
- sender_part.replied_at = sender_part.read_at = datetime.datetime.now()
+ sender_part.replied_at = sender_part.read_at = now()
sender_part.save()
thread.save() #save this last, since this updates the search index
@@ -75,7 +75,7 @@ class ReplyForm(forms.Form):
A simple default form for private messages.
"""
body = forms.CharField(label=_(u"Reply"),
- widget=forms.Textarea(attrs={'rows': '4', 'cols':'55'}))
+ widget=forms.Textarea(attrs={'rows': '4', 'cols': '55'}))
def save(self, sender, thread):
body = self.cleaned_data['body']
View
5 threaded_messages/models.py
@@ -8,7 +8,8 @@
from django.db.models import F, Q
from django.db.models import Avg, Max, Min, Count
-from listeners import start_listening
+from .utils import now
+from .listeners import start_listening
start_listening()
class MessageManager(models.Manager):
@@ -75,7 +76,7 @@ def __unicode__(self):
def save(self, **kwargs):
if not self.id:
- self.sent_at = datetime.datetime.now()
+ self.sent_at = now()
super(Message, self).save(**kwargs)
class Meta:
View
7 threaded_messages/utils.py
@@ -28,6 +28,11 @@
else:
from django.core.mail import send_mail
+try:
+ from django.utils.timezone import now
+except ImportError:
+ now = datetime.datetime.now
+
def open_message_thread(recipients, subject, template,
sender, context={}, send=True, message=None):
@@ -67,7 +72,7 @@ def reply_to_thread(thread,sender, body):
recipients.append(participant.user)
sender_part = Participant.objects.get(thread=thread, user=sender)
- sender_part.replied_at = sender_part.read_at = datetime.datetime.now()
+ sender_part.replied_at = sender_part.read_at = now()
sender_part.save()
if notification:
View
18 threaded_messages/views.py
@@ -1,5 +1,4 @@
# -*- coding:utf-8 -*-
-import datetime
import logging
import simplejson
@@ -21,6 +20,7 @@
from .models import *
from .forms import ComposeForm, ReplyForm
+from .utils import now
@login_required
@@ -144,7 +144,7 @@ def delete(request, thread_id, success_url=None):
page (e.g. `/foo/bar/`) than ``success_url`` after deletion of the message.
"""
user = request.user
- now = datetime.datetime.now()
+ right_now = now()
thread = get_object_or_404(Thread, id=thread_id)
user_part = get_object_or_404(Participant, user=user, thread=thread)
@@ -152,8 +152,8 @@ def delete(request, thread_id, success_url=None):
success_url = request.GET['next']
elif success_url is None:
success_url = reverse('messages_inbox')
-
- user_part.deleted_at = now
+
+ user_part.deleted_at = right_now
user_part.save()
messages.success(request, message=_(u"Conversation successfully deleted."))
return HttpResponseRedirect(success_url)
@@ -174,7 +174,7 @@ def undelete(request, thread_id, success_url=None):
elif success_url is None:
success_url = reverse('messages_inbox')
- user_part.deleted_at = now
+ user_part.deleted_at = now()
user_part.save()
messages.success(request, _(u"Conversation successfully recovered."))
return HttpResponseRedirect(success_url)
@@ -207,7 +207,7 @@ def view(request, thread_id, form_class=ReplyForm,
else:
form = form_class()
- now = datetime.datetime.now()
+ right_now = now()
participant = get_object_or_404(Participant, thread=thread, user=request.user)
message_list = []
# in this view we want the last message last
@@ -215,8 +215,8 @@ def view(request, thread_id, form_class=ReplyForm,
unread = True
if participant.read_at and message.sent_at <= participant.read_at:
unread = False
- participant.read_at = now
message_list.append((message, unread,))
+ participant.read_at = right_now
participant.save()
return render_to_response(template_name, {
'thread': thread,
@@ -241,9 +241,9 @@ def batch_update(request, success_url=None):
if participant:
participant = participant[0]
if request.POST.get("action") == "read":
- participant.read_at = datetime.datetime.now()
+ participant.read_at = now()
elif request.POST.get("action") == "delete":
- participant.deleted_at = datetime.datetime.now()
+ participant.deleted_at = now()
elif request.POST.get("action") == "unread":
participant.read_at = None
participant.save()
Please sign in to comment.
Something went wrong with that request. Please try again.