Skip to content

Commit

Permalink
String representation of IntervalSchedule model correctly translated …
Browse files Browse the repository at this point in the history
…using localized representation for period names. (#316)
  • Loading branch information
mondeja authored Feb 27, 2020
1 parent 99483fb commit f425436
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 204 deletions.
21 changes: 19 additions & 2 deletions django_celery_beat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
(MICROSECONDS, _('Microseconds')),
)

SINGULAR_PERIODS = (
(DAYS, _('Day')),
(HOURS, _('Hour')),
(MINUTES, _('Minute')),
(SECONDS, _('Second')),
(MICROSECONDS, _('Microsecond')),
)

SOLAR_SCHEDULES = [(x, _(x)) for x in sorted(schedules.solar._all_events)]


Expand Down Expand Up @@ -153,9 +161,18 @@ def from_schedule(cls, schedule, period=SECONDS):
return cls(every=every, period=period)

def __str__(self):
readable_period = None
if self.every == 1:
return _('every {0.period_singular}').format(self)
return _('every {0.every} {0.period}').format(self)
for period, _readable_period in SINGULAR_PERIODS:
if period == self.period:
readable_period = _readable_period.lower()
break
return _('every {}').format(readable_period)
for period, _readable_period in PERIOD_CHOICES:
if period == self.period:
readable_period = _readable_period.lower()
break
return _('every {} {}').format(self.every, readable_period)

@property
def period_singular(self):
Expand Down
Loading

0 comments on commit f425436

Please sign in to comment.