Refactoring

* renames model ResponsibilityData into Responsibility
* renames model LegalData into Legal
* moves form->object saving logic into model
* refactors NewDocumentForm into special types for intervention, compensation, eco account and ema
*
This commit is contained in:
2021-11-15 17:09:17 +01:00
parent fcc364027c
commit 4ae5b38198
17 changed files with 220 additions and 155 deletions
+9 -24
View File
@@ -21,11 +21,8 @@ from django.shortcuts import render
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from compensation.models import EcoAccount, Compensation, EcoAccountDocument, CompensationDocument
from ema.models import Ema, EmaDocument
from intervention.models import Intervention, Revocation, RevocationDocument, InterventionDocument
from konova.contexts import BaseContext
from konova.models import BaseObject, Geometry
from konova.models import BaseObject, Geometry, RecordableObject
from konova.settings import DEFAULT_SRID
from konova.utils.message_templates import FORM_INVALID
from user.models import UserActionLogEntry, UserAction
@@ -382,13 +379,10 @@ class NewDocumentForm(BaseModalForm):
}
)
)
document_instance_map = {
Intervention: InterventionDocument,
Compensation: CompensationDocument,
EcoAccount: EcoAccountDocument,
Revocation: RevocationDocument,
Ema: EmaDocument,
}
document_model = None
class Meta:
abstract = True
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@@ -398,11 +392,7 @@ class NewDocumentForm(BaseModalForm):
self.form_attrs = {
"enctype": "multipart/form-data", # important for file upload
}
self.document_type = self.document_instance_map.get(
self.instance.__class__,
None
)
if not self.document_type:
if not self.document_model:
raise NotImplementedError("Unsupported document type for {}".format(self.instance.__class__))
def save(self):
@@ -411,7 +401,7 @@ class NewDocumentForm(BaseModalForm):
user=self.user,
action=UserAction.CREATED,
)
doc = self.document_type.objects.create(
doc = self.document_model.objects.create(
created=action,
title=self.cleaned_data["title"],
comment=self.cleaned_data["comment"],
@@ -456,13 +446,7 @@ class RecordModalForm(BaseModalForm):
self.form_title = _("Unrecord data")
self.form_caption = _("I, {} {}, confirm that this data must be unrecorded.").format(self.user.first_name, self.user.last_name)
implemented_cls_logic = {
Intervention,
EcoAccount,
Ema,
}
instance_name = self.instance.__class__
if instance_name not in implemented_cls_logic:
if not isinstance(self.instance, RecordableObject):
raise NotImplementedError
def is_valid(self):
@@ -471,6 +455,7 @@ class RecordModalForm(BaseModalForm):
Returns:
"""
from intervention.models import Intervention
super_val = super().is_valid()
if self.instance.recorded:
# If user wants to unrecord an already recorded dataset, we do not need to perform custom checks