konova/process/models.py
2021-07-01 13:36:07 +02:00

159 lines
5.3 KiB
Python

"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 17.11.20
"""
from django.contrib.auth.models import User
from django.db import models, transaction
from konova.models import BaseResource
from organisation.enums import RoleTypeEnum
from organisation.models import Organisation
from process.enums import ProcessStateEnum
from process.settings import PROCESS_STATE_STRINGS
class Process(BaseResource):
"""
Process links compensation, intervention and eco accounts. These links are realized as ForeignKeys in their models.
This process model therefore just holds further information on participated legal organizations.
Attribute 'state' holds information on the current state of the process, which are defined in ProcessStateEnum
"""
licensing_authority = models.ForeignKey(Organisation, on_delete=models.SET_NULL, null=True, blank=True, related_name="+")
licensing_authority_document_identifier = models.CharField(max_length=500, null=True, blank=True)
licensing_authority_comment = models.CharField(max_length=500, null=True, blank=True)
registration_office_document_identifier = models.CharField(max_length=500, null=True, blank=True)
registration_office = models.ForeignKey(Organisation, on_delete=models.SET_NULL, null=True, blank=True, related_name="+")
registration_office_comment = models.CharField(max_length=500, null=True, blank=True)
state = models.PositiveIntegerField(choices=ProcessStateEnum.as_choices(drop_empty_choice=True), default=0)
def __str__(self) -> str:
try:
intervention = self.intervention
title = intervention.title
except AttributeError:
title = "NO TITLE"
return title
def get_state_str(self):
"""
Translates the numeric state into a string
Returns:
"""
return PROCESS_STATE_STRINGS.get(self.state, None)
def deactivate(self):
""" Deactivates a process and it's related elements
Returns:
"""
if self.is_active and not self.is_deleted:
self.toggle_deletion()
def activate(self):
""" Activates a process and it's related elements
Returns:
"""
if not self.is_active and self.is_deleted:
self.toggle_deletion()
def toggle_deletion(self):
""" Enables or disables a process
Processes are not truly removed from the database, just toggled in their flags 'is_active' and 'is_deleted'
Returns:
"""
with transaction.atomic():
self.is_active = not self.is_active
self.is_deleted = not self.is_deleted
self.save()
# toggle related elements
comps = self.compensation.all()
elements = list(comps)
if self.intervention is not None:
elements.append(self.intervention)
for elem in elements:
elem.is_active = self.is_active
elem.is_deleted = self.is_deleted
elem.save()
@staticmethod
def get_role_objects(user: User, order_by: str = "-created_on"):
""" Returns processes depending on the currently selected role of the user
* REGISTRATIONOFFICE
* User can see the processes where registration_office is set to the organisation of the currently selected role
* User can see self-created processes
* LICENSINGOFFICE
* same
* DATAPROVIDER
* User can see only self-created processes
Args:
user (User): The performing user
order_by (str): Order by which Process attribute
Returns:
"""
role = user.current_role
if role is None:
return Process.objects.none()
_filter = {
"is_deleted": False,
}
if role.role.type == RoleTypeEnum.REGISTRATIONOFFICE.value:
_filter["registration_office"] = role.organisation
elif role.role.type == RoleTypeEnum.LICENSINGAUTHORITY.value:
_filter["licensing_authority"] = role.organisation
elif role.role.type == RoleTypeEnum.DATAPROVIDER.value:
# Nothing special
_filter["created_by"] = user
else:
# None of the above
pass
other_processes = Process.objects.filter(
**_filter
)
user_created_processes = Process.objects.filter(
created_by=user,
is_deleted=False,
)
qs = (other_processes | user_created_processes).distinct()
qs = qs.order_by(order_by)
return qs
@staticmethod
def create_from_intervention(intervention):
""" Creates a process for an intervention, in case an intervention has been created without a process
Args:
intervention (Intervention): The intervention
Returns:
process (Process)
"""
process = Process()
process.identifier = intervention.identifier
process.title = intervention.title
process.created_by = intervention.created_by
process.save()
intervention.process = process
intervention.save()
return process