Change datetime.now(tz=X) to timezone.now().

datetime.now with a timezone set is equivalent to timezone.now() if it's
never being printed out, but the latter is cleaner and more idiomatic.
This commit is contained in:
Rishi Gupta 2017-02-25 12:50:51 -08:00 committed by Tim Abbott
parent 9b11993fa7
commit 2b2be8120f
5 changed files with 19 additions and 14 deletions

View File

@ -2,6 +2,7 @@ from __future__ import absolute_import
from __future__ import print_function
from django.core.management.base import BaseCommand
from django.utils import timezone
from typing import Any
from zerver.models import UserPresence, UserActivity
@ -35,7 +36,7 @@ class Command(BaseCommand):
for bucket in hour_buckets:
if bucket not in user_info[last_presence.user_profile.realm.string_id]:
user_info[last_presence.user_profile.realm.string_id][bucket] = []
if datetime.now(known_active.tzinfo) - known_active < timedelta(hours=bucket):
if timezone.now() - known_active < timedelta(hours=bucket):
user_info[last_presence.user_profile.realm.string_id][bucket].append(last_presence.user_profile.email)
for realm, buckets in user_info.items():
@ -51,7 +52,7 @@ class Command(BaseCommand):
for bucket in hour_buckets:
if bucket not in user_info[activity.user_profile.realm.string_id]:
user_info[activity.user_profile.realm.string_id][bucket] = []
if datetime.now(activity.last_visit.tzinfo) - activity.last_visit < timedelta(hours=bucket):
if timezone.now() - activity.last_visit < timedelta(hours=bucket):
user_info[activity.user_profile.realm.string_id][bucket].append(activity.user_profile.email)
for realm, buckets in user_info.items():
print("Realm %s" % (realm,))

View File

@ -10,6 +10,8 @@ import pytz
from django.core.management.base import BaseCommand
from django.db.models import Count
from django.utils import timezone
from zerver.models import UserProfile, Realm, Stream, Message, Recipient, UserActivity, \
Subscription, UserMessage, get_realm
@ -29,7 +31,7 @@ class Command(BaseCommand):
def active_users(self, realm):
# type: (Realm) -> List[UserProfile]
# Has been active (on the website, for now) in the last 7 days.
activity_cutoff = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(days=7)
activity_cutoff = timezone.now() - datetime.timedelta(days=7)
return [activity.user_profile for activity in (
UserActivity.objects.filter(user_profile__realm=realm,
user_profile__is_active=True,
@ -39,17 +41,17 @@ class Command(BaseCommand):
def messages_sent_by(self, user, days_ago):
# type: (UserProfile, int) -> int
sent_time_cutoff = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(days=days_ago)
sent_time_cutoff = timezone.now() - datetime.timedelta(days=days_ago)
return human_messages.filter(sender=user, pub_date__gt=sent_time_cutoff).count()
def total_messages(self, realm, days_ago):
# type: (Realm, int) -> int
sent_time_cutoff = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(days=days_ago)
sent_time_cutoff = timezone.now() - datetime.timedelta(days=days_ago)
return Message.objects.filter(sender__realm=realm, pub_date__gt=sent_time_cutoff).count()
def human_messages(self, realm, days_ago):
# type: (Realm, int) -> int
sent_time_cutoff = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(days=days_ago)
sent_time_cutoff = timezone.now() - datetime.timedelta(days=days_ago)
return human_messages.filter(sender__realm=realm, pub_date__gt=sent_time_cutoff).count()
def api_messages(self, realm, days_ago):
@ -58,19 +60,19 @@ class Command(BaseCommand):
def stream_messages(self, realm, days_ago):
# type: (Realm, int) -> int
sent_time_cutoff = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(days=days_ago)
sent_time_cutoff = timezone.now() - datetime.timedelta(days=days_ago)
return human_messages.filter(sender__realm=realm, pub_date__gt=sent_time_cutoff,
recipient__type=Recipient.STREAM).count()
def private_messages(self, realm, days_ago):
# type: (Realm, int) -> int
sent_time_cutoff = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(days=days_ago)
sent_time_cutoff = timezone.now() - datetime.timedelta(days=days_ago)
return human_messages.filter(sender__realm=realm, pub_date__gt=sent_time_cutoff).exclude(
recipient__type=Recipient.STREAM).exclude(recipient__type=Recipient.HUDDLE).count()
def group_private_messages(self, realm, days_ago):
# type: (Realm, int) -> int
sent_time_cutoff = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(days=days_ago)
sent_time_cutoff = timezone.now() - datetime.timedelta(days=days_ago)
return human_messages.filter(sender__realm=realm, pub_date__gt=sent_time_cutoff).exclude(
recipient__type=Recipient.STREAM).exclude(recipient__type=Recipient.PERSONAL).count()

View File

@ -7,6 +7,8 @@ import pytz
from typing import Any
from django.core.management.base import BaseCommand
from django.utils import timezone
from zerver.models import UserProfile, Realm, Stream, Message, get_realm
from six.moves import range
@ -20,8 +22,8 @@ class Command(BaseCommand):
def messages_sent_by(self, user, week):
# type: (UserProfile, int) -> int
start = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(days=(week + 1)*7)
end = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(days=week*7)
start = timezone.now() - datetime.timedelta(days=(week + 1)*7)
end = timezone.now() - datetime.timedelta(days=week*7)
return Message.objects.filter(sender=user, pub_date__gt=start, pub_date__lte=end).count()
def handle(self, *args, **options):

View File

@ -1000,7 +1000,7 @@ def realm_user_summary_table(all_records, admin_emails):
def is_recent(val):
# type: (Optional[datetime]) -> bool
age = datetime.now(val.tzinfo) - val
age = timezone.now() - val
return age.total_seconds() < 5 * 60
rows = []

View File

@ -1,12 +1,12 @@
from __future__ import absolute_import
import datetime
import pytz
import logging
from typing import Any
from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils import timezone
from zerver.lib.queue import queue_json_publish
from zerver.models import UserActivity, UserProfile, Realm
@ -43,7 +43,7 @@ def inactive_since(user_profile, cutoff):
def last_business_day():
# type: () -> datetime.datetime
one_day = datetime.timedelta(hours=23)
previous_day = datetime.datetime.now(tz=pytz.utc) - one_day
previous_day = timezone.now() - one_day
while previous_day.weekday() not in VALID_DIGEST_DAYS:
previous_day -= one_day
return previous_day