1+ # Copyright The IETF Trust 2014-2019, All Rights Reserved
2+ # -*- coding: utf-8 -*-
3+ from __future__ import absolute_import , print_function , unicode_literals
4+
5+ import datetime
6+ import pytz
7+ import sys
8+
9+ from django .apps import apps
10+ from django .conf import settings
11+ from django .contrib .contenttypes .models import ContentType
12+ from django .core .management .base import BaseCommand
13+ from django .db import models
14+
15+ import debug # pyflakes:ignore
16+
17+ from ietf .person .models import Person
18+ from ietf .doc .models import DocEvent
19+
20+ # ----------------------------------------------------------------------
21+
22+ by = Person .objects .get (name = '(System)' )
23+ tz = pytz .timezone (settings .TIME_ZONE )
24+
25+ class Command (BaseCommand ):
26+
27+ def note (self , msg ):
28+ if not self .quiet :
29+ sys .stderr .write ('%s\n ' % msg )
30+
31+ def fixup (self , model , field , start , stop ):
32+ lookup = {
33+ '%s__gt' % field : start ,
34+ '%s__lt' % field : stop ,
35+ }
36+ app_label = model ._meta .app_label
37+ self .note ("%s.%s.%s:" % (app_label , model .__name__ , field ))
38+ for d in model .objects .filter (** lookup ).order_by ('-%s' % field ):
39+ orig = getattr (d , field )
40+ try :
41+ tz .localize (orig , is_dst = None )
42+ except pytz .AmbiguousTimeError as e :
43+ new = orig - datetime .timedelta (minutes = 60 )
44+ setattr (d , field , new )
45+ desc = " %s: changed ambiguous time: %s --> %s" % (d .pk , orig , new )
46+ self .note (desc )
47+ if app_label == 'doc' and model .__name__ == 'Document' :
48+ e = DocEvent (type = 'added_comment' , doc = d , rev = d .rev , by = by , desc = desc )
49+ e .save ()
50+ d .save_with_history ([e ])
51+ else :
52+ d .save ()
53+
54+ def handle (self , * app_labels , ** options ):
55+ self .verbosity = options ['verbosity' ]
56+ self .quiet = self .verbosity < 1
57+ stop = datetime .datetime .now ()
58+ start = stop - datetime .timedelta (days = 14 )
59+
60+ for name , appconf in apps .app_configs .items ():
61+ for model in appconf .get_models ():
62+ for field in model ._meta .fields :
63+ if isinstance (field , models .DateTimeField ):
64+ self .fixup (model , field .name , start , stop )
65+
0 commit comments