coins-demo/todo/models.py

86 lines
2.7 KiB
Python
Raw Normal View History

2010-09-27 08:48:00 +00:00
from django.db import models
2014-05-31 16:09:27 +00:00
from django.contrib.auth.models import User, Group
2010-09-27 08:48:00 +00:00
from django.template.defaultfilters import slugify
2014-05-31 16:09:27 +00:00
import datetime
2010-09-27 08:48:00 +00:00
class List(models.Model):
name = models.CharField(max_length=60)
2014-05-31 16:09:27 +00:00
slug = models.SlugField(max_length=60, editable=False)
2010-09-27 08:48:00 +00:00
# slug = models.SlugField(max_length=60)
group = models.ForeignKey(Group)
2014-05-31 16:09:27 +00:00
2010-09-27 08:48:00 +00:00
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.name)
super(List, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
2014-05-31 16:09:27 +00:00
2010-09-27 08:48:00 +00:00
# Custom manager lets us do things like Item.completed_tasks.all()
objects = models.Manager()
2014-05-31 16:09:27 +00:00
2010-09-27 08:48:00 +00:00
def incomplete_tasks(self):
# Count all incomplete tasks on the current list instance
2014-05-31 16:09:27 +00:00
return Item.objects.filter(list=self, completed=0)
2010-09-27 08:48:00 +00:00
class Meta:
2014-05-31 16:09:27 +00:00
ordering = ["name"]
2010-09-27 08:48:00 +00:00
verbose_name_plural = "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")
class Item(models.Model):
title = models.CharField(max_length=140)
list = models.ForeignKey(List)
2011-09-07 10:48:34 +00:00
created_date = models.DateField(auto_now=True, auto_now_add=True)
2014-05-31 16:09:27 +00:00
due_date = models.DateField(blank=True, null=True, )
2010-09-27 08:48:00 +00:00
completed = models.BooleanField()
2014-05-31 16:09:27 +00:00
completed_date = models.DateField(blank=True, null=True)
created_by = models.ForeignKey(User, related_name='todo_created_by')
2010-09-27 08:48:00 +00:00
assigned_to = models.ForeignKey(User, related_name='todo_assigned_to')
2014-05-31 16:09:27 +00:00
note = models.TextField(blank=True, null=True)
2010-09-27 08:48:00 +00:00
priority = models.PositiveIntegerField(max_length=3)
2014-05-31 16:09:27 +00:00
2010-09-27 08:48:00 +00:00
# Model method: Has due date for an instance of this object passed?
def overdue_status(self):
"Returns whether the item's due date has passed or not."
2014-05-31 16:09:27 +00:00
if datetime.date.today() > self.due_date:
2010-09-27 08:48:00 +00:00
return 1
def __unicode__(self):
return self.title
2014-05-31 16:09:27 +00:00
2010-09-27 08:48:00 +00:00
# Auto-set the item creation / completed date
def save(self):
# 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
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(User)
task = models.ForeignKey(Item)
date = models.DateTimeField(default=datetime.datetime.now)
body = models.TextField(blank=True)
2014-05-31 16:09:27 +00:00
def __unicode__(self):
2010-09-27 08:48:00 +00:00
return '%s - %s' % (
2014-05-31 16:09:27 +00:00
self.author,
self.date,
)