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

73 lines
2.9 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, Geometry
3 years ago
from konova.utils.generators import generate_random_string
from organisation.models import Organisation
3 years ago
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
"""
registration_office = models.ForeignKey(Organisation, on_delete=models.SET_NULL, null=True)
registration_file_number = models.CharField(max_length=1000, blank=True, null=True)
registration_date = models.DateTimeField(null=True, blank=True)
conservation_office = models.ForeignKey(Organisation, on_delete=models.SET_NULL, null=True)
conservations_file_number = models.CharField(max_length=1000, blank=True, null=True)
process_type = models.CharField(max_length=500, null=True, blank=True)
3 years ago
law = models.CharField(max_length=500, null=True, blank=True)
handler = models.CharField(max_length=500, null=True, blank=True)
geometry = models.ForeignKey(Geometry, null=True, blank=True, on_delete=models.SET_NULL)
3 years ago
documents = models.ManyToManyField("konova.Document", blank=True)
# Refers to "verzeichnen"
recorded_on = models.DateTimeField(default=None)
recorded_by = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL)
# Holds which intervention is simply a newer version of this dataset
next_version = models.ForeignKey("Intervention", null=True, on_delete=models.DO_NOTHING)
3 years ago
def __str__(self):
return "{} by {}".format(self.type, self.handler)
def delete(self, *args, **kwargs):
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)