Skip to content

Commit b4cddac

Browse files
authored
Default timezone of CrontabSchedule from 'CELERY_TIMEZONE' setting. (celery#346)
* Default timezone of CrontabSchedule model from 'CELERY_TIMEZONE' setting. * Add test for CrontabSchedule default timezone. * Lint test for CrontabSchedule default timezone. * Lint models, skip linting for new migration and fix 'CrontabSchedule.timezone.help_text' double space. * Minor change in migration * Lint models with 'pydocstyle'. * Get CELERY_TIMEZONE setting from current app namespace. * Regenerate new migration file
1 parent add93c2 commit b4cddac

File tree

6 files changed

+56
-7
lines changed

6 files changed

+56
-7
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Generated by Django 3.0.6 on 2020-06-09 07:27
2+
# flake8: noqa
3+
from django.db import migrations
4+
import django_celery_beat.models
5+
import timezone_field.fields
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('django_celery_beat', '0012_periodictask_expire_seconds'),
12+
]
13+
14+
operations = [
15+
migrations.AlterField(
16+
model_name='crontabschedule',
17+
name='timezone',
18+
field=timezone_field.fields.TimeZoneField(default=django_celery_beat.models.crontab_schedule_celery_timezone, help_text='Timezone to Run the Cron Schedule on. Default is UTC.', verbose_name='Cron Timezone'),
19+
),
20+
]

django_celery_beat/models.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from datetime import timedelta
33

44
import timezone_field
5-
from celery import schedules
5+
from celery import schedules, current_app
66
from django.conf import settings
77
from django.core.exceptions import MultipleObjectsReturned, ValidationError
88
from django.core.validators import MaxValueValidator, MinValueValidator
@@ -46,6 +46,21 @@ def cronexp(field):
4646
return field and str(field).replace(' ', '') or '*'
4747

4848

49+
def crontab_schedule_celery_timezone():
50+
"""Return timezone string from Django settings `CELERY_TIMEZONE` variable.
51+
52+
If is not defined or is not a valid timezone, return `"UTC"` instead.
53+
"""
54+
try:
55+
CELERY_TIMEZONE = getattr(
56+
settings, '%s_TIMEZONE' % current_app.namespace)
57+
except AttributeError:
58+
return 'UTC'
59+
return CELERY_TIMEZONE if CELERY_TIMEZONE in [
60+
choice[0].zone for choice in timezone_field.TimeZoneField.CHOICES
61+
] else 'UTC'
62+
63+
4964
class SolarSchedule(models.Model):
5065
"""Schedule following astronomical patterns.
5166
@@ -277,10 +292,10 @@ class CrontabSchedule(models.Model):
277292
)
278293

279294
timezone = timezone_field.TimeZoneField(
280-
default='UTC',
295+
default=crontab_schedule_celery_timezone,
281296
verbose_name=_('Cron Timezone'),
282297
help_text=_(
283-
'Timezone to Run the Cron Schedule on. Default is UTC.'),
298+
'Timezone to Run the Cron Schedule on. Default is UTC.'),
284299
)
285300

286301
class Meta:

locale/es/LC_MESSAGES/django.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ msgid "Cron Timezone"
266266
msgstr "Zona horaria Cron"
267267

268268
#: django_celery_beat/models.py:293
269-
msgid "Timezone to Run the Cron Schedule on. Default is UTC."
269+
msgid "Timezone to Run the Cron Schedule on. Default is UTC."
270270
msgstr "Zona horaria donde ejecutar la programación Cron. Por defecto UTC."
271271

272272
#: django_celery_beat/models.py:299

locale/fr/LC_MESSAGES/django.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ msgid "Cron Timezone"
267267
msgstr "Fuseau Horaire Cron"
268268

269269
#: django_celery_beat/models.py:293
270-
msgid "Timezone to Run the Cron Schedule on. Default is UTC."
270+
msgid "Timezone to Run the Cron Schedule on. Default is UTC."
271271
msgstr ""
272272
"Fuseau Horaire pour lequel démarrer la planification Cron. UTC par défaut."
273273

locale/ru/LC_MESSAGES/django.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ msgid "Cron Timezone"
251251
msgstr "Временная зона для Cron"
252252

253253
#: django_celery_beat/models.py:273
254-
msgid "Timezone to Run the Cron Schedule on. Default is UTC."
254+
msgid "Timezone to Run the Cron Schedule on. Default is UTC."
255255
msgstr "Временная зона для Cron расписания. UTC по умолчанию."
256256

257257
#: django_celery_beat/models.py:279

t/unit/test_models.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import os
22

3-
from django.test import TestCase
3+
from django.test import TestCase, override_settings
44
from django.apps import apps
55
from django.db.migrations.state import ProjectState
66
from django.db.migrations.autodetector import MigrationAutodetector
77
from django.db.migrations.loader import MigrationLoader
88
from django.db.migrations.questioner import NonInteractiveMigrationQuestioner
99

10+
import timezone_field
11+
1012
from django_celery_beat import migrations as beat_migrations
13+
from django_celery_beat.models import crontab_schedule_celery_timezone
1114

1215

1316
class MigrationTests(TestCase):
@@ -54,3 +57,14 @@ def test_models_match_migrations(self):
5457
self.assertTrue(
5558
not changes,
5659
msg='Model changes exist that do not have a migration')
60+
61+
62+
class CrontabScheduleTestCase(TestCase):
63+
FIRST_VALID_TIMEZONE = timezone_field.TimeZoneField.CHOICES[0][0].zone
64+
65+
def test_default_timezone_without_settings_config(self):
66+
assert crontab_schedule_celery_timezone() == "UTC"
67+
68+
@override_settings(CELERY_TIMEZONE=FIRST_VALID_TIMEZONE)
69+
def test_default_timezone_with_settings_config(self):
70+
assert crontab_schedule_celery_timezone() == self.FIRST_VALID_TIMEZONE

0 commit comments

Comments
 (0)