You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
konova/intervention/models.py

91 lines
3.5 KiB
Python

3 years ago
"""
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.contrib.gis.db import models
from django.utils.timezone import now
from intervention.settings import INTERVENTION_IDENTIFIER_LENGTH, INTERVENTION_IDENTIFIER_TEMPLATE
from konova.models import BaseObject
from konova.utils.generators import generate_random_string
from organisation.enums import RoleTypeEnum
from process.models import Process
class Intervention(BaseObject):
"""
Interventions are e.g. construction sites where nature used to be.
A process consists of exactly one intervention and one or more compensation
"""
type = models.CharField(max_length=500, null=True, blank=True)
law = models.CharField(max_length=500, null=True, blank=True)
handler = models.CharField(max_length=500, null=True, blank=True)
data_provider = models.ForeignKey("organisation.Organisation", on_delete=models.SET_NULL, null=True, blank=True)
data_provider_detail = models.CharField(max_length=500, null=True, blank=True)
geometry = models.MultiPolygonField(null=True, blank=True)
process = models.OneToOneField("process.Process", on_delete=models.CASCADE, null=True, blank=True, related_name="intervention")
documents = models.ManyToManyField("konova.Document", blank=True)
def __str__(self):
return "{} by {}".format(self.type, self.handler)
def delete(self, *args, **kwargs):
if self.process is not None:
self.process.delete()
super().delete(*args, **kwargs)
@staticmethod
def __generate_new_identifier() -> str:
""" Generates a new identifier for the intervention object
Returns:
str
"""
curr_month = str(now().month)
curr_year = str(now().year)
rand_str = generate_random_string(
length=INTERVENTION_IDENTIFIER_LENGTH,
only_numbers=True,
)
_str = "{}{}{}".format(curr_month, curr_year, rand_str)
return INTERVENTION_IDENTIFIER_TEMPLATE.format(_str)
def save(self, *args, **kwargs):
if self.identifier is None or len(self.identifier) == 0:
# Create new identifier
new_id = self.__generate_new_identifier()
while Intervention.objects.filter(identifier=new_id).exists():
new_id = self.__generate_new_identifier()
self.identifier = new_id
super().save(*args, **kwargs)
@staticmethod
def get_role_objects(user: User, order_by: str = "-created_on") -> list:
""" Returns objects 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 Intervention.objects.none()
processes = Process.get_role_objects(user, order_by)
interventions = [process.intervention for process in processes]
return interventions