models.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from django.db import models
  2. from django.utils import timezone
  3. from django.conf import settings
  4. from django.contrib.auth.models import Group
  5. import datetime
  6. import textwrap
  7. class TaskList(models.Model):
  8. name = models.CharField(max_length=60, verbose_name='nom')
  9. slug = models.SlugField(null=False, blank=False, unique=True)
  10. groups = models.ManyToManyField(Group, verbose_name='groupe', blank=True)
  11. @property
  12. def completed_task_set(self):
  13. return self.task_set.filter(completed_date__isnull=False)
  14. @property
  15. def uncompleted_task_set(self):
  16. return self.task_set.filter(completed_date__isnull=True)
  17. def __str__(self):
  18. return self.name
  19. class Meta:
  20. ordering = ['name']
  21. verbose_name = 'liste de tâches'
  22. verbose_name_plural = 'listes de tâches'
  23. class Task(models.Model):
  24. title = models.CharField(max_length=140, verbose_name='titre')
  25. task_list = models.ForeignKey(TaskList, on_delete=models.PROTECT)
  26. created_date = models.DateField(default=timezone.now, blank=True, null=True)
  27. due_date = models.DateField(blank=True, null=True, verbose_name='due pour le')
  28. completed_date = models.DateField(blank=True, null=True)
  29. created_by = models.ForeignKey(
  30. settings.AUTH_USER_MODEL,
  31. on_delete=models.PROTECT,
  32. related_name='created_task_set',
  33. verbose_name='créée par',
  34. )
  35. assigned_to = models.ForeignKey(
  36. settings.AUTH_USER_MODEL,
  37. blank=True,
  38. null=True,
  39. on_delete=models.SET_NULL,
  40. related_name='assigned_task_set',
  41. verbose_name='assignée à',
  42. )
  43. note = models.TextField(blank=True, null=True)
  44. priority = models.PositiveIntegerField(blank=True, null=True)
  45. def overdue_status(self):
  46. "Returns whether the Tasks's due date has passed or not."
  47. if self.due_date and datetime.date.today() > self.due_date:
  48. return True
  49. def __str__(self):
  50. return self.title
  51. class Meta:
  52. ordering = ['priority', 'created_date']
  53. verbose_name = 'tâche'
  54. verbose_name_plural = 'tâches'
  55. class TaskComment(models.Model):
  56. author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
  57. task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='comments')
  58. date = models.DateTimeField(default=timezone.now)
  59. body = models.TextField(blank=True)
  60. @property
  61. def snippet(self):
  62. body_snippet = textwrap.shorten(self.body, width=35, placeholder="...")
  63. return "{author} - {snippet}...".format(author=str(self.author), snippet=body_snippet)
  64. def __str__(self):
  65. return self.snippet