131 lines
4.3 KiB
Python
131 lines
4.3 KiB
Python
from django.db import models
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
|
class Process(models.Model):
|
|
"""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)
|
|
|
|
def percentage_of_completion(self):
|
|
"""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'
|
|
"""
|
|
total = self.tasks.count()
|
|
if total:
|
|
return self.tasks.filter(status=3).count() / self.tasks.count() * 100
|
|
return 'NaN'
|
|
|
|
def __str__(self):
|
|
"""Returns the current document version on which the process applies."""
|
|
return 'Process on {}'.format(self.document_version)
|
|
|
|
def create_task(self, assigned_to):
|
|
"""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):
|
|
"""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')
|
|
)
|
|
|
|
|
|
class Task(models.Model):
|
|
assigned_to = models.ForeignKey(User, related_name='tasks')
|
|
process = models.ForeignKey(Process, related_name='tasks')
|
|
status = models.IntegerField(choices=TASK_STATUS)
|
|
|
|
def __str__(self):
|
|
return '{} - {}'.format(self.process, self.assigned_to)
|
|
|
|
class Meta:
|
|
unique_together = ('assigned_to', 'process', 'status')
|
|
|
|
def save(self, *args, **kwargs):
|
|
super().save()
|
|
|
|
if self.process.percentage_of_completion == 100.0:
|
|
self.process.finalize()
|
|
|
|
|
|
class PublishedProcessMixin(object):
|
|
"""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):
|
|
"""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):
|
|
"""A review process is a process that will apply when a document has been published."""
|
|
PROCESS_TYPE = 'Review'
|
|
|
|
def __str__(self):
|
|
return 'Review: {}'.format(self.document_version)
|
|
|
|
|
|
class Approval(Process, DraftProcessMixin):
|
|
"""Approval process, to published a draft version and make it accessible to other people."""
|
|
PROCESS_TYPE = 'Approval'
|
|
|
|
def save(self, *args, **kwargs):
|
|
super().save()
|
|
for validator in self.document_version.validators.all():
|
|
self.create_task(validator)
|
|
|
|
def finalize(self):
|
|
super().finalize()
|
|
self.document_version.is_published = True
|
|
self.document_version.major = self.document_version.major + 1
|
|
self.document_version.revision = 0
|
|
self.document_version.save()
|
|
|
|
|
|
class GatherComments(Process, DraftProcessMixin):
|
|
PROCESS_TYPE = 'GatherComments'
|
|
|
|
def save(self, *args, **kwargs):
|
|
super().save()
|
|
for reviewer in self.document_version.reviewers.all():
|
|
self.create_task(reviewer)
|