[1.7.x] Fixed #23998 -- Added datetime.time support to migrations questioner.
Backport of 54085b0f9b
from master
This commit is contained in:
parent
c24624025b
commit
1ad5deedd4
|
@ -299,7 +299,9 @@ class MigrationWriter(object):
|
||||||
# Times
|
# Times
|
||||||
elif isinstance(value, datetime.time):
|
elif isinstance(value, datetime.time):
|
||||||
value_repr = repr(value)
|
value_repr = repr(value)
|
||||||
return value_repr, set(["import datetime"])
|
if isinstance(value, datetime_safe.time):
|
||||||
|
value_repr = "datetime.%s" % value_repr
|
||||||
|
return value_repr, {"import datetime"}
|
||||||
# Settings references
|
# Settings references
|
||||||
elif isinstance(value, SettingsReference):
|
elif isinstance(value, SettingsReference):
|
||||||
return "settings.%s" % value.setting_name, set(["from django.conf import settings"])
|
return "settings.%s" % value.setting_name, set(["from django.conf import settings"])
|
||||||
|
|
|
@ -7,9 +7,9 @@
|
||||||
# >>> datetime_safe.date(1850, 8, 2).strftime("%Y/%m/%d was a %A")
|
# >>> datetime_safe.date(1850, 8, 2).strftime("%Y/%m/%d was a %A")
|
||||||
# '1850/08/02 was a Friday'
|
# '1850/08/02 was a Friday'
|
||||||
|
|
||||||
from datetime import date as real_date, datetime as real_datetime
|
from datetime import date as real_date, time as real_time, datetime as real_datetime
|
||||||
import re
|
import re
|
||||||
import time
|
import time as ttime
|
||||||
|
|
||||||
|
|
||||||
class date(real_date):
|
class date(real_date):
|
||||||
|
@ -31,6 +31,10 @@ class datetime(real_datetime):
|
||||||
return date(self.year, self.month, self.day)
|
return date(self.year, self.month, self.day)
|
||||||
|
|
||||||
|
|
||||||
|
class time(real_time):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def new_date(d):
|
def new_date(d):
|
||||||
"Generate a safe date from a datetime.date object."
|
"Generate a safe date from a datetime.date object."
|
||||||
return date(d.year, d.month, d.day)
|
return date(d.year, d.month, d.day)
|
||||||
|
@ -80,10 +84,10 @@ def strftime(dt, fmt):
|
||||||
# Move to around the year 2000
|
# Move to around the year 2000
|
||||||
year = year + ((2000 - year) // 28) * 28
|
year = year + ((2000 - year) // 28) * 28
|
||||||
timetuple = dt.timetuple()
|
timetuple = dt.timetuple()
|
||||||
s1 = time.strftime(fmt, (year,) + timetuple[1:])
|
s1 = ttime.strftime(fmt, (year,) + timetuple[1:])
|
||||||
sites1 = _findall(s1, str(year))
|
sites1 = _findall(s1, str(year))
|
||||||
|
|
||||||
s2 = time.strftime(fmt, (year + 28,) + timetuple[1:])
|
s2 = ttime.strftime(fmt, (year + 28,) + timetuple[1:])
|
||||||
sites2 = _findall(s2, str(year + 28))
|
sites2 = _findall(s2, str(year + 28))
|
||||||
|
|
||||||
sites = []
|
sites = []
|
||||||
|
|
|
@ -158,3 +158,5 @@ Bugfixes
|
||||||
|
|
||||||
* Added quoting to field indexes in the SQL generated by migrations to prevent
|
* Added quoting to field indexes in the SQL generated by migrations to prevent
|
||||||
a crash when the index name requires it (:ticket:`#24015`).
|
a crash when the index name requires it (:ticket:`#24015`).
|
||||||
|
|
||||||
|
* Added ``datetime.time`` support to migrations questioner (:ticket:`23998`).
|
||||||
|
|
|
@ -112,6 +112,10 @@ class WriterTests(TestCase):
|
||||||
string, imports = MigrationWriter.serialize(safe_date)
|
string, imports = MigrationWriter.serialize(safe_date)
|
||||||
self.assertEqual(string, repr(datetime.date(2014, 3, 31)))
|
self.assertEqual(string, repr(datetime.date(2014, 3, 31)))
|
||||||
self.assertEqual(imports, {'import datetime'})
|
self.assertEqual(imports, {'import datetime'})
|
||||||
|
safe_time = datetime_safe.time(10, 25)
|
||||||
|
string, imports = MigrationWriter.serialize(safe_time)
|
||||||
|
self.assertEqual(string, repr(datetime.time(10, 25)))
|
||||||
|
self.assertEqual(imports, {'import datetime'})
|
||||||
safe_datetime = datetime_safe.datetime(2014, 3, 31, 16, 4, 31)
|
safe_datetime = datetime_safe.datetime(2014, 3, 31, 16, 4, 31)
|
||||||
string, imports = MigrationWriter.serialize(safe_datetime)
|
string, imports = MigrationWriter.serialize(safe_datetime)
|
||||||
self.assertEqual(string, repr(datetime.datetime(2014, 3, 31, 16, 4, 31)))
|
self.assertEqual(string, repr(datetime.datetime(2014, 3, 31, 16, 4, 31)))
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from datetime import date as original_date, datetime as original_datetime
|
from datetime import date as original_date, datetime as original_datetime, time as original_time
|
||||||
from django.utils.datetime_safe import date, datetime
|
from django.utils.datetime_safe import date, datetime, time
|
||||||
|
|
||||||
|
|
||||||
class DatetimeTests(unittest.TestCase):
|
class DatetimeTests(unittest.TestCase):
|
||||||
|
@ -9,6 +9,7 @@ class DatetimeTests(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.just_safe = (1900, 1, 1)
|
self.just_safe = (1900, 1, 1)
|
||||||
self.just_unsafe = (1899, 12, 31, 23, 59, 59)
|
self.just_unsafe = (1899, 12, 31, 23, 59, 59)
|
||||||
|
self.just_time = (11, 30, 59)
|
||||||
self.really_old = (20, 1, 1)
|
self.really_old = (20, 1, 1)
|
||||||
self.more_recent = (2006, 1, 1)
|
self.more_recent = (2006, 1, 1)
|
||||||
|
|
||||||
|
@ -21,6 +22,8 @@ class DatetimeTests(unittest.TestCase):
|
||||||
self.assertEqual(original_date(*self.just_safe).strftime('%Y-%m-%d'), date(*self.just_safe).strftime('%Y-%m-%d'))
|
self.assertEqual(original_date(*self.just_safe).strftime('%Y-%m-%d'), date(*self.just_safe).strftime('%Y-%m-%d'))
|
||||||
self.assertEqual(original_datetime(*self.just_safe).strftime('%Y-%m-%d'), datetime(*self.just_safe).strftime('%Y-%m-%d'))
|
self.assertEqual(original_datetime(*self.just_safe).strftime('%Y-%m-%d'), datetime(*self.just_safe).strftime('%Y-%m-%d'))
|
||||||
|
|
||||||
|
self.assertEqual(original_time(*self.just_time).strftime('%H:%M:%S'), time(*self.just_time).strftime('%H:%M:%S'))
|
||||||
|
|
||||||
def test_safe_strftime(self):
|
def test_safe_strftime(self):
|
||||||
self.assertEqual(date(*self.just_unsafe[:3]).strftime('%Y-%m-%d (weekday %w)'), '1899-12-31 (weekday 0)')
|
self.assertEqual(date(*self.just_unsafe[:3]).strftime('%Y-%m-%d (weekday %w)'), '1899-12-31 (weekday 0)')
|
||||||
self.assertEqual(date(*self.just_safe).strftime('%Y-%m-%d (weekday %w)'), '1900-01-01 (weekday 1)')
|
self.assertEqual(date(*self.just_safe).strftime('%Y-%m-%d (weekday %w)'), '1900-01-01 (weekday 1)')
|
||||||
|
@ -28,6 +31,8 @@ class DatetimeTests(unittest.TestCase):
|
||||||
self.assertEqual(datetime(*self.just_unsafe).strftime('%Y-%m-%d %H:%M:%S (weekday %w)'), '1899-12-31 23:59:59 (weekday 0)')
|
self.assertEqual(datetime(*self.just_unsafe).strftime('%Y-%m-%d %H:%M:%S (weekday %w)'), '1899-12-31 23:59:59 (weekday 0)')
|
||||||
self.assertEqual(datetime(*self.just_safe).strftime('%Y-%m-%d %H:%M:%S (weekday %w)'), '1900-01-01 00:00:00 (weekday 1)')
|
self.assertEqual(datetime(*self.just_safe).strftime('%Y-%m-%d %H:%M:%S (weekday %w)'), '1900-01-01 00:00:00 (weekday 1)')
|
||||||
|
|
||||||
|
self.assertEqual(time(*self.just_time).strftime('%H:%M:%S AM'), '11:30:59 AM')
|
||||||
|
|
||||||
# %y will error before this date
|
# %y will error before this date
|
||||||
self.assertEqual(date(*self.just_safe).strftime('%y'), '00')
|
self.assertEqual(date(*self.just_safe).strftime('%y'), '00')
|
||||||
self.assertEqual(datetime(*self.just_safe).strftime('%y'), '00')
|
self.assertEqual(datetime(*self.just_safe).strftime('%y'), '00')
|
||||||
|
|
Loading…
Reference in New Issue