From c87d182102f783015e95e7d98a290dbd9bfd7ea4 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Mon, 24 Jun 2013 13:05:41 -0400 Subject: [PATCH] Avoid saving user_presence twice when creating a new object. (imported from commit 72af41b4e0b85f9a8605f5040c321c80e16e8a97) --- zephyr/lib/actions.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/zephyr/lib/actions.py b/zephyr/lib/actions.py index 7f040c4823..a0c112149a 100644 --- a/zephyr/lib/actions.py +++ b/zephyr/lib/actions.py @@ -795,7 +795,8 @@ def do_update_user_presence(user_profile, client, log_time, status): (presence, created) = UserPresence.objects.get_or_create( user_profile = user_profile, client = client, - defaults = {'timestamp': log_time}) + defaults = {'timestamp': log_time, + 'status': status}) except IntegrityError: transaction.commit() presence = UserPresence.objects.get(user_profile = user_profile, @@ -806,9 +807,10 @@ def do_update_user_presence(user_profile, client, log_time, status): was_idle = presence.status == UserPresence.IDLE became_online = (status == UserPresence.ACTIVE) and (stale_status or was_idle) - presence.timestamp = log_time - presence.status = status - presence.save(update_fields=["timestamp", "status"]) + if not created: + presence.timestamp = log_time + presence.status = status + presence.save(update_fields=["timestamp", "status"]) if not user_profile.realm.domain == "mit.edu" and (created or became_online): # Push event to all users in the realm so they see the new user