|
| 1 | +# ============================================================================== |
| 2 | +# IMPORTS |
| 3 | +# ============================================================================== |
| 4 | +# ============================================================================== |
| 5 | +# IMPORTS |
| 6 | +# ============================================================================== |
| 7 | +# Python |
| 8 | +from __future__ import unicode_literals |
| 9 | +from datetime import datetime, time as datetime_time |
| 10 | +import pytz |
| 11 | + |
| 12 | +# Django |
| 13 | +from django.conf import settings |
| 14 | +from django.db import models |
| 15 | +from django.utils.translation import ugettext_lazy as _ |
| 16 | + |
| 17 | +# App |
| 18 | +from timezone_utils.fields import LinkedTZDateTimeField, TimeZoneField |
| 19 | +from timezone_utils.choices import PRETTY_ALL_TIMEZONES_CHOICES |
| 20 | + |
| 21 | + |
| 22 | +# ============================================================================== |
| 23 | +# MODELS |
| 24 | +# ============================================================================== |
| 25 | +class TZWithLowMaxLength(models.Model): |
| 26 | + """Test should check to make sure that the `max_length` is set to the |
| 27 | + maximum length of the longest value provided by pytz.all_timezones. |
| 28 | +
|
| 29 | + """ |
| 30 | + timezone = TimeZoneField( |
| 31 | + max_length=15, |
| 32 | + null=True, |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +class TZWithBadStringDefault(models.Model): |
| 37 | + """Test should check that a ValidationError is raised when...""" |
| 38 | + timezone = TimeZoneField( |
| 39 | + default='Bad/Worse', |
| 40 | + max_length=64, |
| 41 | + null=True, |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +class ModelWithBadTimeOverride(models.Model): |
| 46 | + timestamp = LinkedTZDateTimeField( |
| 47 | + default=settings.TEST_DATETIME, |
| 48 | + time_override='<invalid>', |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +class ModelWithBadPopulateFrom(models.Model): |
| 53 | + timezone = TimeZoneField(default='US/Eastern') |
| 54 | + timestamp = LinkedTZDateTimeField( |
| 55 | + default=settings.TEST_DATETIME, |
| 56 | + populate_from='invalid_field_reference', |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +class ModelWithBadTimeZoneCharField(models.Model): |
| 61 | + timezone = models.CharField(default='Bad/Worse', max_length=64) |
| 62 | + timestamp = LinkedTZDateTimeField( |
| 63 | + default=settings.TEST_DATETIME, |
| 64 | + populate_from='timezone', |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +class LocationTimeZone(models.Model): |
| 69 | + timezone = TimeZoneField( |
| 70 | + verbose_name=_('timezone'), |
| 71 | + max_length=64, |
| 72 | + null=True, |
| 73 | + blank=True |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +class LocationTimeZoneChoices(models.Model): |
| 78 | + timezone = TimeZoneField( |
| 79 | + verbose_name=_('timezone'), |
| 80 | + max_length=64, |
| 81 | + null=True, |
| 82 | + blank=True, |
| 83 | + choices=PRETTY_ALL_TIMEZONES_CHOICES, |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +class TZWithGoodStringDefault(models.Model): |
| 88 | + """Test should validate that""" |
| 89 | + timezone = TimeZoneField( |
| 90 | + default='US/Eastern', |
| 91 | + max_length=64, |
| 92 | + null=True, |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +class TZWithGoodTZInfoDefault(models.Model): |
| 97 | + timezone = TimeZoneField( |
| 98 | + default=pytz.timezone('US/Pacific'), |
| 99 | + max_length=64, |
| 100 | + null=True, |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +class ModelWithDateTimeOnly(models.Model): |
| 105 | + timestamp = LinkedTZDateTimeField( |
| 106 | + default=settings.TEST_DATETIME, |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +class NullModelWithDateTimeOnly(models.Model): |
| 111 | + timestamp = LinkedTZDateTimeField( |
| 112 | + null=True, |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +class CallableTimeStampedModel(models.Model): |
| 117 | + start = LinkedTZDateTimeField( |
| 118 | + default=settings.TEST_DATETIME, |
| 119 | + time_override=datetime.min.time |
| 120 | + ) |
| 121 | + end = LinkedTZDateTimeField( |
| 122 | + default=settings.TEST_DATETIME, |
| 123 | + time_override=datetime.max.time |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | +class StaticTimeStampedModel(models.Model): |
| 128 | + start = LinkedTZDateTimeField( |
| 129 | + default=settings.TEST_DATETIME, |
| 130 | + time_override=datetime_time(0, 0) |
| 131 | + ) |
| 132 | + end = LinkedTZDateTimeField( |
| 133 | + default=settings.TEST_DATETIME, |
| 134 | + time_override=datetime_time(23, 59, 59, 999999) |
| 135 | + ) |
| 136 | + |
| 137 | + |
| 138 | +class ModelWithForeignKeyToTimeZone(models.Model): |
| 139 | + other_model = models.ForeignKey( |
| 140 | + to='tests.TZWithGoodStringDefault', |
| 141 | + related_name='fk_to_tz' |
| 142 | + ) |
| 143 | + timestamp = LinkedTZDateTimeField( |
| 144 | + default=settings.TEST_DATETIME, |
| 145 | + populate_from=lambda instance: instance.other_model.timezone |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +class ModelWithLocalTimeZone(models.Model): |
| 150 | + timezone = TimeZoneField(default='US/Eastern') |
| 151 | + timestamp = LinkedTZDateTimeField( |
| 152 | + default=settings.TEST_DATETIME, |
| 153 | + populate_from='timezone' |
| 154 | + ) |
| 155 | + |
| 156 | + |
| 157 | +class ModelWithLocalTZCharField(models.Model): |
| 158 | + timezone = models.CharField(default='US/Eastern', max_length=64) |
| 159 | + timestamp = LinkedTZDateTimeField( |
| 160 | + default=settings.TEST_DATETIME, |
| 161 | + populate_from='timezone' |
| 162 | + ) |
0 commit comments