from django.contrib.auth.models import User
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.utils.translation import ugettext_lazy as _


class Task(models.Model):
    CLOSE = 'cl'
    CANCEL = 'ca'
    LATER = 'la'
    UNDEFINED = 'un'
    CHOICES = (
        (UNDEFINED, _("Неизвестно")),
        (CLOSE, _("Завершить")),
        (CANCEL, _("Отменить")),
        (LATER, _("Отложить")),
    )

    title = models.CharField(_("Заголовок"), max_length=50)
    description = models.TextField(_("Описание"))
    executor = models.ForeignKey(User, verbose_name=_("Исполнитель"), on_delete=models.CASCADE)
    status = models.CharField(_("Статус"), choices=CHOICES, default=UNDEFINED, max_length=2)
    deadline = models.DateTimeField(_("Дедлайн"))
    priority = models.IntegerField(_("Приоритет"), default=1, validators=[MinValueValidator(1), MaxValueValidator(3)])
    changed = models.DateTimeField(_("Дата последнего изменения"), auto_now=True)
    created = models.DateTimeField(_("Дата создания"), auto_now_add=True)

    @property
    def text_status(self):
        choices = dict(self.CHOICES)
        return choices[self.status]

    @property
    def text_deadline(self):
        return self.deadline.strftime("%d.%m.%Y %H:%M")


class Comment(models.Model):
    task = models.ForeignKey(Task, related_name="comments", on_delete=models.CASCADE)
    creator = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    text = models.TextField(_('Комментарий'))
    created = models.DateTimeField(_("Дата создания"), auto_now_add=True)

[((1342, 1416), 'django.db.models.ForeignKey', 'models.ForeignKey', (['Task'], {'related_name': '"""comments"""', 'on_delete': 'models.CASCADE'}), "(Task, related_name='comments', on_delete=models.CASCADE)\n", (1359, 1416), False, 'from django.db import models\n'), ((1431, 1492), 'django.db.models.ForeignKey', 'models.ForeignKey', (['User'], {'on_delete': 'models.SET_NULL', 'null': '(True)'}), '(User, on_delete=models.SET_NULL, null=True)\n', (1448, 1492), False, 'from django.db import models\n'), ((490, 504), 'django.utils.translation.ugettext_lazy', '_', (['"""Заголовок"""'], {}), "('Заголовок')\n", (491, 504), True, 'from django.utils.translation import ugettext_lazy as _\n'), ((556, 569), 'django.utils.translation.ugettext_lazy', '_', (['"""Описание"""'], {}), "('Описание')\n", (557, 569), True, 'from django.utils.translation import ugettext_lazy as _\n'), ((697, 708), 'django.utils.translation.ugettext_lazy', '_', (['"""Статус"""'], {}), "('Статус')\n", (698, 708), True, 'from django.utils.translation import ugettext_lazy as _\n'), ((796, 808), 'django.utils.translation.ugettext_lazy', '_', (['"""Дедлайн"""'], {}), "('Дедлайн')\n", (797, 808), True, 'from django.utils.translation import ugettext_lazy as _\n'), ((845, 859), 'django.utils.translation.ugettext_lazy', '_', (['"""Приоритет"""'], {}), "('Приоритет')\n", (846, 859), True, 'from django.utils.translation import ugettext_lazy as _\n'), ((964, 994), 'django.utils.translation.ugettext_lazy', '_', (['"""Дата последнего изменения"""'], {}), "('Дата последнего изменения')\n", (965, 994), True, 'from django.utils.translation import ugettext_lazy as _\n'), ((1046, 1064), 'django.utils.translation.ugettext_lazy', '_', (['"""Дата создания"""'], {}), "('Дата создания')\n", (1047, 1064), True, 'from django.utils.translation import ugettext_lazy as _\n'), ((1521, 1537), 'django.utils.translation.ugettext_lazy', '_', (['"""Комментарий"""'], {}), "('Комментарий')\n", (1522, 1537), True, 'from django.utils.translation import ugettext_lazy as _\n'), ((1574, 1592), 'django.utils.translation.ugettext_lazy', '_', (['"""Дата создания"""'], {}), "('Дата создания')\n", (1575, 1592), True, 'from django.utils.translation import ugettext_lazy as _\n'), ((338, 353), 'django.utils.translation.ugettext_lazy', '_', (['"""Неизвестно"""'], {}), "('Неизвестно')\n", (339, 353), True, 'from django.utils.translation import ugettext_lazy as _\n'), ((372, 386), 'django.utils.translation.ugettext_lazy', '_', (['"""Завершить"""'], {}), "('Завершить')\n", (373, 386), True, 'from django.utils.translation import ugettext_lazy as _\n'), ((406, 419), 'django.utils.translation.ugettext_lazy', '_', (['"""Отменить"""'], {}), "('Отменить')\n", (407, 419), True, 'from django.utils.translation import ugettext_lazy as _\n'), ((438, 451), 'django.utils.translation.ugettext_lazy', '_', (['"""Отложить"""'], {}), "('Отложить')\n", (439, 451), True, 'from django.utils.translation import ugettext_lazy as _\n'), ((623, 639), 'django.utils.translation.ugettext_lazy', '_', (['"""Исполнитель"""'], {}), "('Исполнитель')\n", (624, 639), True, 'from django.utils.translation import ugettext_lazy as _\n'), ((893, 913), 'django.core.validators.MinValueValidator', 'MinValueValidator', (['(1)'], {}), '(1)\n', (910, 913), False, 'from django.core.validators import MaxValueValidator, MinValueValidator\n'), ((915, 935), 'django.core.validators.MaxValueValidator', 'MaxValueValidator', (['(3)'], {}), '(3)\n', (932, 935), False, 'from django.core.validators import MaxValueValidator, MinValueValidator\n')]