Rewrote the ietf/bin/fix-ambiguous-timestamps command as a management command (fix_ambiguous_timestamps) and made it look at all instances of DateTimeField by means of model introspection.

- Legacy-Id: 16954
This commit is contained in:
Henrik Levkowetz 2019-11-04 12:56:52 +00:00
parent cee82e9f43
commit feaa5038a0
2 changed files with 65 additions and 87 deletions

View file

@ -1,87 +0,0 @@
#!/usr/bin/env python
# Copyright The IETF Trust 2016-2018, All Rights Reserved
# -*- Python -*-
"""
This script looks at document timestamps going back 14 days, and if it finds
ambiguous timestamps, shifts them one hour in order to disambiguate
them.
"""
from __future__ import unicode_literals, print_function
import os, sys
import datetime
import pytz
filename = os.path.abspath(__file__)
basedir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
sys.path = [ basedir ] + sys.path
os.environ["DJANGO_SETTINGS_MODULE"] = "ietf.settings"
virtualenv_activation = os.path.join(basedir, "env", "bin", "activate_this.py")
if os.path.exists(virtualenv_activation):
execfile(virtualenv_activation, dict(__file__=virtualenv_activation))
import django
django.setup()
from django.conf import settings
from django.contrib.auth.models import User
from ietf.doc.models import Document, DocEvent, DocHistory
from ietf.mailinglists.models import Subscribed
from ietf.meeting.models import SchedTimeSessAssignment, Session
from ietf.message.models import Message
from ietf.person.models import Person, HistoricalEmail, HistoricalPerson
from ietf.submit.models import SubmissionCheck, SubmissionEvent
now = datetime.datetime.now()
then = now - datetime.timedelta(days=30)
by = Person.objects.get(name='(System)')
tz = pytz.timezone(settings.TIME_ZONE)
def fixup(Model, field, start, stop):
lookup = {
'%s__gt'%field: start,
'%s__lt'%field: stop,
}
print("%s.%s:" % (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)
print(desc)
if 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()
time_fields = (
(DocEvent, 'time'),
(DocHistory, 'time'),
(Document, 'time'),
(HistoricalEmail, 'time'),
(HistoricalPerson, 'time'),
(Message, 'time'),
(SchedTimeSessAssignment, 'modified'),
(Session, 'requested'),
(Session, 'scheduled'),
(Session, 'modified'),
(SubmissionCheck, 'time'),
(SubmissionEvent, 'time'),
(Subscribed, 'time'),
(User, 'date_joined'),
)
for Model, field in time_fields:
stop = datetime.datetime.now()
start = stop - datetime.timedelta(days=14)
fixup(Model, field, start, stop)

View file

@ -0,0 +1,65 @@
# Copyright The IETF Trust 2014-2019, All Rights Reserved
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import datetime
import pytz
import sys
from django.apps import apps
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand
from django.db import models
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 = datetime.datetime.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)