From 3eff97b6dd8ad47ad07a4aabee6b399c960ffc2a Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Thu, 30 Mar 2023 15:08:42 +0200 Subject: [PATCH 1/2] # Implements #332 * extends intervention's mark_as_deleted() functionality to drop related deductions and free reserved deductable surface from the related eco accounts --- intervention/models/intervention.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/intervention/models/intervention.py b/intervention/models/intervention.py index 24f5fd2..e226b8e 100644 --- a/intervention/models/intervention.py +++ b/intervention/models/intervention.py @@ -14,6 +14,7 @@ from django.urls import reverse from django.utils import timezone from analysis.settings import LKOMPVZVO_PUBLISH_DATE +from compensation.models import EcoAccountDeduction from intervention.tasks import celery_export_to_egon from user.models import User from django.db import models, transaction @@ -295,6 +296,29 @@ class Intervention(BaseObject, self.set_unchecked() return action + def mark_as_deleted(self, user, send_mail: bool = True): + """ Extends base mark_as_delete functionality + + Removes related deductions from the database, which results in updating the deductable_rest of the + corresponding eco account. + + Args: + user (User): The performing user + send_mail (bool): Whether to send an info mail + + Returns: + + """ + super().mark_as_deleted(user, send_mail) + + # Remove pending deductions to free booked capacities + deductions = EcoAccountDeduction.objects.filter( + intervention=self + ) + # Remove one by one instead of bulk to trigger EcoAccountDeduction custom delete() logic + for deduction in deductions: + deduction.delete() + def set_status_messages(self, request: HttpRequest): """ Setter for different information that need to be rendered From 8a84df0fcdbd48b2ede9fbc05b2cc42eb700f08a Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Thu, 30 Mar 2023 15:11:19 +0200 Subject: [PATCH 2/2] Simplification * simplifies fetching of intervention's deductions --- intervention/models/intervention.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/intervention/models/intervention.py b/intervention/models/intervention.py index e226b8e..face187 100644 --- a/intervention/models/intervention.py +++ b/intervention/models/intervention.py @@ -312,9 +312,7 @@ class Intervention(BaseObject, super().mark_as_deleted(user, send_mail) # Remove pending deductions to free booked capacities - deductions = EcoAccountDeduction.objects.filter( - intervention=self - ) + deductions = self.deductions.all() # Remove one by one instead of bulk to trigger EcoAccountDeduction custom delete() logic for deduction in deductions: deduction.delete()