Created|Deleted refactoring
* refactors base attributes created and deleted into UserActionLogEntry foreign keys * refactors all related queries and process logic * fixes binding_on into binding_date in intervention/detail/view.html * adds basic __str__ for some models *
This commit is contained in:
+34
-8
@@ -8,14 +8,17 @@ Created on: 17.11.20
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.gis.db import models
|
||||
from django.core.validators import MinValueValidator, MaxValueValidator
|
||||
from django.db import transaction
|
||||
from django.utils import timezone
|
||||
from django.utils.timezone import now
|
||||
|
||||
from compensation.settings import COMPENSATION_IDENTIFIER_LENGTH, COMPENSATION_IDENTIFIER_TEMPLATE
|
||||
from intervention.models import Intervention, ResponsibilityData
|
||||
from konova.enums import UserActionLogEntryEnum
|
||||
from konova.models import BaseObject, BaseResource, Geometry, UuidModel
|
||||
from konova.utils.generators import generate_random_string
|
||||
from organisation.models import Organisation
|
||||
from user.models import UserActionLogEntry
|
||||
|
||||
|
||||
class Payment(BaseResource):
|
||||
@@ -68,10 +71,11 @@ class CompensationAction(BaseResource):
|
||||
control = models.ForeignKey(CompensationControl, on_delete=models.SET_NULL, null=True, blank=True)
|
||||
|
||||
|
||||
class Compensation(BaseObject):
|
||||
class AbstractCompensation(BaseObject):
|
||||
"""
|
||||
The compensation holds information about which actions have to be performed until which date, who is in charge
|
||||
of this, which legal authority is the point of contact, and so on.
|
||||
Abstract compensation model which holds basic attributes, shared by subclasses like the regular Compensation
|
||||
or EcoAccount.
|
||||
|
||||
"""
|
||||
responsible = models.OneToOneField(
|
||||
ResponsibilityData,
|
||||
@@ -93,6 +97,14 @@ class Compensation(BaseObject):
|
||||
# Holds which intervention is simply a newer version of this dataset
|
||||
next_version = models.ForeignKey("Compensation", null=True, blank=True, on_delete=models.DO_NOTHING)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class Compensation(AbstractCompensation):
|
||||
"""
|
||||
Regular compensation, linked to an intervention
|
||||
"""
|
||||
intervention = models.ForeignKey(
|
||||
Intervention,
|
||||
on_delete=models.CASCADE,
|
||||
@@ -101,6 +113,9 @@ class Compensation(BaseObject):
|
||||
related_name='compensations'
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return "{}".format(self.identifier)
|
||||
|
||||
@staticmethod
|
||||
def _generate_new_identifier() -> str:
|
||||
""" Generates a new identifier for the intervention object
|
||||
@@ -131,9 +146,16 @@ class Compensation(BaseObject):
|
||||
"""
|
||||
_now = timezone.now()
|
||||
_user = kwargs.get("user", None)
|
||||
self.deleted_on = _now
|
||||
self.deleted_by = _user
|
||||
self.save()
|
||||
with transaction.atomic():
|
||||
action = UserActionLogEntry.objects.create(
|
||||
user=_user,
|
||||
timestamp=_now,
|
||||
action=UserActionLogEntryEnum.DELETED.value
|
||||
)
|
||||
self.deleted = action
|
||||
#self.deleted_on = _now
|
||||
#self.deleted_by = _user
|
||||
self.save()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.identifier is None or len(self.identifier) == 0:
|
||||
@@ -145,13 +167,17 @@ class Compensation(BaseObject):
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
|
||||
class EcoAccount(Compensation):
|
||||
class EcoAccount(AbstractCompensation):
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
# Users having access on this object
|
||||
users = models.ManyToManyField(User)
|
||||
# Not needed in regular Compensation since their access is defined by the linked intervention's access
|
||||
users = models.ManyToManyField(
|
||||
User,
|
||||
help_text="Users having access"
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return "{}".format(self.identifier)
|
||||
|
||||
Reference in New Issue
Block a user