datatracker/ietf/bin/fix-ambiguous-timestamps
2018-11-12 15:44:22 +00:00

88 lines
2.7 KiB
Python
Executable file

#!/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)