Refactoring
* splits compensation/models.py into subpackage * renames base objects by adding suffix Mixin
This commit is contained in:
66
compensation/models/action.py
Normal file
66
compensation/models/action.py
Normal file
@@ -0,0 +1,66 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 16.11.21
|
||||
|
||||
"""
|
||||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from codelist.models import KonovaCode
|
||||
from codelist.settings import CODELIST_COMPENSATION_ACTION_ID
|
||||
from compensation.managers import CompensationActionManager
|
||||
from konova.models import BaseResource
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
class CompensationAction(BaseResource):
|
||||
"""
|
||||
Compensations include actions like planting trees, refreshing rivers and so on.
|
||||
"""
|
||||
action_type = models.ForeignKey(
|
||||
KonovaCode,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
limit_choices_to={
|
||||
"code_lists__in": [CODELIST_COMPENSATION_ACTION_ID],
|
||||
"is_selectable": True,
|
||||
"is_archived": False,
|
||||
}
|
||||
)
|
||||
amount = models.FloatField()
|
||||
unit = models.CharField(max_length=100, null=True, blank=True, choices=UnitChoices.choices)
|
||||
comment = models.TextField(blank=True, null=True, help_text="Additional comment")
|
||||
|
||||
objects = CompensationActionManager()
|
||||
|
||||
def __str__(self):
|
||||
return "{} | {} {}".format(self.action_type, self.amount, self.unit)
|
||||
|
||||
@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
|
||||
Reference in New Issue
Block a user