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:
@@ -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
|
||||
|
||||
@@ -15,7 +15,7 @@ from django.urls import reverse
|
||||
|
||||
from codelist.models import KonovaCode
|
||||
from compensation.models import Compensation, CompensationState, CompensationAction, EcoAccount
|
||||
from intervention.models import LegalData, ResponsibilityData, Intervention
|
||||
from intervention.models import Legal, Responsibility, Intervention
|
||||
from konova.management.commands.setup_data import GROUPS_DATA
|
||||
from konova.models import Geometry
|
||||
from konova.settings import DEFAULT_GROUP
|
||||
@@ -103,9 +103,9 @@ class BaseTestCase(TestCase):
|
||||
action=UserAction.CREATED,
|
||||
)
|
||||
# Create legal data object (without M2M laws first)
|
||||
legal_data = LegalData.objects.create()
|
||||
legal_data = Legal.objects.create()
|
||||
# Create responsible data object
|
||||
responsibility_data = ResponsibilityData.objects.create()
|
||||
responsibility_data = Responsibility.objects.create()
|
||||
geometry = Geometry.objects.create()
|
||||
# Finally create main object, holding the other objects
|
||||
intervention = Intervention.objects.create(
|
||||
@@ -162,8 +162,8 @@ class BaseTestCase(TestCase):
|
||||
)
|
||||
geometry = Geometry.objects.create()
|
||||
# Create responsible data object
|
||||
lega_data = LegalData.objects.create()
|
||||
responsible_data = ResponsibilityData.objects.create()
|
||||
lega_data = Legal.objects.create()
|
||||
responsible_data = Responsibility.objects.create()
|
||||
# Finally create main object, holding the other objects
|
||||
eco_account = EcoAccount.objects.create(
|
||||
identifier="TEST",
|
||||
|
||||
Reference in New Issue
Block a user