Minor enhancements

* refactors eco account rest surface calculation into designated method get_available_rest()
* refactors placeholder setter for BaseForm
* adds automatic eco account identifier generating on saving
* fixes ServerMessageImportance template tag
This commit is contained in:
mipel
2021-08-11 14:17:43 +02:00
parent c95e451520
commit df70f99ca4
8 changed files with 77 additions and 15 deletions
+50 -2
View File
@@ -12,7 +12,8 @@ from django.db.models import Sum
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _
from compensation.settings import COMPENSATION_IDENTIFIER_LENGTH, COMPENSATION_IDENTIFIER_TEMPLATE
from compensation.settings import COMPENSATION_IDENTIFIER_LENGTH, COMPENSATION_IDENTIFIER_TEMPLATE, \
ECO_ACCOUNT_IDENTIFIER_LENGTH, ECO_ACCOUNT_IDENTIFIER_TEMPLATE
from intervention.models import Intervention, ResponsibilityData
from konova.models import BaseObject, BaseResource, Geometry, UuidModel
from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE
@@ -218,13 +219,44 @@ class EcoAccount(AbstractCompensation):
def __str__(self):
return "{}".format(self.identifier)
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 EcoAccount.objects.filter(identifier=new_id).exists():
new_id = self._generate_new_identifier()
self.identifier = new_id
super().save(*args, **kwargs)
def get_surface_withdraws(self) -> float:
""" Calculates the compensation's/account's surface
Returns:
sum_surface (float)
"""
return self.withdraws.all().aggregate(Sum("surface"))["surface__sum"]
return self.withdraws.all().aggregate(Sum("surface"))["surface__sum"] or 0
def get_available_rest(self, as_percentage: bool = False):
""" Calculates available rest surface of the eco account
Args:
as_percentage (bool): Whether to return the result as m² or %
Returns:
"""
ret_val = 0
withdraws = self.withdraws.all()
withdraw_surfaces = withdraws.aggregate(Sum("surface"))["surface__sum"] or 0
after_states_surfaces = self.after_states.all().aggregate(Sum("surface"))["surface__sum"] or withdraw_surfaces ## no division by zero
ret_val = after_states_surfaces - withdraw_surfaces
if as_percentage:
if after_states_surfaces > 0:
ret_val = int((ret_val / after_states_surfaces) * 100)
else:
ret_val = 0
return ret_val
def get_LANIS_link(self) -> str:
""" Generates a link for LANIS depending on the geometry
@@ -252,6 +284,22 @@ class EcoAccount(AbstractCompensation):
# ToDo
pass
@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=ECO_ACCOUNT_IDENTIFIER_LENGTH,
only_numbers=True,
)
_str = "{}{}{}".format(curr_month, curr_year, rand_str)
return ECO_ACCOUNT_IDENTIFIER_TEMPLATE.format(_str)
class EcoAccountWithdraw(BaseResource):
"""