konova/compensation/models/state.py
mpeltriaux 7535f008b7 #86 Logs
* adds log detail support for compensation state and action
2022-02-04 16:56:08 +01:00

61 lines
1.9 KiB
Python

"""
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.db.models import Q
from codelist.models import KonovaCode
from codelist.settings import CODELIST_BIOTOPES_ID, CODELIST_BIOTOPES_EXTRA_CODES_ID
from compensation.managers import CompensationStateManager
from konova.models import UuidModel
from konova.utils.message_templates import COMPENSATION_STATE_REMOVED
class CompensationState(UuidModel):
"""
Compensations must define the state of an area before and after the compensation.
"""
biotope_type = models.ForeignKey(
KonovaCode,
on_delete=models.SET_NULL,
null=True,
blank=True,
limit_choices_to={
"code_lists__in": [CODELIST_BIOTOPES_ID],
"is_selectable": True,
"is_archived": False,
},
related_name='+',
)
biotope_type_details = models.ManyToManyField(
KonovaCode,
blank=True,
limit_choices_to={
"code_lists__in": [CODELIST_BIOTOPES_EXTRA_CODES_ID],
"is_selectable": True,
"is_archived": False,
},
related_name='+',
)
surface = models.FloatField()
objects = CompensationStateManager()
def __str__(self):
return f"{self.biotope_type} | {self.surface}"
def delete(self, user=None, *args, **kwargs):
from compensation.models import Compensation
if user:
comps = Compensation.objects.filter(
Q(before_states__id__in=[self.id]) |
Q(after_states__id__in=[self.id])
).distinct()
for comp in comps:
comp.mark_as_edited(user, edit_comment=COMPENSATION_STATE_REMOVED)
super().delete(*args, **kwargs)