datatracker/ietf/utils/management/commands/fix_ambiguous_timestamps.py
Jennifer Richards ebebdbed3e
refactor: replace datetime.now and datetime.today with timezone.now (#4211)
* refactor: replace datetime.now with timezone.now

* refactor: migrate model fields to use timezone.now as default

* refactor: replace datetime.today with timezone.now

datetime.datetime.today() is equivalent to datetime.datetime.now(); both
return a naive datetime with the current local time.

* refactor: rephrase datetime.now(tz) as timezone.now().astimezone(tz)

This is effectively the same, but is less likely to encourage accidental
use of naive datetimes.

* refactor: revert datetime.today() change to old migrations

* refactor: change a missed datetime.now to timezone.now

* chore: renumber timezone_now migration

* chore: renumber migrations
2022-08-25 13:45:16 -03:00

66 lines
2.2 KiB
Python

# Copyright The IETF Trust 2019-2020, All Rights Reserved
# -*- coding: utf-8 -*-
import datetime
import pytz
import sys
from django.apps import apps
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import models
from django.utils import timezone
import debug # pyflakes:ignore
from ietf.person.models import Person
from ietf.doc.models import DocEvent
# ----------------------------------------------------------------------
by = Person.objects.get(name='(System)')
tz = pytz.timezone(settings.TIME_ZONE)
class Command(BaseCommand):
def note(self, msg):
if not self.quiet:
sys.stderr.write('%s\n' % msg)
def fixup(self, model, field, start, stop):
lookup = {
'%s__gt'%field: start,
'%s__lt'%field: stop,
}
app_label = model._meta.app_label
self.note("%s.%s.%s:" % (app_label, model.__name__, field))
for d in model.objects.filter(**lookup).order_by('-%s'%field):
orig = getattr(d, field)
try:
tz.localize(orig, is_dst=None)
except pytz.AmbiguousTimeError as e:
new = orig-datetime.timedelta(minutes=60)
setattr(d, field, new)
desc = " %s: changed ambiguous time: %s --> %s" % (d.pk, orig, new)
self.note(desc)
if app_label == 'doc' and model.__name__ == 'Document':
e = DocEvent(type='added_comment', doc=d, rev=d.rev, by=by, desc=desc)
e.save()
d.save_with_history([e])
else:
d.save()
def handle(self, *app_labels, **options):
self.verbosity = options['verbosity']
self.quiet = self.verbosity < 1
stop = timezone.now()
start = stop - datetime.timedelta(days=14)
for name, appconf in apps.app_configs.items():
for model in appconf.get_models():
for field in model._meta.fields:
if isinstance(field, models.DateTimeField):
self.fixup(model, field.name, start, stop)