# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from celerybeatmongo.models import PeriodicTask as BasePeriodicTask, PERIODS

from udata.i18n import lazy_gettext as _
from udata.models import db


__all__ = ('PeriodicTask', 'PERIODS')


class PeriodicTask(BasePeriodicTask):
    last_run_id = db.StringField()

    class Interval(BasePeriodicTask.Interval):
        def __unicode__(self):
            if self.every == 1:
                return _('every {0.period_singular}').format(self)
            return _('every {0.every} {0.period}').format(self)

    @property
    def schedule_display(self):
        if self.interval:
            return str(self.interval)
        elif self.crontab:
            return str(self.crontab)
        else:
            raise Exception("must define internal or crontab schedule")

@@ -246,16 +246,87 @@
 IODS')%0A%0A
+CRON = '%7Bminute%7D %7Bhour%7D %7Bday_of_month%7D %7Bmonth_of_year%7D %7Bday_of_week%7D'%0A%0A
 %0Aclass P
@@ -634,16 +634,138 @@
 (self)%0A%0A
+    class Crontab(BasePeriodicTask.Crontab):%0A        def __unicode__(self):%0A            return CRON.format(**self._data)%0A%0A
     @pro
@@ -944,16 +944,16 @@
   else:%0A
-
         
@@ -1016,8 +1016,107 @@
 edule%22)%0A
+%0A    interval = db.EmbeddedDocumentField(Interval)%0A    crontab = db.EmbeddedDocumentField(Crontab)%0A

Fix periodic tasks model display