Skip to content

Commit

Permalink
Eliminated all known uses of naive datetimes (#2202)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuastegmaier authored and rasarkar committed Dec 4, 2023
1 parent 9415851 commit 9421fc4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
6 changes: 3 additions & 3 deletions concordia/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

@celery_app.task
def expire_inactive_asset_reservations():
timestamp = datetime.datetime.now()
timestamp = timezone.now()

# Clear old reservations, with a grace period:
cutoff = timestamp - (
Expand All @@ -64,7 +64,7 @@ def expire_inactive_asset_reservations():

@celery_app.task
def tombstone_old_active_asset_reservations():
timestamp = datetime.datetime.now()
timestamp = timezone.now()

cutoff = timestamp - (
datetime.timedelta(hours=settings.TRANSCRIPTION_RESERVATION_TOMBSTONE_HOURS)
Expand All @@ -81,7 +81,7 @@ def tombstone_old_active_asset_reservations():

@celery_app.task
def delete_old_tombstoned_reservations():
timestamp = datetime.datetime.now()
timestamp = timezone.now()

cutoff = timestamp - (
datetime.timedelta(
Expand Down
12 changes: 6 additions & 6 deletions concordia/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Tests for the core application features
"""

from datetime import date, datetime, timedelta
from datetime import date, timedelta

from captcha.models import CaptchaStore
from django.conf import settings
Expand Down Expand Up @@ -577,7 +577,7 @@ def test_asset_reservation_expiration(self):
stale_reservation.full_clean()
stale_reservation.save()
# Backdate the object as if it happened 15 minutes ago:
old_timestamp = datetime.now() - timedelta(minutes=15)
old_timestamp = now() - timedelta(minutes=15)
AssetTranscriptionReservation.objects.update(
created_on=old_timestamp, updated_on=old_timestamp
)
Expand Down Expand Up @@ -625,10 +625,10 @@ def test_asset_reservation_tombstone(self):
tombstone_reservation.save()
# Backdate the object as if it was created hours ago,
# even if it was recently updated
old_timestamp = datetime.now() - timedelta(
old_timestamp = now() - timedelta(
hours=settings.TRANSCRIPTION_RESERVATION_TOMBSTONE_HOURS + 1
)
current_timestamp = datetime.now()
current_timestamp = now()
AssetTranscriptionReservation.objects.update(
created_on=old_timestamp, updated_on=current_timestamp
)
Expand Down Expand Up @@ -690,12 +690,12 @@ def test_asset_reservation_tombstone_expiration(self):
tombstone_reservation.save()
# Backdate the object as if it was created hours ago
# and tombstoned hours ago
old_timestamp = datetime.now() - timedelta(
old_timestamp = now() - timedelta(
hours=settings.TRANSCRIPTION_RESERVATION_TOMBSTONE_HOURS
+ settings.TRANSCRIPTION_RESERVATION_TOMBSTONE_LENGTH_HOURS
+ 1
)
not_as_old_timestamp = datetime.now() - timedelta(
not_as_old_timestamp = now() - timedelta(
hours=settings.TRANSCRIPTION_RESERVATION_TOMBSTONE_LENGTH_HOURS + 1
)
AssetTranscriptionReservation.objects.update(
Expand Down
5 changes: 4 additions & 1 deletion concordia/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,10 @@ def _get_pages(request):
latest_activity__day=date.day,
)
# CONCD-189 only show pages from the last 6 months
SIX_MONTHS_AGO = datetime.datetime.today() - datetime.timedelta(days=6 * 30)
# This should be an aware datetime, not a date. A date is cast
# to a naive datetime when it's compared to a datetime
# field, as is being done here
SIX_MONTHS_AGO = now() - datetime.timedelta(days=6 * 30)
assets = assets.filter(latest_activity__gte=SIX_MONTHS_AGO)
order_by = request.GET.get("order_by", "date-descending")
if order_by == "date-ascending":
Expand Down
6 changes: 3 additions & 3 deletions exporter/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import io
import zipfile
from datetime import datetime

from django.test import TestCase
from django.urls import reverse
from django.utils import timezone

from concordia.models import MediaType, Transcription, User
from concordia.tests.utils import (
Expand Down Expand Up @@ -60,8 +60,8 @@ def setUp(self):
asset=asset,
user=user,
text="Sample",
submitted=datetime.now(),
accepted=datetime.now(),
submitted=timezone.now(),
accepted=timezone.now(),
)
transcription1.full_clean()
transcription1.save()
Expand Down

0 comments on commit 9421fc4

Please sign in to comment.