models.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """
  2. Django Models.
  3. """
  4. from datetime import datetime
  5. import django
  6. from django.db import models
  7. from django.utils.translation import ugettext_lazy as _
  8. from picklefield.fields import PickledObjectField
  9. from celery import conf
  10. from celery.registry import tasks
  11. from celery.managers import TaskManager
  12. TASK_STATUS_PENDING = "PENDING"
  13. TASK_STATUS_RETRY = "RETRY"
  14. TASK_STATUS_FAILURE = "FAILURE"
  15. TASK_STATUS_SUCCESS = "SUCCESS"
  16. TASK_STATUSES = (TASK_STATUS_PENDING, TASK_STATUS_RETRY,
  17. TASK_STATUS_FAILURE, TASK_STATUS_SUCCESS)
  18. TASK_STATUSES_CHOICES = zip(TASK_STATUSES, TASK_STATUSES)
  19. class TaskMeta(models.Model):
  20. """Task result/status."""
  21. task_id = models.CharField(_(u"task id"), max_length=255, unique=True)
  22. status = models.CharField(_(u"task status"), max_length=50,
  23. default=TASK_STATUS_PENDING, choices=TASK_STATUSES_CHOICES)
  24. result = PickledObjectField()
  25. date_done = models.DateTimeField(_(u"done at"), auto_now=True)
  26. traceback = models.TextField(_(u"traceback"), blank=True, null=True)
  27. objects = TaskManager()
  28. class Meta:
  29. """Model meta-data."""
  30. verbose_name = _(u"task meta")
  31. verbose_name_plural = _(u"task meta")
  32. def __unicode__(self):
  33. return u"<Task: %s done:%s>" % (self.task_id, self.status)
  34. if (django.VERSION[0], django.VERSION[1]) >= (1, 1):
  35. # keep models away from syncdb/reset if database backend is not
  36. # being used.
  37. if conf.CELERY_BACKEND != 'database':
  38. TaskMeta._meta.managed = False