Skip to content

Commit 2005bbe

Browse files
author
Omer Katz
authored
Merge pull request #123 from ShaheedHaque/master
Size CrontabSchedule fields for max length values.
2 parents feaf34e + 9ac68ef commit 2005bbe

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Generated by Django 2.0.1 on 2018-02-10 12:26
2+
from __future__ import absolute_import, unicode_literals
3+
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('django_celery_beat', '0005_add_solarschedule_events_choices'),
11+
]
12+
13+
operations = [
14+
migrations.AlterField(
15+
model_name='crontabschedule',
16+
name='day_of_month',
17+
field=models.CharField(default='*', max_length=124,
18+
verbose_name='day of month'),
19+
),
20+
migrations.AlterField(
21+
model_name='crontabschedule',
22+
name='hour',
23+
field=models.CharField(default='*', max_length=96,
24+
verbose_name='hour'),
25+
),
26+
migrations.AlterField(
27+
model_name='crontabschedule',
28+
name='minute',
29+
field=models.CharField(default='*', max_length=240,
30+
verbose_name='minute'),
31+
),
32+
]

django_celery_beat/models.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,20 @@ def period_singular(self):
142142
class CrontabSchedule(models.Model):
143143
"""Crontab-like schedule."""
144144

145-
minute = models.CharField(_('minute'), max_length=64, default='*')
146-
hour = models.CharField(_('hour'), max_length=64, default='*')
145+
#
146+
# The worst case scenario for day of month is a list of all 31 day numbers
147+
# '[1, 2, ..., 31]' which has a length of 115. Likewise, minute can be
148+
# 0..59 and hour can be 0..23. Ensure we can accomodate these by allowing
149+
# 4 chars for each value (what we save on 0-9 accomodates the []).
150+
# We leave the other fields at their historical length.
151+
#
152+
minute = models.CharField(_('minute'), max_length=60 * 4, default='*')
153+
hour = models.CharField(_('hour'), max_length=24 * 4, default='*')
147154
day_of_week = models.CharField(
148155
_('day of week'), max_length=64, default='*',
149156
)
150157
day_of_month = models.CharField(
151-
_('day of month'), max_length=64, default='*',
158+
_('day of month'), max_length=31 * 4, default='*',
152159
)
153160
month_of_year = models.CharField(
154161
_('month of year'), max_length=64, default='*',

t/unit/test_schedulers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,27 @@ def test_CrontabSchedule_schedule(self):
436436
assert s.schedule.day_of_month == {1, 16}
437437
assert s.schedule.month_of_year == {1, 7}
438438

439+
def test_CrontabSchedule_long_schedule(self):
440+
s = CrontabSchedule(
441+
minute=str(list(range(60)))[1:-1],
442+
hour=str(list(range(24)))[1:-1],
443+
day_of_week=str(list(range(7)))[1:-1],
444+
day_of_month=str(list(range(1, 32)))[1:-1],
445+
month_of_year=str(list(range(1, 13)))[1:-1]
446+
)
447+
assert s.schedule.minute == set(range(60))
448+
assert s.schedule.hour == set(range(24))
449+
assert s.schedule.day_of_week == set(range(7))
450+
assert s.schedule.day_of_month == set(range(1, 32))
451+
assert s.schedule.month_of_year == set(range(1, 13))
452+
fields = [
453+
'minute', 'hour', 'day_of_week', 'day_of_month', 'month_of_year'
454+
]
455+
for field in fields:
456+
str_length = len(str(getattr(s.schedule, field)))
457+
field_length = s._meta.get_field(field).max_length
458+
assert str_length <= field_length
459+
439460
def test_SolarSchedule_schedule(self):
440461
s = SolarSchedule(event='solar_noon', latitude=48.06, longitude=12.86)
441462
dt = datetime(day=26, month=7, year=2050, hour=1, minute=0)

0 commit comments

Comments
 (0)