From feaa5038a0e0409ee99ed677b9bf396b5a01d681 Mon Sep 17 00:00:00 2001 From: Henrik Levkowetz Date: Mon, 4 Nov 2019 12:56:52 +0000 Subject: [PATCH] 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 --- ietf/bin/fix-ambiguous-timestamps | 87 ------------------- .../commands/fix_ambiguous_timestamps.py | 65 ++++++++++++++ 2 files changed, 65 insertions(+), 87 deletions(-) delete mode 100755 ietf/bin/fix-ambiguous-timestamps create mode 100644 ietf/utils/management/commands/fix_ambiguous_timestamps.py diff --git a/ietf/bin/fix-ambiguous-timestamps b/ietf/bin/fix-ambiguous-timestamps deleted file mode 100755 index 2263cf31e..000000000 --- a/ietf/bin/fix-ambiguous-timestamps +++ /dev/null @@ -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) - diff --git a/ietf/utils/management/commands/fix_ambiguous_timestamps.py b/ietf/utils/management/commands/fix_ambiguous_timestamps.py new file mode 100644 index 000000000..174a8a321 --- /dev/null +++ b/ietf/utils/management/commands/fix_ambiguous_timestamps.py @@ -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) + \ No newline at end of file