forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_ambiguous_timestamps.py
More file actions
64 lines (51 loc) · 2.15 KB
/
fix_ambiguous_timestamps.py
File metadata and controls
64 lines (51 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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
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)