Add new timestamp_to_datetime helper function.

(imported from commit 6791d009ae2e8371abe2c929e87c816a1981f5fe)
This commit is contained in:
Tim Abbott 2012-12-05 13:53:23 -05:00
parent f843152427
commit 970969fc44
4 changed files with 13 additions and 7 deletions

5
zephyr/lib/time.py Normal file
View File

@ -0,0 +1,5 @@
import datetime
from django.utils.timezone import utc
def timestamp_to_datetime(timestamp):
return datetime.datetime.utcfromtimestamp(float(timestamp)).replace(tzinfo=utc)

View File

@ -15,6 +15,7 @@ from django.db import transaction, connection
from django.conf import settings
from api.bots import mit_subs_list
from zephyr.lib.bulk_create import batch_bulk_create
from zephyr.lib.time import timestamp_to_datetime
import simplejson
import datetime
@ -463,8 +464,7 @@ def restore_saved_messages():
message.type = type_hash[old_message["type"]]
message.content = old_message["content"]
message.subject = old_message["subject"]
ts = float(old_message["timestamp"])
message.pub_date = datetime.datetime.utcfromtimestamp(ts).replace(tzinfo=utc)
message.pub_date = timestamp_to_datetime(old_message["timestamp"])
if message.type == Recipient.PERSONAL:
message.recipient = user_recipients[old_message["recipient"][0]["email"]]
@ -524,8 +524,8 @@ def restore_saved_messages():
elif old_message["type"] == "user_activated" or old_message["type"] == "user_created":
# These are rare, so just handle them the slow way
user = User.objects.get(email=old_message["user"])
timestamp=datetime.datetime.utcfromtimestamp(float(old_message['timestamp'])).replace(tzinfo=utc)
do_activate_user(user, log=False, timestamp=timestamp)
join_date = timestamp_to_datetime(old_message['timestamp'])
do_activate_user(user, log=False, join_date=join_date)
# Update the cache of users to show this user as activated
users_by_id[user.userprofile.id] = UserProfile.objects.get(user=user)
users[user.email] = user.userprofile

View File

@ -601,10 +601,10 @@ def log_subscription_property_change(user_email, property, property_dict):
event.update(property_dict)
log_event(event)
def do_activate_user(user, log=True, timestamp=time.time()):
def do_activate_user(user, log=True, join_date=time.time()):
user.is_active = True
user.set_password(initial_password(user.email))
user.date_joined = timestamp
user.date_joined = join_date
user.save()
if log:
log_event({'type': 'user_activated',

View File

@ -28,6 +28,7 @@ from zephyr.decorator import asynchronous, require_post, \
from zephyr.lib.query import last_n
from zephyr.lib.avatar import gravatar_hash
from zephyr.lib.response import json_success, json_error
from zephyr.lib.time import timestamp_to_datetime
from confirmation.models import Confirmation
@ -690,7 +691,7 @@ def send_message_backend(request, user_profile, client,
message.subject = subject_name
if forged:
# Forged messages come with a timestamp
message.pub_date = datetime.datetime.utcfromtimestamp(float(request.POST['time'])).replace(tzinfo=utc)
message.pub_date = timestamp_to_datetime(request.POST['time'])
else:
message.pub_date = now()
message.sending_client = client