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
|
|
|
|
|
|
|
|
"""
|
2021-07-21 15:40:34 +02:00
|
|
|
from django.contrib.auth.models import User
|
2021-07-01 13:36:07 +02:00
|
|
|
from django.contrib.gis.db import models
|
2021-08-02 10:14:34 +02:00
|
|
|
from django.core.validators import MinValueValidator, MaxValueValidator
|
2021-08-02 11:52:20 +02:00
|
|
|
from django.db import transaction
|
2021-07-01 15:08:22 +02:00
|
|
|
from django.utils import timezone
|
2021-07-01 13:36:07 +02:00
|
|
|
from django.utils.timezone import now
|
2021-08-04 10:44:02 +02:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-07-01 13:36:07 +02:00
|
|
|
|
|
|
|
from compensation.settings import COMPENSATION_IDENTIFIER_LENGTH, COMPENSATION_IDENTIFIER_TEMPLATE
|
2021-07-30 12:20:23 +02:00
|
|
|
from intervention.models import Intervention, ResponsibilityData
|
|
|
|
from konova.models import BaseObject, BaseResource, Geometry, UuidModel
|
2021-07-01 13:36:07 +02:00
|
|
|
from konova.utils.generators import generate_random_string
|
2021-08-09 14:39:36 +02:00
|
|
|
from user.models import UserActionLogEntry
|
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)
|
2021-07-22 14:58:58 +02:00
|
|
|
comment = models.CharField(
|
|
|
|
max_length=1000,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
help_text="Refers to german money transfer 'Verwendungszweck'",
|
|
|
|
)
|
2021-07-23 09:36:43 +02:00
|
|
|
intervention = models.ForeignKey(
|
|
|
|
Intervention,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name='payments'
|
|
|
|
)
|
2021-07-01 15:08:22 +02:00
|
|
|
|
|
|
|
|
2021-08-03 13:13:01 +02:00
|
|
|
class CompensationState(UuidModel):
|
2021-07-01 13:36:07 +02:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
2021-08-03 13:13:01 +02:00
|
|
|
def __str__(self):
|
|
|
|
return "{} | {} m²".format(self.biotope_type, self.surface)
|
|
|
|
|
2021-07-01 13:36:07 +02:00
|
|
|
|
2021-08-04 10:44:02 +02:00
|
|
|
class UnitChoices(models.TextChoices):
|
|
|
|
"""
|
|
|
|
Predefines units for selection
|
|
|
|
"""
|
|
|
|
cm = "cm", _("cm")
|
|
|
|
m = "m", _("m")
|
|
|
|
km = "km", _("km")
|
|
|
|
qm = "qm", _("m²")
|
|
|
|
ha = "ha", _("ha")
|
|
|
|
st = "pcs", _("Pieces") # pieces
|
|
|
|
|
|
|
|
|
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()
|
2021-08-04 10:44:02 +02:00
|
|
|
unit = models.CharField(max_length=100, null=True, blank=True, choices=UnitChoices.choices)
|
2021-08-04 11:56:56 +02:00
|
|
|
comment = models.TextField(blank=True, null=True, help_text="Additional comment")
|
2021-07-01 13:36:07 +02:00
|
|
|
|
2021-08-03 13:13:01 +02:00
|
|
|
def __str__(self):
|
|
|
|
return "{} | {} {}".format(self.action_type, self.amount, self.unit)
|
|
|
|
|
2021-08-04 10:44:02 +02:00
|
|
|
@property
|
|
|
|
def unit_humanize(self):
|
|
|
|
""" Returns humanized version of enum
|
|
|
|
|
|
|
|
Used for template rendering
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
choices = UnitChoices.choices
|
|
|
|
for choice in choices:
|
|
|
|
if choice[0] == self.unit:
|
|
|
|
return choice[1]
|
|
|
|
return None
|
|
|
|
|
2021-07-01 13:36:07 +02:00
|
|
|
|
2021-08-02 11:52:20 +02:00
|
|
|
class AbstractCompensation(BaseObject):
|
2021-07-01 13:36:07 +02:00
|
|
|
"""
|
2021-08-02 11:52:20 +02:00
|
|
|
Abstract compensation model which holds basic attributes, shared by subclasses like the regular Compensation
|
|
|
|
or EcoAccount.
|
|
|
|
|
2021-07-01 13:36:07 +02:00
|
|
|
"""
|
2021-07-30 12:20:23 +02:00
|
|
|
responsible = models.OneToOneField(
|
|
|
|
ResponsibilityData,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
help_text="Holds data on responsible organizations ('Zulassungsbehörde', 'Eintragungsstelle') and handler",
|
|
|
|
)
|
2021-07-01 14:38:57 +02:00
|
|
|
|
2021-07-30 12:20:23 +02:00
|
|
|
before_states = models.ManyToManyField(CompensationState, blank=True, related_name='+', help_text="Refers to 'Ausgangszustand Biotop'")
|
|
|
|
after_states = models.ManyToManyField(CompensationState, blank=True, related_name='+', help_text="Refers to 'Zielzustand Biotop'")
|
2021-08-03 13:13:01 +02:00
|
|
|
actions = models.ManyToManyField(CompensationAction, blank=True, help_text="Refers to 'Maßnahmen'")
|
2021-07-01 14:38:57 +02:00
|
|
|
|
2021-08-05 12:54:28 +02:00
|
|
|
deadlines = models.ManyToManyField("konova.Deadline", 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
|
|
|
|
2021-08-05 12:54:28 +02:00
|
|
|
# Holds a successor for this data
|
2021-07-28 08:50:53 +02:00
|
|
|
next_version = models.ForeignKey("Compensation", null=True, blank=True, on_delete=models.DO_NOTHING)
|
|
|
|
|
2021-08-02 11:52:20 +02:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
|
|
|
|
class Compensation(AbstractCompensation):
|
|
|
|
"""
|
|
|
|
Regular compensation, linked to an intervention
|
|
|
|
"""
|
2021-07-23 09:36:43 +02:00
|
|
|
intervention = models.ForeignKey(
|
|
|
|
Intervention,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
related_name='compensations'
|
|
|
|
)
|
|
|
|
|
2021-08-02 11:52:20 +02:00
|
|
|
def __str__(self):
|
|
|
|
return "{}".format(self.identifier)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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-08-02 11:52:20 +02:00
|
|
|
class EcoAccount(AbstractCompensation):
|
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-29 16:10:56 +02:00
|
|
|
# Users having access on this object
|
2021-08-02 11:52:20 +02:00
|
|
|
# Not needed in regular Compensation since their access is defined by the linked intervention's access
|
|
|
|
users = models.ManyToManyField(
|
|
|
|
User,
|
2021-08-05 12:54:28 +02:00
|
|
|
help_text="Users having access (shared with)"
|
2021-08-02 11:52:20 +02:00
|
|
|
)
|
2021-08-02 10:14:34 +02:00
|
|
|
|
2021-08-09 14:16:54 +02:00
|
|
|
# Refers to "verzeichnen"
|
|
|
|
recorded = models.OneToOneField(
|
|
|
|
UserActionLogEntry,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
help_text="Holds data on user and timestamp of this action",
|
|
|
|
related_name="+"
|
|
|
|
)
|
|
|
|
|
2021-08-02 10:14:34 +02:00
|
|
|
def __str__(self):
|
|
|
|
return "{}".format(self.identifier)
|
|
|
|
|
|
|
|
|
|
|
|
class EcoAccountWithdraw(BaseResource):
|
|
|
|
"""
|
|
|
|
A withdraw object for eco accounts
|
|
|
|
"""
|
|
|
|
account = models.ForeignKey(
|
|
|
|
EcoAccount,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
help_text="Withdrawn from",
|
2021-08-09 14:16:54 +02:00
|
|
|
related_name="withdraws",
|
2021-08-02 10:14:34 +02:00
|
|
|
)
|
2021-08-09 14:16:54 +02:00
|
|
|
surface = models.FloatField(
|
2021-08-02 10:14:34 +02:00
|
|
|
null=True,
|
|
|
|
blank=True,
|
2021-08-09 14:16:54 +02:00
|
|
|
help_text="Amount withdrawn (m²)",
|
2021-08-02 10:14:34 +02:00
|
|
|
validators=[
|
|
|
|
MinValueValidator(limit_value=0.00),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
intervention = models.ForeignKey(
|
|
|
|
Intervention,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
help_text="Withdrawn for",
|
2021-08-09 14:16:54 +02:00
|
|
|
related_name="withdraws",
|
2021-08-02 10:14:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
2021-08-09 14:16:54 +02:00
|
|
|
return "{} of {}".format(self.surface, self.account)
|