File tree Expand file tree Collapse file tree 3 files changed +49
-8
lines changed Expand file tree Collapse file tree 3 files changed +49
-8
lines changed Original file line number Diff line number Diff line change @@ -84,6 +84,26 @@ class LocationTimeZoneChoices(models.Model):
84
84
)
85
85
86
86
87
+ class LocationTimeZoneChoicesWithEmpty (models .Model ):
88
+ timezone = TimeZoneField (
89
+ verbose_name = _ ('timezone' ),
90
+ max_length = 64 ,
91
+ null = True ,
92
+ blank = True ,
93
+ choices = [('' , 'No time zone' )] + list (PRETTY_ALL_TIMEZONES_CHOICES ),
94
+ )
95
+
96
+
97
+ class LocationTimeZoneBadChoices (models .Model ):
98
+ timezone = TimeZoneField (
99
+ verbose_name = _ ('timezone' ),
100
+ max_length = 64 ,
101
+ null = True ,
102
+ blank = True ,
103
+ choices = [('Bad/Worse' , 'Bad Choice' )],
104
+ )
105
+
106
+
87
107
class TZWithGoodStringDefault (models .Model ):
88
108
"""Test should validate that"""
89
109
timezone = TimeZoneField (
Original file line number Diff line number Diff line change 8
8
import re
9
9
10
10
# Django
11
+ from django .core import checks
11
12
from django .test import TestCase
12
13
13
14
# App
15
+ from tests import models
14
16
from timezone_utils .choices import (ALL_TIMEZONES_CHOICES ,
15
17
COMMON_TIMEZONES_CHOICES ,
16
18
GROUPED_ALL_TIMEZONES_CHOICES ,
@@ -170,3 +172,21 @@ def test_get_choices_values_ungrouped(self):
170
172
values = map (itemgetter (0 ), choices )
171
173
for value in values :
172
174
self .assertIn (value , pytz .all_timezones )
175
+
176
+ def test_check_choices_attribute_good (self ):
177
+ self .assertEqual (models .LocationTimeZoneChoices .check (), [])
178
+ # Don't warn for empty values:
179
+ self .assertEqual (models .LocationTimeZoneChoicesWithEmpty .check (), [])
180
+
181
+ def test_check_choices_attribute_bad (self ):
182
+ self .assertEqual (models .LocationTimeZoneBadChoices .check (), [
183
+ checks .Warning (
184
+ msg = (
185
+ "'choices' contains an invalid time zone value 'Bad/Worse' "
186
+ "which was not found as a supported time zone by pytz "
187
+ "{version}." .format (version = pytz .__version__ )
188
+ ),
189
+ hint = 'Values must be found in pytz.all_timezones.' ,
190
+ obj = models .LocationTimeZoneBadChoices ._meta .get_field ('timezone' ),
191
+ ),
192
+ ])
Original file line number Diff line number Diff line change @@ -195,10 +195,11 @@ def _check_choices_attribute(self): # pragma: no cover
195
195
)
196
196
})
197
197
198
- # Return the warning
199
- return [
200
- checks .Warning (** warning_params )
201
- ]
198
+ # Return the warning
199
+ return [
200
+ checks .Warning (** warning_params )
201
+ ]
202
+
202
203
elif option_key not in pytz .all_timezones :
203
204
# Make sure we don't raise this error on empty
204
205
# values
@@ -211,10 +212,10 @@ def _check_choices_attribute(self): # pragma: no cover
211
212
)
212
213
})
213
214
214
- # Return the warning
215
- return [
216
- checks .Warning (** warning_params )
217
- ]
215
+ # Return the warning
216
+ return [
217
+ checks .Warning (** warning_params )
218
+ ]
218
219
219
220
# When no error, return an empty list
220
221
return []
You can’t perform that action at this time.
0 commit comments