Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

user_activity: Use .first() instead of queryset indexing.

Tim Abbott721172958b29
diff --git a/zerver/actions/user_activity.py b/zerver/actions/user_activity.py
index eb0ca8379bf..a9f3969305c 100644
--- a/zerver/actions/user_activity.py
+++ b/zerver/actions/user_activity.py
@@ -10,18 +10,15 @@ def do_update_user_activity_interval(user_profile: UserProfile, log_time: dateti
# This code isn't perfect, because with various races we might end
# up creating two overlapping intervals, but that shouldn't happen
# often, and can be corrected for in post-processing
- try:
- last = UserActivityInterval.objects.filter(user_profile=user_profile).order_by("-end")[0]
- # Two intervals overlap iff each interval ends after the other
- # begins. In this case, we just extend the old interval to
- # include the new interval.
- if log_time <= last.end and effective_end >= last.start:
- last.end = max(last.end, effective_end)
- last.start = min(last.start, log_time)
- last.save(update_fields=["start", "end"])
- return
- except IndexError:
- pass
+ last = UserActivityInterval.objects.filter(user_profile=user_profile).order_by("-end").first()
+ # Two intervals overlap iff each interval ends after the other
+ # begins. In this case, we just extend the old interval to
+ # include the new interval.
+ if last is not None and log_time <= last.end and effective_end >= last.start:
+ last.end = max(last.end, effective_end)
+ last.start = min(last.start, log_time)
+ last.save(update_fields=["start", "end"])
+ return
# Otherwise, the intervals don't overlap, so we should make a new one
UserActivityInterval.objects.create(