konova/compensation/models.py

137 lines
5.4 KiB
Python
Raw Normal View History

2021-07-01 13:36:07 +02:00
"""
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
2021-07-01 13:36:07 +02:00
from django.contrib.gis.db import models
2021-07-01 15:08:22 +02:00
from django.core.validators import MinValueValidator
from django.utils import timezone
2021-07-01 13:36:07 +02:00
from django.utils.timezone import now
from compensation.settings import COMPENSATION_IDENTIFIER_LENGTH, COMPENSATION_IDENTIFIER_TEMPLATE
2021-07-01 14:38:57 +02:00
from konova.models import BaseObject, BaseResource, Geometry
2021-07-01 13:36:07 +02:00
from konova.utils.generators import generate_random_string
2021-07-01 14:38:57 +02:00
from organisation.models import Organisation
2021-07-01 13:36:07 +02:00
2021-07-01 15:08:22 +02:00
class Payment(BaseResource):
"""
Holds data on a payment for an intervention (alternative to a classic compensation)
"""
amount = models.FloatField(validators=[MinValueValidator(limit_value=0.00)])
due_on = models.DateField(null=True)
comment = models.CharField(
max_length=1000,
null=True,
blank=True,
help_text="Refers to german money transfer 'Verwendungszweck'",
)
2021-07-01 15:08:22 +02:00
2021-07-01 13:36:07 +02:00
class CompensationControl(BaseResource):
"""
Holds data on how a compensation shall be controlled
"""
deadline = models.ForeignKey("konova.Deadline", on_delete=models.SET_NULL, null=True, blank=True)
type = models.CharField(max_length=500, null=True, blank=True)
expected_result = models.CharField(max_length=500, null=True, blank=True, help_text="The expected outcome, that needs to be controlled")
2021-07-01 14:38:57 +02:00
by_authority = models.ForeignKey(Organisation, null=True, blank=True, on_delete=models.SET_NULL)
2021-07-01 13:36:07 +02:00
comment = models.TextField()
class CompensationState(models.Model):
"""
Compensations must define the state of an area before and after the compensation.
"""
biotope_type = models.CharField(max_length=500, null=True, blank=True)
2021-07-01 14:38:57 +02:00
surface = models.FloatField()
2021-07-01 13:36:07 +02:00
class CompensationAction(BaseResource):
"""
Compensations include actions like planting trees, refreshing rivers and so on.
"""
action_type = models.CharField(max_length=500, null=True, blank=True)
amount = models.FloatField()
unit = models.CharField(max_length=100, null=True, blank=True)
control = models.ForeignKey(CompensationControl, on_delete=models.SET_NULL, null=True, blank=True)
class Compensation(BaseObject):
"""
The compensation holds information about which actions have to be performed until which date, who is in charge
of this, which legal authority is the point of contact, and so on.
"""
2021-07-01 15:08:22 +02:00
registration_office = models.ForeignKey(Organisation, on_delete=models.SET_NULL, null=True, related_name="+")
conservation_office = models.ForeignKey(Organisation, on_delete=models.SET_NULL, null=True, related_name="+")
2021-07-01 14:38:57 +02:00
2021-07-01 13:36:07 +02:00
ground_definitions = models.CharField(max_length=500, null=True, blank=True) # ToDo: Need to be M2M to laws!
action_definitions = models.CharField(max_length=500, null=True, blank=True) # ToDo: Need to be M2M to laws!
2021-07-01 14:38:57 +02:00
before_states = models.ManyToManyField(CompensationState, blank=True, related_name='+')
after_states = models.ManyToManyField(CompensationState, blank=True, related_name='+')
2021-07-01 13:36:07 +02:00
actions = models.ManyToManyField(CompensationAction)
2021-07-01 14:38:57 +02:00
2021-07-01 15:08:22 +02:00
deadline_creation = models.ForeignKey("konova.Deadline", on_delete=models.SET_NULL, null=True, blank=True, related_name="+")
deadline_maintaining = models.ForeignKey("konova.Deadline", on_delete=models.SET_NULL, null=True, blank=True, related_name="+")
2021-07-01 13:36:07 +02:00
2021-07-01 14:38:57 +02:00
geometry = models.ForeignKey(Geometry, null=True, blank=True, on_delete=models.SET_NULL)
documents = models.ManyToManyField("konova.Document", blank=True)
2021-07-01 13:36:07 +02:00
# Users having access on this object
users = models.ManyToManyField(User)
2021-07-01 13:36:07 +02:00
@staticmethod
2021-07-01 15:08:22 +02:00
def _generate_new_identifier() -> str:
2021-07-01 13:36:07 +02:00
""" 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=COMPENSATION_IDENTIFIER_LENGTH,
only_numbers=True,
)
_str = "{}{}{}".format(curr_month, curr_year, rand_str)
return COMPENSATION_IDENTIFIER_TEMPLATE.format(_str)
2021-07-01 15:08:22 +02:00
def delete(self, *args, **kwargs):
""" Custom delete functionality
Does not delete from database but sets a timestamp for being deleted on and which user deleted the object
Args:
*args ():
**kwargs ():
Returns:
"""
_now = timezone.now()
_user = kwargs.get("user", None)
self.deleted_on = _now
self.deleted_by = _user
self.save()
2021-07-01 13:36:07 +02:00
def save(self, *args, **kwargs):
if self.identifier is None or len(self.identifier) == 0:
# Create new identifier
2021-07-01 15:08:22 +02:00
new_id = self._generate_new_identifier()
2021-07-01 13:36:07 +02:00
while Compensation.objects.filter(identifier=new_id).exists():
2021-07-01 15:08:22 +02:00
new_id = self._generate_new_identifier()
2021-07-01 13:36:07 +02:00
self.identifier = new_id
super().save(*args, **kwargs)
2021-07-01 14:38:57 +02:00
class EcoAccount(Compensation):
2021-07-01 13:36:07 +02:00
"""
An eco account is a kind of 'prepaid' compensation. It can be compared to an account that already has been filled
with some kind of currency. From this account one is able to 'withdraw' currency for current projects.
"""
2021-07-01 14:38:57 +02:00
handler = models.CharField(max_length=500, null=True, blank=True, help_text="Who is responsible for handling the actions")