diff --git a/compensation/admin.py b/compensation/admin.py index a902e1bc..43fef018 100644 --- a/compensation/admin.py +++ b/compensation/admin.py @@ -37,8 +37,7 @@ class CompensationAdmin(admin.ModelAdmin): "id", "identifier", "title", - "created_on", - "created_by", + "created", ] @@ -47,8 +46,6 @@ class EcoAccountAdmin(admin.ModelAdmin): "id", "identifier", "title", - "created_on", - "created_by", ] @@ -57,8 +54,6 @@ class PaymentAdmin(admin.ModelAdmin): "id", "amount", "due_on", - "created_by", - "created_on", ] @@ -68,8 +63,6 @@ class EcoAccountWithdrawAdmin(admin.ModelAdmin): "account", "intervention", "amount", - "created_by", - "created_on", ] diff --git a/compensation/forms.py b/compensation/forms.py index 0741478a..05154673 100644 --- a/compensation/forms.py +++ b/compensation/forms.py @@ -10,7 +10,9 @@ from django.db import transaction from django.utils.translation import gettext_lazy as _ from compensation.models import Payment +from konova.enums import UserActionLogEntryEnum from konova.forms import BaseForm, BaseModalForm +from user.models import UserActionLogEntry class NewCompensationForm(BaseForm): @@ -56,8 +58,12 @@ class NewPaymentForm(BaseModalForm): def save(self): with transaction.atomic(): + action = UserActionLogEntry.objects.create( + user=self.user, + action=UserActionLogEntryEnum.CREATED.value, + ) pay = Payment.objects.create( - created_by=self.user, + created=action, amount=self.cleaned_data.get("amount", -1), due_on=self.cleaned_data.get("due", None), comment=self.cleaned_data.get("transfer_note", None), diff --git a/compensation/models.py b/compensation/models.py index 0221a7b6..dd5488de 100644 --- a/compensation/models.py +++ b/compensation/models.py @@ -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) diff --git a/compensation/tables.py b/compensation/tables.py index 0277b52b..5fd65b3e 100644 --- a/compensation/tables.py +++ b/compensation/tables.py @@ -51,7 +51,7 @@ class CompensationTable(BaseTable): lm = tables.Column( verbose_name=_("Last edit"), orderable=True, - accessor="created_on", + accessor="created__timestamp", ) class Meta(BaseTable.Meta): @@ -173,7 +173,7 @@ class EcoAccountTable(BaseTable): d = tables.Column( verbose_name=_("Created on"), orderable=True, - accessor="created_on", + accessor="created__timestamp", ) ac = tables.Column( verbose_name=_("Actions"), diff --git a/compensation/views.py b/compensation/views.py index c0b55a34..55e585ad 100644 --- a/compensation/views.py +++ b/compensation/views.py @@ -28,7 +28,7 @@ def index_view(request: HttpRequest): template = "generic_index.html" user = request.user compensations = Compensation.objects.filter( - deleted_on=None, + deleted=None, ) table = CompensationTable( request=request, @@ -92,7 +92,7 @@ def account_index_view(request: HttpRequest): template = "generic_index.html" user = request.user eco_accounts = EcoAccount.objects.filter( - deleted_on=None, + deleted=None, ) table = EcoAccountTable( request=request, diff --git a/intervention/admin.py b/intervention/admin.py index 4adf095e..591a9a6e 100644 --- a/intervention/admin.py +++ b/intervention/admin.py @@ -7,8 +7,8 @@ class InterventionAdmin(admin.ModelAdmin): list_display = [ "id", "title", - "created_on", - "deleted_on", + "created", + "deleted", ] diff --git a/intervention/forms.py b/intervention/forms.py index 3eb2a885..147c06be 100644 --- a/intervention/forms.py +++ b/intervention/forms.py @@ -15,10 +15,12 @@ from django.urls import reverse from django.utils.translation import gettext_lazy as _ from intervention.models import Intervention +from konova.enums import UserActionLogEntryEnum from konova.forms import BaseForm, BaseModalForm from konova.models import Document from konova.settings import DEFAULT_LAT, DEFAULT_LON, DEFAULT_ZOOM from organisation.models import Organisation +from user.models import UserActionLogEntry class NewInterventionForm(BaseForm): @@ -115,6 +117,10 @@ class NewInterventionForm(BaseForm): geometry = self.cleaned_data.get("geometry", Polygon()) documents = self.cleaned_data.get("documents", []) or [] + action = UserActionLogEntry.objects.create( + user=user, + action=UserActionLogEntryEnum.CREATED.value, + ) intervention = Intervention( identifier=identifier, title=title, @@ -124,7 +130,7 @@ class NewInterventionForm(BaseForm): data_provider=data_provider, data_provider_detail=data_provider_detail, geometry=geometry, - created_by=user, + created=action, ) intervention.save() for doc in documents: diff --git a/intervention/models.py b/intervention/models.py index 81981bba..22f68f8c 100644 --- a/intervention/models.py +++ b/intervention/models.py @@ -12,6 +12,7 @@ from django.utils import timezone from django.utils.timezone import now from intervention.settings import INTERVENTION_IDENTIFIER_LENGTH, INTERVENTION_IDENTIFIER_TEMPLATE +from konova.enums import UserActionLogEntryEnum from konova.models import BaseObject, Geometry, UuidModel from konova.utils import generators from konova.utils.generators import generate_random_string @@ -30,6 +31,13 @@ class ResponsibilityData(UuidModel): conservation_file_number = models.CharField(max_length=1000, blank=True, null=True) handler = models.CharField(max_length=500, null=True, blank=True, help_text="Refers to 'Eingriffsverursacher'") + def __str__(self): + return "ZB: {} | ETS: {} | Handler: {}".format( + self.registration_office, + self.conservation_office, + self.handler + ) + class LegalData(UuidModel): """ @@ -44,6 +52,13 @@ class LegalData(UuidModel): process_type = models.CharField(max_length=500, null=True, blank=True) law = models.CharField(max_length=500, null=True, blank=True) + def __str__(self): + return "{} | {} | {}".format( + self.process_type, + self.law, + self.id + ) + class Intervention(BaseObject): """ @@ -121,13 +136,20 @@ class Intervention(BaseObject): with transaction.atomic(): # "Delete" related compensations as well coms = self.compensations.all() + action = UserActionLogEntry.objects.create( + user=_user, + timestamp=_now, + action=UserActionLogEntryEnum.DELETED.value + ) for com in coms: - com.deleted_on = _now - com.deleted_by = _user + com.deleted = action + #com.deleted_on = _now + #com.deleted_by = _user com.save() - self.deleted_on = _now - self.deleted_by = _user + self.deleted = action + #self.deleted_on = _now + #self.deleted_by = _user self.save() @staticmethod diff --git a/intervention/tables.py b/intervention/tables.py index 570eed41..be459377 100644 --- a/intervention/tables.py +++ b/intervention/tables.py @@ -50,7 +50,7 @@ class InterventionTable(BaseTable): lm = tables.Column( verbose_name=_("Last edit"), orderable=True, - accessor="created_on", + accessor="created__timestamp", ) """ # ToDo: Decide to keep actions column or to dismiss them diff --git a/intervention/templates/intervention/detail/view.html b/intervention/templates/intervention/detail/view.html index f7540b0e..2ade6d7a 100644 --- a/intervention/templates/intervention/detail/view.html +++ b/intervention/templates/intervention/detail/view.html @@ -122,15 +122,15 @@