Refactoring

* moves adding of revocation into Intervention model
This commit is contained in:
mpeltriaux 2021-11-16 12:26:50 +01:00
parent 60a50b304a
commit 9b531bc09e
2 changed files with 42 additions and 28 deletions

View File

@ -176,33 +176,7 @@ class NewRevocationModalForm(BaseModalForm):
} }
def save(self): def save(self):
with transaction.atomic(): revocation = self.instance.add_revocation(self)
created_action = UserActionLogEntry.objects.create(
user=self.user,
action=UserAction.CREATED
)
edited_action = UserActionLogEntry.objects.create(
user=self.user,
action=UserAction.EDITED
)
revocation = Revocation.objects.create(
date=self.cleaned_data["date"],
legal=self.instance.legal,
comment=self.cleaned_data["comment"],
created=created_action,
)
self.instance.modified = edited_action
self.instance.save()
self.instance.log.add(edited_action)
if self.cleaned_data["file"]:
RevocationDocument.objects.create(
title="revocation_of_{}".format(self.instance.identifier),
date_of_creation=self.cleaned_data["date"],
comment=self.cleaned_data["comment"],
file=self.cleaned_data["file"],
instance=revocation
)
return revocation return revocation

View File

@ -15,7 +15,7 @@ from django.utils.translation import gettext_lazy as _
from intervention.managers import InterventionManager from intervention.managers import InterventionManager
from intervention.models.legal import Legal from intervention.models.legal import Legal
from intervention.models.responsibility import Responsibility from intervention.models.responsibility import Responsibility
from intervention.models.revocation import RevocationDocument from intervention.models.revocation import RevocationDocument, Revocation
from intervention.utils.quality import InterventionQualityChecker from intervention.utils.quality import InterventionQualityChecker
from konova.models import generate_document_file_upload_path, AbstractDocument, Geometry, BaseObject, ShareableObjectMixin, \ from konova.models import generate_document_file_upload_path, AbstractDocument, Geometry, BaseObject, ShareableObjectMixin, \
RecordableObjectMixin, CheckableObjectMixin RecordableObjectMixin, CheckableObjectMixin
@ -209,6 +209,46 @@ class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, Chec
self.save() self.save()
return pay return pay
def add_revocation(self, form):
""" Adds a new revocation to the intervention
Args:
form (NewPaymentForm): The form holding the data
Returns:
"""
form_data = form.cleaned_data
user = form.user
with transaction.atomic():
created_action = UserActionLogEntry.objects.create(
user=user,
action=UserAction.CREATED
)
edited_action = UserActionLogEntry.objects.create(
user=user,
action=UserAction.EDITED
)
revocation = Revocation.objects.create(
date=form_data["date"],
legal=self.legal,
comment=form_data["comment"],
created=created_action,
)
self.modified = edited_action
self.save()
self.log.add(edited_action)
if form_data["file"]:
RevocationDocument.objects.create(
title="revocation_of_{}".format(self.identifier),
date_of_creation=form_data["date"],
comment=form_data["comment"],
file=form_data["file"],
instance=revocation
)
return revocation
class InterventionDocument(AbstractDocument): class InterventionDocument(AbstractDocument):
""" """