coins-demo/todo/models.py

86 lines
3.1 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
import datetime
2010-09-27 08:48:00 +00:00
from django.db import models
from django.contrib.auth.models import Group
from django.urls import reverse
from django.utils.encoding import python_2_unicode_compatible
from django.conf import settings
2014-05-31 16:09:27 +00:00
2010-09-27 08:48:00 +00:00
@python_2_unicode_compatible
2018-02-10 08:25:28 +00:00
class TaskList(models.Model):
2010-09-27 08:48:00 +00:00
name = models.CharField(max_length=60)
slug = models.SlugField(default='',)
group = models.ForeignKey(Group, on_delete=models.CASCADE)
2014-05-31 16:09:27 +00:00
def __str__(self):
2010-09-27 08:48:00 +00:00
return self.name
2014-05-31 16:09:27 +00:00
def list_detail(self):
2010-09-27 08:48:00 +00:00
# Count all incomplete tasks on the current list instance
2018-02-10 08:25:28 +00:00
return Item.objects.filter(task_list=self, completed=0)
2014-05-31 16:09:27 +00:00
2010-09-27 08:48:00 +00:00
class Meta:
2014-05-31 16:09:27 +00:00
ordering = ["name"]
2018-02-10 08:25:28 +00:00
verbose_name_plural = "Task Lists"
2014-05-31 16:09:27 +00:00
2010-09-27 08:48:00 +00:00
# Prevents (at the database level) creation of two lists with the same name in the same group
unique_together = ("group", "slug")
@python_2_unicode_compatible
2010-09-27 08:48:00 +00:00
class Item(models.Model):
title = models.CharField(max_length=140)
2018-02-10 08:25:28 +00:00
task_list = models.ForeignKey(TaskList, on_delete=models.CASCADE, null=True)
created_date = models.DateField(auto_now=True)
2014-05-31 16:09:27 +00:00
due_date = models.DateField(blank=True, null=True, )
completed = models.BooleanField(default=False)
2014-05-31 16:09:27 +00:00
completed_date = models.DateField(blank=True, null=True)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='todo_created_by', on_delete=models.CASCADE)
assigned_to = models.ForeignKey(
settings.AUTH_USER_MODEL, blank=True, null=True, related_name='todo_assigned_to', on_delete=models.CASCADE)
2014-05-31 16:09:27 +00:00
note = models.TextField(blank=True, null=True)
priority = models.PositiveIntegerField()
2014-05-31 16:09:27 +00:00
# Has due date for an instance of this object passed?
2010-09-27 08:48:00 +00:00
def overdue_status(self):
"Returns whether the item's due date has passed or not."
if self.due_date and datetime.date.today() > self.due_date:
return True
2010-09-27 08:48:00 +00:00
def __str__(self):
2010-09-27 08:48:00 +00:00
return self.title
2014-05-31 16:09:27 +00:00
def get_absolute_url(self):
return reverse('todo:task_detail', kwargs={'task_id': self.id, })
2010-09-27 08:48:00 +00:00
# Auto-set the item creation / completed date
def save(self, **kwargs):
2010-09-27 08:48:00 +00:00
# If Item is being marked complete, set the completed_date
2014-05-31 16:09:27 +00:00
if self.completed:
2010-09-27 08:48:00 +00:00
self.completed_date = datetime.datetime.now()
super(Item, self).save()
class Meta:
2014-05-31 16:09:27 +00:00
ordering = ["priority"]
2010-09-27 08:48:00 +00:00
2014-05-31 16:09:27 +00:00
@python_2_unicode_compatible
2014-05-31 16:09:27 +00:00
class Comment(models.Model):
2010-09-27 08:48:00 +00:00
"""
2014-05-31 16:09:27 +00:00
Not using Django's built-in comments because we want to be able to save
2010-09-27 08:48:00 +00:00
a comment and change task details at the same time. Rolling our own since it's easy.
"""
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
task = models.ForeignKey(Item, on_delete=models.CASCADE)
2010-09-27 08:48:00 +00:00
date = models.DateTimeField(default=datetime.datetime.now)
body = models.TextField(blank=True)
2014-05-31 16:09:27 +00:00
def snippet(self):
# Define here rather than in __str__ so we can use it in the admin list_display
return "{author} - {snippet}...".format(author=self.author, snippet=self.body[:35])
def __str__(self):
return self.snippet