dms/process/models.py

131 lines
4.1 KiB
Python
Raw Normal View History

2017-09-20 15:34:03 +02:00
from django.db import models
2017-09-21 21:18:15 +02:00
from django.contrib.auth.models import User
2017-09-20 15:34:03 +02:00
class Process(models.Model):
2017-11-16 22:21:58 +01:00
"""A process is a group of tasks that needs to be completed by several users.
Attributes:
document_version
created_at (datetime - auto)
"""
document_version = models.ForeignKey('dms.Version', related_name='processes')
created_at = models.DateTimeField(auto_now_add=True)
2017-09-20 15:34:03 +02:00
2017-09-21 21:18:15 +02:00
def percentage_of_completion(self):
2017-11-16 22:21:58 +01:00
"""Defines how much the current has progressed.
At 100%, the process is considered finished.
Returns:
The number of status where status = 3 'Completed', divided by the total number of tasks, times 100.
If there are no tasks associated to the current process, we simply return 'NaN'
"""
2017-09-21 21:18:15 +02:00
total = self.tasks.count()
if total:
return self.tasks.filter(status=3).count() / self.tasks.count() * 100
return 'NaN'
2017-09-20 15:34:03 +02:00
def __str__(self):
2017-11-16 22:21:58 +01:00
"""Returns the current document version on which the process applies."""
2017-10-27 23:00:30 +02:00
return 'Process on {}'.format(self.document_version)
def create_task(self, assigned_to):
2017-10-30 14:57:20 +01:00
"""Create a new task associated to this process.
The new task has the following default values:
1. Status = 1
2. assigned_to = the user passed as a parameter
3. process = self
:returns
A new task if none existed before;
The task with status = 1, assigned_to=User, process=self if it already exists.
"""
task, created = Task.objects.get_or_create(status=1, assigned_to=assigned_to, process=self)
if created:
task.save()
def allow_tasks_delegation(self):
2017-10-30 14:57:20 +01:00
"""By default, the user who should do the task can delegate to another user.
In case this functionality is not desired, this function should be overridden.
"""
return True
TASK_STATUS = (
(1, 'Created'),
(3, 'Completed')
)
2017-09-20 15:34:03 +02:00
class Task(models.Model):
2017-09-21 21:18:15 +02:00
assigned_to = models.ForeignKey(User)
process = models.ForeignKey(Process, related_name='tasks')
status = models.IntegerField(choices=TASK_STATUS)
2017-09-20 15:34:03 +02:00
def __str__(self):
return '{} - {}'.format(self.process, self.assigned_to)
2017-09-21 21:18:15 +02:00
class Meta:
unique_together = ('assigned_to', 'process', 'status')
2017-10-27 23:00:30 +02:00
def save(self, *args, **kwargs):
super().save()
if self.process.percentage_of_completion == 100.0:
self.process.finalize()
2017-09-20 15:34:03 +02:00
class PublishedProcessMixin(object):
2017-11-16 22:21:58 +01:00
"""Defines a specific mixin that only applies on published version"""
def save(self, *args, **kwargs):
if self.document_version.status == 'Draft':
raise TypeError('Review flow cant apply to draft documents')
return super().save(*args, **kwargs)
class DraftProcessMixin(object):
2017-11-16 22:21:58 +01:00
"""Defines a specific mixin that only applies on draft version."""
def save(self, *args, **kwargs):
if self.document_version.status == 'Published':
raise TypeError('Review flow cant apply to published documents')
return super().save(*args, **kwargs)
class Review(Process, PublishedProcessMixin):
2017-11-16 22:21:58 +01:00
"""A review process is a process that will apply when a document has been published."""
2017-09-20 15:34:03 +02:00
PROCESS_TYPE = 'Review'
def __str__(self):
return 'Review: {}'.format(self.document_version)
class Approval(Process, DraftProcessMixin):
2017-11-16 22:21:58 +01:00
"""Approval process, to published a draft version and make it accessible to other people."""
2017-09-20 15:34:03 +02:00
PROCESS_TYPE = 'Approval'
2017-09-21 21:18:15 +02:00
def save(self, *args, **kwargs):
super().save()
for validator in self.document_version.validators.all():
self.create_task(validator)
2017-10-27 23:00:30 +02:00
def finalize(self):
2017-10-30 14:57:20 +01:00
super().finalize()
2017-10-27 23:00:30 +02:00
self.document_version.is_published = True
self.document_version.major = self.document_version.major + 1
self.document_version.revision = 0
self.document_version.save()
2017-09-20 15:34:03 +02:00
class GatherComments(Process, DraftProcessMixin):
2017-09-20 15:34:03 +02:00
PROCESS_TYPE = 'GatherComments'
2017-09-21 21:18:15 +02:00
def save(self, *args, **kwargs):
super().save()
for reviewer in self.document_version.reviewers.all():
self.create_task(reviewer)