Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix display of long name day of week #745

Merged
merged 3 commits into from
May 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion django_celery_beat/models.py
Original file line number Diff line number Diff line change
@@ -317,10 +317,22 @@ class Meta:

@property
def human_readable(self):
try:
c = schedules.crontab(
minute=self.minute,
hour=self.hour,
day_of_week=self.day_of_week,
day_of_month=self.day_of_month,
month_of_year=self.month_of_year,
)
day_of_week = cronexp(",".join(str(day) for day in c.day_of_week))
except ValueError:
day_of_week = cronexp(self.day_of_week)

cron_expression = '{} {} {} {} {}'.format(
cronexp(self.minute), cronexp(self.hour),
cronexp(self.day_of_month), cronexp(self.month_of_year),
cronexp(self.day_of_week)
day_of_week
)
try:
human_readable = get_description(cron_expression)
30 changes: 18 additions & 12 deletions t/unit/test_models.py
Original file line number Diff line number Diff line change
@@ -226,21 +226,27 @@ def test_invalid(self):
cron = CrontabSchedule.objects.create(
hour="2",
minute="0",
day_of_week="monxxx",
day_of_week="xxx",
)
self.assertEqual(
cron.human_readable, "0 2 * * monxxx UTC"
cron.human_readable, "0 2 * * xxx UTC"
)

def test_long_name(self):
"""Long day name display."""
# TODO: this should eventually work, but probably needs conversion
# before passing data to cron_description
cron = CrontabSchedule.objects.create(
hour="2",
minute="0",
day_of_week="monday",
)
self.assertEqual(
cron.human_readable, "0 2 * * monday UTC"
)
for day_day_of_week, expected in (
("1", "Monday"),
("mon", "Monday"),
("Monday,tue", "Monday and Tuesday"),
("sat-sun/2", "Saturday"),
("mon-wed", "Monday, Tuesday, and Wednesday"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we were missing a good all_days_of_the_week test here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @cclauss, I added a fix for all days of the week and more test cases here: #797.

):
cron = CrontabSchedule.objects.create(
hour="2",
minute="0",
day_of_week=day_day_of_week,
)

self.assertEqual(
cron.human_readable, f"At 02:00 AM, only on {expected} UTC"
)