diff --git a/zephyr/lib/time.py b/zephyr/lib/time.py new file mode 100644 index 0000000000..7d17be7b28 --- /dev/null +++ b/zephyr/lib/time.py @@ -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) diff --git a/zephyr/management/commands/populate_db.py b/zephyr/management/commands/populate_db.py index b11f7b46a4..dc8c12aece 100644 --- a/zephyr/management/commands/populate_db.py +++ b/zephyr/management/commands/populate_db.py @@ -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 diff --git a/zephyr/models.py b/zephyr/models.py index 144e6b5104..3d6f9a60ee 100644 --- a/zephyr/models.py +++ b/zephyr/models.py @@ -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', diff --git a/zephyr/views.py b/zephyr/views.py index 8e0f2264b6..597e1ac280 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -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