2012-12-21 17:26:35 -08:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.contrib.sites.models import Site
|
2019-06-13 05:00:19 -07:00
|
|
|
from django.core.mail import EmailMessage
|
2012-12-21 17:26:35 -08:00
|
|
|
from django.db import models
|
2023-11-02 04:52:30 -07:00
|
|
|
from django.db.models.signals import post_save, pre_save
|
2019-06-13 05:00:19 -07:00
|
|
|
from django.template import loader
|
2012-12-21 17:26:35 -08:00
|
|
|
|
2023-11-02 04:52:30 -07:00
|
|
|
from main.models import Arch, Package, Repo
|
2012-12-21 17:26:35 -08:00
|
|
|
from main.utils import set_created_field
|
|
|
|
|
|
|
|
|
|
|
|
class Todolist(models.Model):
|
2021-01-12 14:21:00 -08:00
|
|
|
REBUILD = 0
|
|
|
|
TASK = 1
|
|
|
|
|
|
|
|
KIND_CHOICES = (
|
|
|
|
(REBUILD, 'Rebuild'),
|
|
|
|
(TASK, 'Task'),
|
|
|
|
)
|
|
|
|
|
2012-12-28 07:42:17 -08:00
|
|
|
slug = models.SlugField(max_length=255, unique=True)
|
2012-12-21 17:26:35 -08:00
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
description = models.TextField()
|
2020-11-08 08:52:59 -08:00
|
|
|
creator = models.ForeignKey(User, on_delete=models.PROTECT, related_name="created_todolists")
|
2012-12-21 17:26:35 -08:00
|
|
|
created = models.DateTimeField(db_index=True)
|
2021-01-12 14:21:00 -08:00
|
|
|
kind = models.SmallIntegerField(default=REBUILD, choices=KIND_CHOICES, help_text='(Rebuild for soname bumps, Task for independent tasks)')
|
2012-12-21 17:26:35 -08:00
|
|
|
last_modified = models.DateTimeField(editable=False)
|
|
|
|
raw = models.TextField(blank=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
get_latest_by = 'created'
|
|
|
|
|
2018-04-22 13:14:23 -07:00
|
|
|
def __str__(self):
|
2012-12-21 17:26:35 -08:00
|
|
|
return self.name
|
|
|
|
|
2015-06-05 06:15:32 -07:00
|
|
|
@property
|
|
|
|
def stripped_description(self):
|
|
|
|
return self.description.strip()
|
|
|
|
|
2012-12-21 17:26:35 -08:00
|
|
|
def get_absolute_url(self):
|
2012-12-28 08:06:32 -08:00
|
|
|
return '/todo/%s/' % self.slug
|
2012-12-21 17:26:35 -08:00
|
|
|
|
|
|
|
def get_full_url(self, proto='https'):
|
|
|
|
'''get a URL suitable for things like email including the domain'''
|
|
|
|
domain = Site.objects.get_current().domain
|
|
|
|
return '%s://%s%s' % (proto, domain, self.get_absolute_url())
|
|
|
|
|
2012-12-27 21:32:05 -08:00
|
|
|
def packages(self):
|
|
|
|
if not hasattr(self, '_packages'):
|
2012-12-31 09:31:35 -08:00
|
|
|
self._packages = self.todolistpackage_set.filter(
|
2020-10-03 21:59:41 -07:00
|
|
|
removed__isnull=True).select_related(
|
|
|
|
'pkg', 'repo', 'arch', 'user__userprofile').order_by(
|
|
|
|
'pkgname', 'arch')
|
2012-12-27 21:32:05 -08:00
|
|
|
return self._packages
|
|
|
|
|
2021-01-12 14:21:00 -08:00
|
|
|
@property
|
|
|
|
def kind_str(self):
|
|
|
|
'''Todo list kind as str'''
|
|
|
|
return self.KIND_CHOICES[self.kind][1]
|
|
|
|
|
2012-12-21 17:26:35 -08:00
|
|
|
|
|
|
|
class TodolistPackage(models.Model):
|
|
|
|
INCOMPLETE = 0
|
|
|
|
COMPLETE = 1
|
|
|
|
IN_PROGRESS = 2
|
|
|
|
STATUS_CHOICES = (
|
|
|
|
(INCOMPLETE, 'Incomplete'),
|
|
|
|
(COMPLETE, 'Complete'),
|
|
|
|
(IN_PROGRESS, 'In-progress'),
|
|
|
|
)
|
|
|
|
|
2018-10-21 11:49:38 -07:00
|
|
|
todolist = models.ForeignKey(Todolist, on_delete=models.CASCADE)
|
2012-12-21 17:26:35 -08:00
|
|
|
pkg = models.ForeignKey(Package, null=True, on_delete=models.SET_NULL)
|
|
|
|
pkgname = models.CharField(max_length=255)
|
|
|
|
pkgbase = models.CharField(max_length=255)
|
2018-10-21 11:49:38 -07:00
|
|
|
arch = models.ForeignKey(Arch, on_delete=models.CASCADE)
|
|
|
|
repo = models.ForeignKey(Repo, on_delete=models.CASCADE)
|
2013-01-12 14:33:31 -08:00
|
|
|
created = models.DateTimeField(editable=False)
|
|
|
|
last_modified = models.DateTimeField(editable=False)
|
2012-12-31 07:52:28 -08:00
|
|
|
removed = models.DateTimeField(null=True, blank=True)
|
2020-11-08 08:52:59 -08:00
|
|
|
status = models.SmallIntegerField(default=INCOMPLETE, choices=STATUS_CHOICES)
|
2012-12-21 17:26:35 -08:00
|
|
|
user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
|
|
|
|
comments = models.TextField(null=True, blank=True)
|
|
|
|
|
|
|
|
class Meta:
|
2012-12-31 08:24:09 -08:00
|
|
|
unique_together = (('todolist', 'pkgname', 'arch'),)
|
2012-12-31 07:52:28 -08:00
|
|
|
get_latest_by = 'created'
|
2012-12-21 17:26:35 -08:00
|
|
|
|
2018-04-22 13:14:23 -07:00
|
|
|
def __str__(self):
|
2012-12-21 17:26:35 -08:00
|
|
|
return self.pkgname
|
|
|
|
|
|
|
|
def status_css_class(self):
|
|
|
|
return self.get_status_display().lower().replace('-', '')
|
|
|
|
|
2020-11-08 08:07:25 -08:00
|
|
|
@property
|
|
|
|
def status_str(self):
|
|
|
|
return self.STATUS_CHOICES[self.status][1]
|
|
|
|
|
2012-12-21 17:26:35 -08:00
|
|
|
|
2019-06-13 05:00:19 -07:00
|
|
|
def check_todolist_complete(sender, instance, **kwargs):
|
|
|
|
if instance.status == instance.INCOMPLETE:
|
|
|
|
return
|
|
|
|
|
|
|
|
query = TodolistPackage.objects.filter(todolist=instance.todolist, status__exact=instance.INCOMPLETE)
|
|
|
|
if query.count() > 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Send e-mail notification
|
|
|
|
subject = "The last package on the TODO list '%s' has been completed." % instance.todolist.name
|
|
|
|
tmpl = loader.get_template('todolists/complete_email_notification.txt')
|
|
|
|
toemail = [instance.todolist.creator.email]
|
|
|
|
ctx = {
|
|
|
|
'todolist': instance.todolist,
|
|
|
|
}
|
2020-11-08 08:52:59 -08:00
|
|
|
msg = EmailMessage(subject, tmpl.render(ctx), 'Arch Website Notification <nobody@archlinux.org>', toemail)
|
2019-06-13 05:00:19 -07:00
|
|
|
msg.send(fail_silently=True)
|
|
|
|
|
|
|
|
|
2020-11-08 08:52:59 -08:00
|
|
|
pre_save.connect(set_created_field, sender=Todolist, dispatch_uid="todolists.models")
|
|
|
|
pre_save.connect(set_created_field, sender=TodolistPackage, dispatch_uid="todolists.models")
|
2012-12-21 17:26:35 -08:00
|
|
|
|
2020-11-08 08:52:59 -08:00
|
|
|
post_save.connect(check_todolist_complete, sender=TodolistPackage, dispatch_uid='todolist.models')
|
2019-06-13 05:00:19 -07:00
|
|
|
|
2012-12-21 17:26:35 -08:00
|
|
|
# vim: set ts=4 sw=4 et:
|