2014-12-02 18:53:38 +00:00
|
|
|
from __future__ import unicode_literals
|
2019-04-07 23:11:19 +00:00
|
|
|
|
2014-12-02 18:53:38 +00:00
|
|
|
import datetime
|
2019-04-07 23:11:19 +00:00
|
|
|
import os
|
2019-03-11 07:04:19 +00:00
|
|
|
import textwrap
|
2014-12-02 18:53:38 +00:00
|
|
|
|
2018-03-29 05:51:10 +00:00
|
|
|
from django.conf import settings
|
2016-11-28 21:15:46 +00:00
|
|
|
from django.contrib.auth.models import Group
|
2019-04-07 23:11:19 +00:00
|
|
|
from django.db import DEFAULT_DB_ALIAS, models
|
2019-03-11 07:04:19 +00:00
|
|
|
from django.db.transaction import Atomic, get_connection
|
2018-02-03 08:09:23 +00:00
|
|
|
from django.urls import reverse
|
2018-04-05 07:27:53 +00:00
|
|
|
from django.utils import timezone
|
2014-05-31 16:09:27 +00:00
|
|
|
|
2010-09-27 08:48:00 +00:00
|
|
|
|
2019-04-06 23:30:01 +00:00
|
|
|
def get_attachment_upload_dir(instance, filename):
|
|
|
|
"""Determine upload dir for task attachment files.
|
|
|
|
"""
|
|
|
|
|
|
|
|
return "/".join(["tasks", "attachments", str(instance.task.id), filename])
|
|
|
|
|
|
|
|
|
2019-03-11 07:04:19 +00:00
|
|
|
class LockedAtomicTransaction(Atomic):
|
|
|
|
"""
|
|
|
|
modified from https://stackoverflow.com/a/41831049
|
|
|
|
this is needed for safely merging
|
|
|
|
|
|
|
|
Does a atomic transaction, but also locks the entire table for any transactions, for the duration of this
|
|
|
|
transaction. Although this is the only way to avoid concurrency issues in certain situations, it should be used with
|
|
|
|
caution, since it has impacts on performance, for obvious reasons...
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *models, using=None, savepoint=None):
|
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
super().__init__(using, savepoint)
|
|
|
|
self.models = models
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
super(LockedAtomicTransaction, self).__enter__()
|
|
|
|
|
|
|
|
# Make sure not to lock, when sqlite is used, or you'll run into problems while running tests!!!
|
|
|
|
if settings.DATABASES[self.using]["ENGINE"] != "django.db.backends.sqlite3":
|
|
|
|
cursor = None
|
|
|
|
try:
|
|
|
|
cursor = get_connection(self.using).cursor()
|
|
|
|
for model in self.models:
|
|
|
|
cursor.execute(
|
2019-04-12 07:09:01 +00:00
|
|
|
"LOCK TABLE {table_name}".format(table_name=model._meta.db_table)
|
2019-03-11 07:04:19 +00:00
|
|
|
)
|
|
|
|
finally:
|
|
|
|
if cursor and not cursor.closed:
|
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
|
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)
|
2018-12-21 08:38:44 +00:00
|
|
|
slug = models.SlugField(default="")
|
2018-02-03 08:09:23 +00:00
|
|
|
group = models.ForeignKey(Group, on_delete=models.CASCADE)
|
2014-05-31 16:09:27 +00:00
|
|
|
|
2014-12-02 18:53:38 +00:00
|
|
|
def __str__(self):
|
2010-09-27 08:48:00 +00:00
|
|
|
return self.name
|
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
|
|
|
|
2018-03-27 06:50:14 +00:00
|
|
|
# Prevents (at the database level) creation of two lists with the same slug in the same group
|
2010-09-27 08:48:00 +00:00
|
|
|
unique_together = ("group", "slug")
|
|
|
|
|
|
|
|
|
2018-03-29 05:51:10 +00:00
|
|
|
class Task(models.Model):
|
2010-09-27 08:48:00 +00:00
|
|
|
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)
|
2018-04-05 07:27:53 +00:00
|
|
|
created_date = models.DateField(default=timezone.now, blank=True, null=True)
|
2018-12-21 08:38:44 +00:00
|
|
|
due_date = models.DateField(blank=True, null=True)
|
2018-02-13 07:37:28 +00:00
|
|
|
completed = models.BooleanField(default=False)
|
2014-05-31 16:09:27 +00:00
|
|
|
completed_date = models.DateField(blank=True, null=True)
|
2018-12-21 08:38:44 +00:00
|
|
|
created_by = models.ForeignKey(
|
2019-03-11 07:04:19 +00:00
|
|
|
settings.AUTH_USER_MODEL,
|
|
|
|
null=True,
|
2019-07-30 05:53:33 +00:00
|
|
|
blank=True,
|
2019-03-11 07:04:19 +00:00
|
|
|
related_name="todo_created_by",
|
|
|
|
on_delete=models.CASCADE,
|
2018-12-21 08:38:44 +00:00
|
|
|
)
|
2018-02-03 08:09:23 +00:00
|
|
|
assigned_to = models.ForeignKey(
|
2018-12-21 08:38:44 +00:00
|
|
|
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)
|
2019-03-26 06:19:11 +00:00
|
|
|
priority = models.PositiveIntegerField(blank=True, null=True)
|
2014-05-31 16:09:27 +00:00
|
|
|
|
2016-04-09 18:56: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):
|
2018-03-29 05:51:10 +00:00
|
|
|
"Returns whether the Tasks's due date has passed or not."
|
2014-08-03 13:59:25 +00:00
|
|
|
if self.due_date and datetime.date.today() > self.due_date:
|
2018-02-04 08:35:04 +00:00
|
|
|
return True
|
2010-09-27 08:48:00 +00:00
|
|
|
|
2014-12-02 18:53:38 +00:00
|
|
|
def __str__(self):
|
2010-09-27 08:48:00 +00:00
|
|
|
return self.title
|
2014-05-31 16:09:27 +00:00
|
|
|
|
2014-11-11 07:18:44 +00:00
|
|
|
def get_absolute_url(self):
|
2018-12-21 08:38:44 +00:00
|
|
|
return reverse("todo:task_detail", kwargs={"task_id": self.id})
|
2014-11-11 07:18:44 +00:00
|
|
|
|
2018-03-29 05:51:10 +00:00
|
|
|
# Auto-set the Task creation / completed date
|
2018-03-26 07:37:29 +00:00
|
|
|
def save(self, **kwargs):
|
2018-03-29 05:51:10 +00:00
|
|
|
# If Task 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()
|
2018-03-29 05:51:10 +00:00
|
|
|
super(Task, self).save()
|
2010-09-27 08:48:00 +00:00
|
|
|
|
2019-03-11 07:04:19 +00:00
|
|
|
def merge_into(self, merge_target):
|
|
|
|
if merge_target.pk == self.pk:
|
|
|
|
raise ValueError("can't merge a task with self")
|
|
|
|
|
|
|
|
# lock the comments to avoid concurrent additions of comments after the
|
|
|
|
# update request. these comments would be irremediably lost because of
|
|
|
|
# the cascade clause
|
|
|
|
with LockedAtomicTransaction(Comment):
|
|
|
|
Comment.objects.filter(task=self).update(task=merge_target)
|
|
|
|
self.delete()
|
|
|
|
|
2010-09-27 08:48:00 +00:00
|
|
|
class Meta:
|
2019-03-26 06:19:11 +00:00
|
|
|
ordering = ["priority", "created_date"]
|
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.
|
|
|
|
"""
|
2018-12-21 08:38:44 +00:00
|
|
|
|
2019-03-11 07:04:19 +00:00
|
|
|
author = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, null=True
|
|
|
|
)
|
2018-03-29 05:51:10 +00:00
|
|
|
task = models.ForeignKey(Task, on_delete=models.CASCADE)
|
2010-09-27 08:48:00 +00:00
|
|
|
date = models.DateTimeField(default=datetime.datetime.now)
|
2019-03-11 07:04:19 +00:00
|
|
|
email_from = models.CharField(max_length=320, blank=True, null=True)
|
2019-03-25 14:43:53 +00:00
|
|
|
email_message_id = models.CharField(max_length=255, blank=True, null=True)
|
2019-03-11 07:04:19 +00:00
|
|
|
|
2010-09-27 08:48:00 +00:00
|
|
|
body = models.TextField(blank=True)
|
2014-05-31 16:09:27 +00:00
|
|
|
|
2019-03-11 07:04:19 +00:00
|
|
|
class Meta:
|
|
|
|
# an email should only appear once per task
|
|
|
|
unique_together = ("task", "email_message_id")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def author_text(self):
|
|
|
|
if self.author is not None:
|
|
|
|
return str(self.author)
|
|
|
|
|
|
|
|
assert self.email_message_id is not None
|
|
|
|
return str(self.email_from)
|
|
|
|
|
|
|
|
@property
|
2014-12-02 18:53:38 +00:00
|
|
|
def snippet(self):
|
2019-03-11 07:04:19 +00:00
|
|
|
body_snippet = textwrap.shorten(self.body, width=35, placeholder="...")
|
2014-12-02 18:53:38 +00:00
|
|
|
# Define here rather than in __str__ so we can use it in the admin list_display
|
2019-04-12 07:09:01 +00:00
|
|
|
return "{author} - {snippet}...".format(author=self.author_text, snippet=body_snippet)
|
2014-12-02 18:53:38 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2019-03-11 07:04:19 +00:00
|
|
|
return self.snippet
|
2019-04-06 23:30:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Attachment(models.Model):
|
|
|
|
"""
|
|
|
|
Defines a generic file attachment for use in M2M relation with Task.
|
|
|
|
"""
|
|
|
|
|
|
|
|
task = models.ForeignKey(Task, on_delete=models.CASCADE)
|
2019-04-12 07:09:01 +00:00
|
|
|
added_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
2019-04-06 23:30:01 +00:00
|
|
|
timestamp = models.DateTimeField(default=datetime.datetime.now)
|
|
|
|
file = models.FileField(upload_to=get_attachment_upload_dir, max_length=255)
|
|
|
|
|
2019-04-07 23:11:19 +00:00
|
|
|
def filename(self):
|
|
|
|
return os.path.basename(self.file.name)
|
|
|
|
|
|
|
|
def extension(self):
|
|
|
|
name, extension = os.path.splitext(self.file.name)
|
|
|
|
return extension
|
|
|
|
|
2019-04-06 23:30:01 +00:00
|
|
|
def __str__(self):
|
|
|
|
return f"{self.task.id} - {self.file.name}"
|