mirror of
https://github.com/zulip/zulip.git
synced 2026-06-18 21:01:52 +08:00
Replace datetime.now() with timezone.now() in Django ORM queries.
When you pass a naive datetime to the Django ORM, it uses settings.TIME_ZONE for the time zone. In the development environment, both settings.TIME_ZONE and datetime.now() use 'America/New_York', so there is no change in behavior there. (fromtimestamp with no tz argument uses the same timezone as datetime.now) We are soon going to change settings.TIME_ZONE to UTC, so need to remove naive datetimes from queries to the ORM.
This commit is contained in:
parent
01a4615f6e
commit
562bc6429c
@ -19,7 +19,7 @@ class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
# type: (*Any, **Any) -> None
|
||||
# Get list of all active users in the last 1 week
|
||||
cutoff = datetime.now() - timedelta(minutes=30, hours=168)
|
||||
cutoff = timezone.now() - timedelta(minutes=30, hours=168)
|
||||
|
||||
users = UserPresence.objects.select_related().filter(timestamp__gt=cutoff)
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@ from typing import Any
|
||||
from argparse import ArgumentParser
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db.models import Count, QuerySet
|
||||
from django.utils import timezone
|
||||
|
||||
from zerver.models import UserActivity, UserProfile, Realm, \
|
||||
get_realm, get_user_profile_by_email
|
||||
@ -38,7 +39,7 @@ Usage examples:
|
||||
#
|
||||
# Importantly, this does NOT tell you anything about the relative
|
||||
# volumes of requests from clients.
|
||||
threshold = datetime.datetime.now() - datetime.timedelta(days=7)
|
||||
threshold = timezone.now() - datetime.timedelta(days=7)
|
||||
client_counts = user_activity_objects.filter(
|
||||
last_visit__gt=threshold).values("client__name").annotate(
|
||||
count=Count('client__name'))
|
||||
|
||||
@ -445,7 +445,7 @@ def delete_realm_user_sessions(realm):
|
||||
# type: (Realm) -> None
|
||||
realm_user_ids = [user_profile.id for user_profile in
|
||||
UserProfile.objects.filter(realm=realm)]
|
||||
for session in Session.objects.filter(expire_date__gte=datetime.datetime.now()):
|
||||
for session in Session.objects.filter(expire_date__gte=timezone.now()):
|
||||
if get_session_user(session) in realm_user_ids:
|
||||
delete_session(session)
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@ from typing import Any
|
||||
|
||||
from optparse import make_option
|
||||
from django.core.management.base import BaseCommand, CommandParser
|
||||
from django.utils import timezone
|
||||
from zerver.models import get_realm, Message, Realm, Stream, Recipient
|
||||
|
||||
import datetime
|
||||
@ -31,7 +32,7 @@ class Command(BaseCommand):
|
||||
streams = Stream.objects.filter(realm=realm, invite_only=False)
|
||||
recipients = Recipient.objects.filter(
|
||||
type=Recipient.STREAM, type_id__in=[stream.id for stream in streams])
|
||||
cutoff = datetime.datetime.fromtimestamp(options["since"])
|
||||
cutoff = datetime.datetime.fromtimestamp(options["since"], tz=timezone.utc)
|
||||
messages = Message.objects.filter(pub_date__gt=cutoff, recipient__in=recipients)
|
||||
|
||||
for message in messages:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user