Skip to content

Commit

Permalink
Default timezone of CrontabSchedule from 'CELERY_TIMEZONE' setting. (c…
Browse files Browse the repository at this point in the history
…elery#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
  • Loading branch information
mondeja authored Jun 9, 2020
1 parent add93c2 commit b4cddac
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 7 deletions.
20 changes: 20 additions & 0 deletions django_celery_beat/migrations/0013_auto_20200609_0727.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.0.6 on 2020-06-09 07:27
# flake8: noqa
from django.db import migrations
import django_celery_beat.models
import timezone_field.fields


class Migration(migrations.Migration):

dependencies = [
('django_celery_beat', '0012_periodictask_expire_seconds'),
]

operations = [
migrations.AlterField(
model_name='crontabschedule',
name='timezone',
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'),
),
]
21 changes: 18 additions & 3 deletions django_celery_beat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import timedelta

import timezone_field
from celery import schedules
from celery import schedules, current_app
from django.conf import settings
from django.core.exceptions import MultipleObjectsReturned, ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
Expand Down Expand Up @@ -46,6 +46,21 @@ def cronexp(field):
return field and str(field).replace(' ', '') or '*'


def crontab_schedule_celery_timezone():
"""Return timezone string from Django settings `CELERY_TIMEZONE` variable.
If is not defined or is not a valid timezone, return `"UTC"` instead.
"""
try:
CELERY_TIMEZONE = getattr(
settings, '%s_TIMEZONE' % current_app.namespace)
except AttributeError:
return 'UTC'
return CELERY_TIMEZONE if CELERY_TIMEZONE in [
choice[0].zone for choice in timezone_field.TimeZoneField.CHOICES
] else 'UTC'


class SolarSchedule(models.Model):
"""Schedule following astronomical patterns.
Expand Down Expand Up @@ -277,10 +292,10 @@ class CrontabSchedule(models.Model):
)

timezone = timezone_field.TimeZoneField(
default='UTC',
default=crontab_schedule_celery_timezone,
verbose_name=_('Cron Timezone'),
help_text=_(
'Timezone to Run the Cron Schedule on. Default is UTC.'),
'Timezone to Run the Cron Schedule on. Default is UTC.'),
)

class Meta:
Expand Down
2 changes: 1 addition & 1 deletion locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ msgid "Cron Timezone"
msgstr "Zona horaria Cron"

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

#: django_celery_beat/models.py:299
Expand Down
2 changes: 1 addition & 1 deletion locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ msgid "Cron Timezone"
msgstr "Fuseau Horaire Cron"

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

Expand Down
2 changes: 1 addition & 1 deletion locale/ru/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ msgid "Cron Timezone"
msgstr "Временная зона для Cron"

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

#: django_celery_beat/models.py:279
Expand Down
16 changes: 15 additions & 1 deletion t/unit/test_models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import os

from django.test import TestCase
from django.test import TestCase, override_settings
from django.apps import apps
from django.db.migrations.state import ProjectState
from django.db.migrations.autodetector import MigrationAutodetector
from django.db.migrations.loader import MigrationLoader
from django.db.migrations.questioner import NonInteractiveMigrationQuestioner

import timezone_field

from django_celery_beat import migrations as beat_migrations
from django_celery_beat.models import crontab_schedule_celery_timezone


class MigrationTests(TestCase):
Expand Down Expand Up @@ -54,3 +57,14 @@ def test_models_match_migrations(self):
self.assertTrue(
not changes,
msg='Model changes exist that do not have a migration')


class CrontabScheduleTestCase(TestCase):
FIRST_VALID_TIMEZONE = timezone_field.TimeZoneField.CHOICES[0][0].zone

def test_default_timezone_without_settings_config(self):
assert crontab_schedule_celery_timezone() == "UTC"

@override_settings(CELERY_TIMEZONE=FIRST_VALID_TIMEZONE)
def test_default_timezone_with_settings_config(self):
assert crontab_schedule_celery_timezone() == self.FIRST_VALID_TIMEZONE

0 comments on commit b4cddac

Please sign in to comment.