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-09-17 09:16:55 +02:00
|
|
|
import shutil
|
|
|
|
|
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-09-17 13:33:51 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2021-08-10 10:42:04 +02:00
|
|
|
from django.core.validators import MinValueValidator
|
2021-09-17 09:16:55 +02:00
|
|
|
from django.db.models import Sum, QuerySet
|
2021-08-04 10:44:02 +02:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-07-01 13:36:07 +02:00
|
|
|
|
2021-08-24 14:50:51 +02:00
|
|
|
from codelist.models import KonovaCode
|
2021-09-20 12:47:55 +02:00
|
|
|
from codelist.settings import CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES_ID, \
|
2021-10-04 09:55:59 +02:00
|
|
|
CODELIST_COMPENSATION_FUNDING_ID
|
2021-10-14 14:12:33 +02:00
|
|
|
from compensation.managers import CompensationStateManager, EcoAccountDeductionManager, CompensationActionManager, \
|
|
|
|
EcoAccountManager, CompensationManager
|
2021-10-22 13:32:05 +02:00
|
|
|
from intervention.models import Intervention, ResponsibilityData, LegalData
|
2021-09-01 16:24:49 +02:00
|
|
|
from konova.models import BaseObject, BaseResource, Geometry, UuidModel, AbstractDocument, \
|
|
|
|
generate_document_file_upload_path
|
2021-08-10 13:57:03 +02:00
|
|
|
from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE
|
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)])
|
2021-08-24 14:50:51 +02:00
|
|
|
due_on = models.DateField(null=True, blank=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-24 15:55:06 +02:00
|
|
|
class Meta:
|
|
|
|
ordering = [
|
|
|
|
"-amount",
|
|
|
|
]
|
|
|
|
|
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.
|
|
|
|
"""
|
2021-08-24 14:50:51 +02:00
|
|
|
biotope_type = models.ForeignKey(
|
|
|
|
KonovaCode,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
2021-08-27 15:16:05 +02:00
|
|
|
blank=True,
|
|
|
|
limit_choices_to={
|
|
|
|
"code_lists__in": [CODELIST_BIOTOPES_ID],
|
|
|
|
"is_selectable": True,
|
|
|
|
"is_archived": False,
|
|
|
|
}
|
2021-08-24 14:50:51 +02:00
|
|
|
)
|
2021-07-01 14:38:57 +02:00
|
|
|
surface = models.FloatField()
|
2021-07-01 13:36:07 +02:00
|
|
|
|
2021-10-14 14:12:33 +02:00
|
|
|
objects = CompensationStateManager()
|
|
|
|
|
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.
|
|
|
|
"""
|
2021-08-24 14:50:51 +02:00
|
|
|
action_type = models.ForeignKey(
|
|
|
|
KonovaCode,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
2021-08-27 15:16:05 +02:00
|
|
|
blank=True,
|
|
|
|
limit_choices_to={
|
|
|
|
"code_lists__in": [CODELIST_COMPENSATION_ACTION_ID],
|
|
|
|
"is_selectable": True,
|
|
|
|
"is_archived": False,
|
|
|
|
}
|
2021-08-24 14:50:51 +02:00
|
|
|
)
|
2021-07-01 13:36:07 +02:00
|
|
|
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-10-14 14:12:33 +02:00
|
|
|
objects = CompensationActionManager()
|
|
|
|
|
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-19 13:02:31 +02:00
|
|
|
Abstract compensation model which holds basic attributes, shared by subclasses like the regular Compensation,
|
|
|
|
EMA or EcoAccount.
|
2021-08-02 11:52:20 +02:00
|
|
|
|
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-09-20 12:47:55 +02:00
|
|
|
fundings = models.ManyToManyField(
|
|
|
|
KonovaCode,
|
|
|
|
blank=True,
|
|
|
|
limit_choices_to={
|
2021-10-04 09:55:59 +02:00
|
|
|
"code_lists__in": [CODELIST_COMPENSATION_FUNDING_ID],
|
2021-09-20 12:47:55 +02:00
|
|
|
"is_selectable": True,
|
|
|
|
"is_archived": False,
|
|
|
|
},
|
|
|
|
help_text="List of funding project codes",
|
|
|
|
)
|
|
|
|
|
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)
|
2021-07-01 13:36:07 +02:00
|
|
|
|
2021-08-02 11:52:20 +02:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2021-08-10 10:42:04 +02:00
|
|
|
def get_surface(self) -> float:
|
|
|
|
""" Calculates the compensation's/account's surface
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
sum_surface (float)
|
|
|
|
"""
|
|
|
|
return self.after_states.all().aggregate(Sum("surface"))["surface__sum"]
|
|
|
|
|
2021-08-02 11:52:20 +02:00
|
|
|
|
|
|
|
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-10-14 14:12:33 +02:00
|
|
|
objects = CompensationManager()
|
|
|
|
|
2021-08-02 11:52:20 +02:00
|
|
|
def __str__(self):
|
|
|
|
return "{}".format(self.identifier)
|
|
|
|
|
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-10-05 16:35:24 +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-10-05 16:35:24 +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-10 13:57:03 +02:00
|
|
|
def get_LANIS_link(self) -> str:
|
|
|
|
""" Generates a link for LANIS depending on the geometry
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
geom = self.geometry.geom.transform(DEFAULT_SRID_RLP, clone=True)
|
|
|
|
x = geom.centroid.x
|
|
|
|
y = geom.centroid.y
|
|
|
|
zoom_lvl = 16
|
|
|
|
except AttributeError:
|
|
|
|
# If no geometry has been added, yet.
|
|
|
|
x = 1
|
|
|
|
y = 1
|
|
|
|
zoom_lvl = 6
|
|
|
|
return LANIS_LINK_TEMPLATE.format(
|
|
|
|
zoom_lvl,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
)
|
|
|
|
|
2021-09-17 09:16:55 +02:00
|
|
|
def get_documents(self) -> QuerySet:
|
|
|
|
""" Getter for all documents of a compensation
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
docs (QuerySet): The queryset of all documents
|
|
|
|
"""
|
|
|
|
docs = CompensationDocument.objects.filter(
|
|
|
|
instance=self
|
|
|
|
)
|
|
|
|
return docs
|
|
|
|
|
2021-07-01 13:36:07 +02:00
|
|
|
|
2021-09-01 16:24:49 +02:00
|
|
|
class CompensationDocument(AbstractDocument):
|
|
|
|
"""
|
|
|
|
Specializes document upload for revocations with certain path
|
|
|
|
"""
|
|
|
|
instance = models.ForeignKey(
|
|
|
|
Compensation,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="documents",
|
|
|
|
)
|
|
|
|
file = models.FileField(
|
2021-09-01 16:40:36 +02:00
|
|
|
upload_to=generate_document_file_upload_path,
|
|
|
|
max_length=1000,
|
2021-09-01 16:24:49 +02:00
|
|
|
)
|
|
|
|
|
2021-09-17 09:16:55 +02:00
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Custom delete functionality for CompensationDocuments.
|
|
|
|
Removes the folder from the file system if there are no further documents for this entry.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
*args ():
|
|
|
|
**kwargs ():
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
comp_docs = self.instance.get_documents()
|
|
|
|
|
|
|
|
folder_path = None
|
|
|
|
if comp_docs.count() == 1:
|
|
|
|
# The only file left for this compensation is the one which is currently processed and will be deleted
|
|
|
|
# Make sure that the compensation folder itself is deleted as well, not only the file
|
|
|
|
# Therefore take the folder path from the file path
|
|
|
|
folder_path = self.file.path.split("/")[:-1]
|
|
|
|
folder_path = "/".join(folder_path)
|
|
|
|
|
|
|
|
# Remove the file itself
|
|
|
|
super().delete(*args, **kwargs)
|
|
|
|
|
|
|
|
# If a folder path has been set, we need to delete the whole folder!
|
|
|
|
if folder_path is not None:
|
2021-09-17 09:43:03 +02:00
|
|
|
try:
|
|
|
|
shutil.rmtree(folder_path)
|
|
|
|
except FileNotFoundError:
|
|
|
|
# Folder seems to be missing already...
|
|
|
|
pass
|
2021-09-17 09:16:55 +02:00
|
|
|
|
2021-09-01 16:24:49 +02:00
|
|
|
|
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
|
2021-08-30 11:29:15 +02:00
|
|
|
with some kind of currency. From this account one is able to deduct currency for current projects.
|
2021-07-01 13:36:07 +02:00
|
|
|
"""
|
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-09-17 13:33:51 +02:00
|
|
|
deductable_surface = models.FloatField(
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
help_text="Amount of deductable surface - can be lower than the total surface due to deduction limitations",
|
|
|
|
default=0,
|
|
|
|
)
|
|
|
|
|
2021-10-22 13:32:05 +02:00
|
|
|
legal = models.OneToOneField(
|
|
|
|
LegalData,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
help_text="Holds data on legal dates or law"
|
|
|
|
)
|
|
|
|
|
2021-10-14 14:12:33 +02:00
|
|
|
objects = EcoAccountManager()
|
|
|
|
|
2021-08-02 10:14:34 +02:00
|
|
|
def __str__(self):
|
|
|
|
return "{}".format(self.identifier)
|
|
|
|
|
2021-09-17 13:33:51 +02:00
|
|
|
def clean(self):
|
|
|
|
# Deductable surface can not be larger than added states after surface
|
|
|
|
after_state_sum = self.get_state_after_surface_sum()
|
|
|
|
if self.deductable_surface > after_state_sum:
|
|
|
|
raise ValidationError(_("Deductable surface can not be larger than existing surfaces in after states"))
|
|
|
|
|
2021-09-20 09:03:03 +02:00
|
|
|
# Deductable surface can not be lower than amount of already deducted surfaces
|
|
|
|
# User needs to contact deducting user in case of further problems
|
|
|
|
deducted_sum = self.get_deductions_surface()
|
|
|
|
if self.deductable_surface < deducted_sum:
|
|
|
|
raise ValidationError(
|
|
|
|
_("Deductable surface can not be smaller than the sum of already existing deductions. Please contact the responsible users for the deductions!")
|
|
|
|
)
|
|
|
|
|
2021-08-11 14:17:43 +02:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.identifier is None or len(self.identifier) == 0:
|
|
|
|
# Create new identifier
|
2021-10-05 16:35:24 +02:00
|
|
|
new_id = self.generate_new_identifier()
|
2021-08-11 14:17:43 +02:00
|
|
|
while EcoAccount.objects.filter(identifier=new_id).exists():
|
2021-10-05 16:35:24 +02:00
|
|
|
new_id = self.generate_new_identifier()
|
2021-08-11 14:17:43 +02:00
|
|
|
self.identifier = new_id
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2021-09-17 13:33:51 +02:00
|
|
|
@property
|
|
|
|
def deductions_surface_sum(self) -> float:
|
|
|
|
""" Shortcut for get_deductions_surface.
|
|
|
|
|
|
|
|
Can be used in templates
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
sum_surface (float)
|
|
|
|
"""
|
|
|
|
return self.get_deductions_surface()
|
|
|
|
|
2021-08-30 11:29:15 +02:00
|
|
|
def get_deductions_surface(self) -> float:
|
2021-09-17 13:33:51 +02:00
|
|
|
""" Calculates the account's deductions surface sum
|
2021-08-10 10:42:04 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
sum_surface (float)
|
|
|
|
"""
|
2021-08-30 11:34:35 +02:00
|
|
|
return self.deductions.all().aggregate(Sum("surface"))["surface__sum"] or 0
|
2021-08-11 14:17:43 +02:00
|
|
|
|
2021-09-17 13:33:51 +02:00
|
|
|
def get_state_after_surface_sum(self) -> float:
|
|
|
|
""" Calculates the account's after state surface sum
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
sum_surface (float)
|
|
|
|
"""
|
|
|
|
return self.after_states.all().aggregate(Sum("surface"))["surface__sum"] or 0
|
|
|
|
|
2021-10-05 16:35:24 +02:00
|
|
|
def get_available_rest(self) -> (float, float):
|
2021-08-11 14:17:43 +02:00
|
|
|
""" Calculates available rest surface of the eco account
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
|
|
Returns:
|
2021-10-05 16:35:24 +02:00
|
|
|
ret_val_total (float): Total amount
|
|
|
|
ret_val_relative (float): Amount as percentage (0-100)
|
2021-08-11 14:17:43 +02:00
|
|
|
"""
|
2021-08-30 11:34:35 +02:00
|
|
|
deductions = self.deductions.filter(
|
2021-08-26 15:45:24 +02:00
|
|
|
intervention__deleted=None,
|
|
|
|
)
|
2021-08-30 11:29:15 +02:00
|
|
|
deductions_surfaces = deductions.aggregate(Sum("surface"))["surface__sum"] or 0
|
2021-09-17 13:33:51 +02:00
|
|
|
available_surfaces = self.deductable_surface or deductions_surfaces ## no division by zero
|
2021-10-05 16:35:24 +02:00
|
|
|
ret_val_total = available_surfaces - deductions_surfaces
|
|
|
|
|
|
|
|
if available_surfaces > 0:
|
|
|
|
ret_val_relative = int((ret_val_total / available_surfaces) * 100)
|
|
|
|
else:
|
|
|
|
ret_val_relative = 0
|
|
|
|
|
|
|
|
return ret_val_total, ret_val_relative
|
2021-08-10 10:42:04 +02:00
|
|
|
|
2021-08-10 13:57:03 +02:00
|
|
|
def get_LANIS_link(self) -> str:
|
|
|
|
""" Generates a link for LANIS depending on the geometry
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
geom = self.geometry.geom.transform(DEFAULT_SRID_RLP, clone=True)
|
|
|
|
x = geom.centroid.x
|
|
|
|
y = geom.centroid.y
|
|
|
|
zoom_lvl = 16
|
|
|
|
except AttributeError:
|
|
|
|
# If no geometry has been added, yet.
|
|
|
|
x = 1
|
|
|
|
y = 1
|
|
|
|
zoom_lvl = 6
|
|
|
|
return LANIS_LINK_TEMPLATE.format(
|
|
|
|
zoom_lvl,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
)
|
|
|
|
|
2021-08-19 13:44:06 +02:00
|
|
|
def quality_check(self) -> list:
|
|
|
|
""" Quality check
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
ret_msgs (list): Holds error messages
|
|
|
|
"""
|
|
|
|
ret_msgs = []
|
|
|
|
|
|
|
|
# ToDo: Add check methods!
|
|
|
|
|
|
|
|
return ret_msgs
|
2021-08-10 17:19:42 +02:00
|
|
|
|
2021-09-17 09:20:20 +02:00
|
|
|
def get_documents(self) -> QuerySet:
|
|
|
|
""" Getter for all documents of an EcoAccount
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
docs (QuerySet): The queryset of all documents
|
|
|
|
"""
|
|
|
|
docs = EcoAccountDocument.objects.filter(
|
|
|
|
instance=self
|
|
|
|
)
|
|
|
|
return docs
|
|
|
|
|
2021-08-02 10:14:34 +02:00
|
|
|
|
2021-09-01 16:24:49 +02:00
|
|
|
class EcoAccountDocument(AbstractDocument):
|
|
|
|
"""
|
|
|
|
Specializes document upload for revocations with certain path
|
|
|
|
"""
|
|
|
|
instance = models.ForeignKey(
|
|
|
|
EcoAccount,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="documents",
|
|
|
|
)
|
|
|
|
file = models.FileField(
|
2021-09-01 16:40:36 +02:00
|
|
|
upload_to=generate_document_file_upload_path,
|
|
|
|
max_length=1000,
|
2021-09-01 16:24:49 +02:00
|
|
|
)
|
|
|
|
|
2021-09-17 09:20:20 +02:00
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Custom delete functionality for EcoAccountDocuments.
|
|
|
|
Removes the folder from the file system if there are no further documents for this entry.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
*args ():
|
|
|
|
**kwargs ():
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
acc_docs = self.instance.get_documents()
|
|
|
|
|
|
|
|
folder_path = None
|
|
|
|
if acc_docs.count() == 1:
|
|
|
|
# The only file left for this eco account is the one which is currently processed and will be deleted
|
|
|
|
# Make sure that the compensation folder itself is deleted as well, not only the file
|
|
|
|
# Therefore take the folder path from the file path
|
|
|
|
folder_path = self.file.path.split("/")[:-1]
|
|
|
|
folder_path = "/".join(folder_path)
|
|
|
|
|
|
|
|
# Remove the file itself
|
|
|
|
super().delete(*args, **kwargs)
|
|
|
|
|
|
|
|
# If a folder path has been set, we need to delete the whole folder!
|
|
|
|
if folder_path is not None:
|
2021-09-17 09:43:03 +02:00
|
|
|
try:
|
|
|
|
shutil.rmtree(folder_path)
|
|
|
|
except FileNotFoundError:
|
|
|
|
# Folder seems to be missing already...
|
|
|
|
pass
|
2021-09-17 09:20:20 +02:00
|
|
|
|
2021-09-01 16:24:49 +02:00
|
|
|
|
2021-08-30 11:34:35 +02:00
|
|
|
class EcoAccountDeduction(BaseResource):
|
2021-08-02 10:14:34 +02:00
|
|
|
"""
|
2021-08-30 11:29:15 +02:00
|
|
|
A deduction object for eco accounts
|
2021-08-02 10:14:34 +02:00
|
|
|
"""
|
|
|
|
account = models.ForeignKey(
|
|
|
|
EcoAccount,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
2021-08-30 11:29:15 +02:00
|
|
|
help_text="Deducted from",
|
2021-08-30 11:34:35 +02:00
|
|
|
related_name="deductions",
|
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-30 11:29:15 +02:00
|
|
|
help_text="Amount deducted (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,
|
2021-08-30 11:29:15 +02:00
|
|
|
help_text="Deducted for",
|
2021-08-30 11:34:35 +02:00
|
|
|
related_name="deductions",
|
2021-08-02 10:14:34 +02:00
|
|
|
)
|
|
|
|
|
2021-10-14 14:12:33 +02:00
|
|
|
objects = EcoAccountDeductionManager()
|
|
|
|
|
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)
|