New command
* adds a specific command for repairing old entries of trier-saarburg ets
This commit is contained in:
parent
e2b0120f93
commit
b941b4236e
7
konova/management/__init__.py
Normal file
7
konova/management/__init__.py
Normal file
@ -0,0 +1,7 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 06.03.23
|
||||
|
||||
"""
|
7
konova/management/commands/__init__.py
Normal file
7
konova/management/commands/__init__.py
Normal file
@ -0,0 +1,7 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 06.03.23
|
||||
|
||||
"""
|
147
konova/management/commands/repair_old_entries_trier_saarburg.py
Normal file
147
konova/management/commands/repair_old_entries_trier_saarburg.py
Normal file
@ -0,0 +1,147 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 06.03.23
|
||||
|
||||
"""
|
||||
from django.db.models import Q
|
||||
|
||||
from codelist.models import KonovaCode
|
||||
from compensation.utils.quality import CompensationQualityChecker
|
||||
from intervention.models import Intervention
|
||||
from intervention.utils.quality import InterventionQualityChecker
|
||||
from konova.management.commands.setup import BaseKonovaCommand
|
||||
from user.models import User
|
||||
|
||||
|
||||
class Command(BaseKonovaCommand):
|
||||
help = "Repairs old entries, which have lost some attributes but can easily be fixed"
|
||||
|
||||
interventions = None
|
||||
correct_law = None
|
||||
correct_registration_office = None
|
||||
user = None
|
||||
|
||||
def handle(self, *args, **options):
|
||||
self.get_law()
|
||||
self.get_interventions()
|
||||
self.get_user()
|
||||
self.get_registration_office()
|
||||
|
||||
for entry in self.interventions:
|
||||
self._write_warning(f"{entry.identifier};{entry.responsible.registration_file_number}; {entry.responsible.registration_office}, {list(entry.legal.laws.all())}")
|
||||
|
||||
def get_law(self):
|
||||
""" Returns the expected law (LBauO; id=1935994)
|
||||
|
||||
"""
|
||||
if not self.correct_law:
|
||||
law = KonovaCode.objects.get(
|
||||
id=1935994
|
||||
)
|
||||
self.correct_law = law
|
||||
return self.correct_law
|
||||
|
||||
def get_interventions(self):
|
||||
""" Returns the needed interventions
|
||||
|
||||
"""
|
||||
if not self.interventions:
|
||||
interventions = Intervention.objects.filter(
|
||||
Q(responsible__registration_file_number__contains="BG") |
|
||||
Q(responsible__registration_file_number__contains="BA")
|
||||
).filter(
|
||||
responsible__conservation_office_id=1944348,
|
||||
responsible__registration_office_id__isnull=True,
|
||||
recorded__isnull=True,
|
||||
)
|
||||
self.interventions = interventions
|
||||
|
||||
return self.interventions
|
||||
|
||||
def get_user(self):
|
||||
""" Returns the user, which shall be logged as 'performing user'
|
||||
|
||||
"""
|
||||
if not self.user:
|
||||
user = User.objects.get(
|
||||
username="weinberger"
|
||||
)
|
||||
self.user = user
|
||||
return self.user
|
||||
|
||||
def get_registration_office(self):
|
||||
""" Returns the registration office
|
||||
|
||||
Finds and returns 'untere Bauaufsichtsbehörde - LK Trier-Saarburg' (id=1958136)
|
||||
|
||||
"""
|
||||
if not self.correct_registration_office:
|
||||
office = KonovaCode.objects.get(
|
||||
id=1958136
|
||||
)
|
||||
self.correct_registration_office = office
|
||||
return self.correct_registration_office
|
||||
|
||||
def repair(self):
|
||||
""" Repairs the laws
|
||||
|
||||
|
||||
"""
|
||||
interventions = self.get_interventions()
|
||||
for entry in interventions:
|
||||
self._repair_laws(entry)
|
||||
self._repair_registration_office(entry)
|
||||
|
||||
def _repair_laws(self, entry):
|
||||
""" Fixes missing law
|
||||
|
||||
Inserts the law to be expected into the m2m relation of an intervention entry
|
||||
|
||||
"""
|
||||
correct_law = self.get_law()
|
||||
laws = entry.legal.laws.all()
|
||||
if correct_law not in laws:
|
||||
laws.add(correct_law)
|
||||
|
||||
def _repair_registration_office(self, entry):
|
||||
""" Fixes missing registration office
|
||||
|
||||
"""
|
||||
assert entry.responsible.registration_office is None
|
||||
|
||||
reg_office = self.get_registration_office()
|
||||
entry.responsible.registration_office = reg_office
|
||||
entry.responsible.save()
|
||||
|
||||
def _repair_recording(self, entry):
|
||||
""" Fixes recording state
|
||||
|
||||
Performs quality check on intervention.
|
||||
Performs quality check on compensations.
|
||||
|
||||
If all checks pass, the entry will be recorded
|
||||
|
||||
"""
|
||||
user = self.get_user()
|
||||
intervention_checker = InterventionQualityChecker(entry)
|
||||
intervention_checker.run_check()
|
||||
intervention_valid = intervention_checker.valid
|
||||
if not intervention_valid:
|
||||
self._write_error(f"{entry.identifier} not valid: {intervention_checker.messages}")
|
||||
return
|
||||
|
||||
entry_coms = entry.compensations.all()
|
||||
coms_valid = True
|
||||
for com in entry_coms:
|
||||
com_checker = CompensationQualityChecker(com)
|
||||
com_checker.run_check()
|
||||
if not com_checker.valid:
|
||||
self._write_warning(
|
||||
f"{entry.identifier}: Compensation {com.identifier} not valid: {com_checker.messages}"
|
||||
)
|
||||
coms_valid = False
|
||||
|
||||
if coms_valid and intervention_valid:
|
||||
entry.set_recorded(user=user)
|
Loading…
Reference in New Issue
Block a user