diff --git a/ietf/bin/fix-ambiguous-document-timestamps b/ietf/bin/fix-ambiguous-document-timestamps deleted file mode 100755 index 2436e9303..000000000 --- a/ietf/bin/fix-ambiguous-document-timestamps +++ /dev/null @@ -1,51 +0,0 @@ -#!/usr/bin/env python -# -*- Python -*- - -""" -This script looks at document timestamps going back three years, and if it finds -ambiguous timestamps, shifts them backwards one hour in order to disambiguate -them. -""" - -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 ietf.doc.models import Document, DocEvent - -now = datetime.datetime.now() -then = now - datetime.timedelta(days=365*3) - -for d in Document.objects.filter(time__gt=then).order_by('-time'): - tz = pytz.timezone(settings.TIME_ZONE) - try: - t = tz.localize(d.time, is_dst=None) - except pytz.AmbiguousTimeError as e: - orig = d.time - d.time = d.time - datetime.timedelta(minutes=60) - print "%s:\n changed %s --> %s" % (d, orig, d.time) - d.save() - -for d in DocEvent.objects.filter(time__gt=then).order_by('-time'): - tz = pytz.timezone(settings.TIME_ZONE) - try: - t = tz.localize(d.time, is_dst=None) - except pytz.AmbiguousTimeError as e: - orig = d.time - d.time = d.time - datetime.timedelta(minutes=60) - print "%s:\n changed %s --> %s" % (d, orig, d.time) - d.save() - diff --git a/ietf/bin/fix-ambiguous-timestamps b/ietf/bin/fix-ambiguous-timestamps new file mode 100755 index 000000000..b7d64d369 --- /dev/null +++ b/ietf/bin/fix-ambiguous-timestamps @@ -0,0 +1,89 @@ +#!/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 +import debug +import tqdm + +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) +