@@ -0,0 +1,2003 @@
+# -*- coding: utf-8 -*-%0A%0Aimport datetime%0Aimport logging%0Aimport sys%0A%0Afrom modularodm import Q%0A%0Afrom website.models import NodeLog%0A%0Afrom bson import ObjectId%0Afrom nose.tools import *%0A%0Afrom tests.base import OsfTestCase%0Afrom tests.factories import NodeLogFactory%0A%0A%0Alogger = logging.getLogger(__name__)%0Alogger.setLevel(logging.DEBUG)%0A%0A%0Adef find_invalid_logs():%0A    for log in NodeLog.find(Q('action', 'eq', NodeLog.WIKI_DELETED)):%0A        # Derive UTC datetime object from ObjectId%0A        id_date = ObjectId(log._id).generation_time%0A        id_date = id_date.replace(tzinfo=None) - id_date.utcoffset()%0A%0A        if id_date %3E log.date:%0A            yield log%0A%0A%0Adef fix_invalid_log(log):%0A    new_dt = ObjectId(log._id).generation_time%0A    new_dt = new_dt.replace(tzinfo=None) - new_dt.utcoffset()%0A    NodeLog._fields%5B'date'%5D.__set__(%0A        log,%0A        new_dt,%0A        safe=False%0A    )%0A    log.save()%0A%0A%0Aif __name__ == '__main__':%0A    if 'dry' in sys.argv:%0A        for log in find_invalid_logs():%0A            print(log._id)%0A    else:%0A        for log in find_invalid_logs():%0A            fix_invalid_log(log)%0A%0A%0Aclass TestEnsureLogDates(OsfTestCase):%0A%0A    def setUp(self):%0A        super(TestEnsureLogDates, self).setUp()%0A        self.good_log = NodeLogFactory(action=NodeLog.WIKI_DELETED)%0A        self.bad_log = NodeLogFactory(action=NodeLog.WIKI_DELETED)%0A%0A        self.mongo = self.good_log._storage%5B0%5D.db%5B'nodelog'%5D%0A%0A        self.mongo.update(%0A            %7B'_id': self.bad_log._id%7D,%0A            %7B'$set': %7B'date': datetime.datetime.utcnow() - datetime.timedelta(weeks=52)%7D%7D,%0A        )%0A        self.bad_log.reload()%0A%0A    def tearDown(self):%0A        super(TestEnsureLogDates, self).tearDown()%0A%0A    def test_find_invalid_logs(self):%0A        assert_equal(%0A            1,%0A            len(list(find_invalid_logs()))%0A        )%0A%0A    def test_fix_invalid_log(self):%0A        fix_invalid_log(self.bad_log)%0A        assert_true(%0A            self.good_log.date - self.bad_log.date %3C datetime.timedelta(seconds=1)%0A        )%0A%0A%0A

Add migration script for defective "delete wiki page" logs