#132 Log migration

* adds basic log migration for all data types
  * empty users will be auto-created on log creation
* drops CODELIST_INTERVENTION_HANDLER in favor of CODELIST_COMPENSATION_HANDLER
* fixes bug in rendering of parent-child Konova Code label
This commit is contained in:
mpeltriaux 2022-03-24 13:42:05 +01:00
parent f621aab6e1
commit a77cfa7fe3
8 changed files with 67 additions and 10 deletions

View File

@ -6,15 +6,14 @@ Created on: 23.08.21
"""
import requests
from django.core.management import BaseCommand
from xml.etree import ElementTree as etree
from codelist.models import KonovaCode, KonovaCodeList
from codelist.settings import CODELIST_INTERVENTION_HANDLER_ID, CODELIST_CONSERVATION_OFFICE_ID, \
from codelist.settings import CODELIST_CONSERVATION_OFFICE_ID, \
CODELIST_REGISTRATION_OFFICE_ID, CODELIST_BIOTOPES_ID, CODELIST_LAW_ID, CODELIST_HANDLER_ID, \
CODELIST_COMPENSATION_ACTION_ID, CODELIST_COMPENSATION_ACTION_CLASS_ID, CODELIST_COMPENSATION_ADDITIONAL_TYPE_ID, \
CODELIST_BASE_URL, CODELIST_PROCESS_TYPE_ID, CODELIST_BIOTOPES_EXTRA_CODES_ID, \
CODELIST_COMPENSATION_ACTION_DETAIL_ID, CODELIST_288_ID
CODELIST_COMPENSATION_ACTION_DETAIL_ID, CODELIST_288_ID, CODELIST_COMPENSATION_HANDLER_ID
from konova.management.commands.setup import BaseKonovaCommand
from konova.settings import PROXIES
@ -29,8 +28,7 @@ class Command(BaseKonovaCommand):
def handle(self, *args, **options):
try:
codelist_ids = [
CODELIST_INTERVENTION_HANDLER_ID,
codelist_ids = {
CODELIST_CONSERVATION_OFFICE_ID,
CODELIST_REGISTRATION_OFFICE_ID,
CODELIST_288_ID,
@ -38,12 +36,13 @@ class Command(BaseKonovaCommand):
CODELIST_BIOTOPES_EXTRA_CODES_ID,
CODELIST_LAW_ID,
CODELIST_HANDLER_ID,
CODELIST_COMPENSATION_HANDLER_ID,
CODELIST_COMPENSATION_ACTION_ID,
CODELIST_COMPENSATION_ACTION_CLASS_ID,
CODELIST_COMPENSATION_ACTION_DETAIL_ID,
CODELIST_COMPENSATION_ADDITIONAL_TYPE_ID,
CODELIST_PROCESS_TYPE_ID,
]
}
self._write_warning("Fetching codes...")
for list_id in codelist_ids:

View File

@ -51,8 +51,8 @@ class KonovaCode(models.Model):
def __str__(self, with_parent: bool = True):
ret_val = ""
if self.parent and with_parent:
ret_val += self.parent.long_name or self.parent.short_name + " > "
ret_val += self.long_name
ret_val += (self.parent.long_name or self.parent.short_name) + " > "
ret_val += self.long_name or ""
if self.short_name and self.short_name != self.long_name:
# Only add short name, if we won't have stupid repition like 'thing a (thing a)' due to misused long-short names
ret_val += f" ({self.short_name})"

View File

@ -10,7 +10,6 @@ Created on: 23.08.21
CODELIST_BASE_URL = "https://codelisten.naturschutz.rlp.de/repository/referenzliste"
# Identifier
CODELIST_INTERVENTION_HANDLER_ID = 903 # CLMassnahmeträger
CODELIST_CONSERVATION_OFFICE_ID = 907 # CLNaturschutzbehörden
CODELIST_REGISTRATION_OFFICE_ID = 1053 # CLZulassungsbehörden
CODELIST_288_ID = 288 # CL_288 (holds GISPAD related detail-biotope codes, similar to 975, but historical and only important for migration)

View File

@ -2,9 +2,12 @@ from abc import abstractmethod
import psycopg2
from django.contrib.gis.geos import GEOSException, MultiPolygon, Polygon, MultiPoint, MultiLineString
from django.core.exceptions import ObjectDoesNotExist
from django.core.files.uploadedfile import UploadedFile
from django.utils.timezone import make_aware
from konova.models import Geometry
from user.models import User, UserActionLogEntry, UserAction
class BaseMigrater:
@ -107,4 +110,56 @@ class BaseMigrater:
except FileNotFoundError:
print(f"------ !!! File not found: {doc_path}")
tmp_cursor.close()
return instance
return instance
def _migrate_log(self, instance, db_result: tuple):
identifier = f"'{db_result[0]}'"
tmp_cursor = self.db_connection.cursor()
tmp_cursor.execute(
'select '
'p.bemerkung1, '
'p.geaendertam, '
'p.geaendertvon '
'from "OBJ_MASTER" om '
'join protokoll p on om."GISPADID"=p."GISPADID" '
'where '
f'om."KENNUNG"={identifier}'
)
fetch_results = tmp_cursor.fetchall()
instance.log.all().delete()
for result in fetch_results:
comment = result[0]
timestamp = make_aware(result[1])
user_name = result[2]
user = User.objects.get_or_create(
username=user_name,
)
is_new = user[1]
user = user[0]
if is_new:
user.is_active = False
user.first_name = "MIGRIERT"
user.last_name = "MIGRIERT"
user.save()
try:
action = instance.log.get(
user=user,
timestamp=timestamp,
action=UserAction.EDITED,
comment=comment
)
except ObjectDoesNotExist:
action = UserActionLogEntry(
user=user,
timestamp=timestamp,
action=UserAction.EDITED,
comment=comment
)
action.save()
action.timestamp = timestamp
action.save()
instance.log.add(action)
tmp_cursor.close()
return instance

View File

@ -65,6 +65,7 @@ class CompensationMigrater(BaseMigrater):
compensation = self._migrate_deadlines(compensation, kom)
compensation = self._migrate_action_control_deadlines(compensation, kom)
compensation = self._migrate_actions(compensation, kom)
compensation = self._migrate_log(compensation, kom)
compensation = self._migrate_documents(compensation, CompensationDocument, kom)
try:
compensation = self._migrate_interventions_reference(compensation, kom)

View File

@ -61,6 +61,7 @@ class EcoAccountMigrater(CompensationMigrater):
eco_account = self._migrate_deadlines(eco_account, oek)
eco_account = self._migrate_action_control_deadlines(eco_account, oek)
eco_account = self._migrate_actions(eco_account, oek)
eco_account = self._migrate_log(eco_account, oek)
eco_account = self._migrate_documents(eco_account, EcoAccountDocument, oek)
eco_account.save()

View File

@ -62,6 +62,7 @@ class EmaMigrater(CompensationMigrater):
ema_obj = self._migrate_action_control_deadlines(ema_obj, ema)
ema_obj = self._migrate_actions(ema_obj, ema)
ema_obj = self._migrate_finance_volume_to_comment(ema_obj, ema)
ema_obj = self._migrate_log(ema_obj, ema)
ema_obj = self._migrate_documents(ema_obj, EmaDocument, ema)
ema_obj.save()

View File

@ -170,6 +170,7 @@ class InterventionMigrater(BaseMigrater):
intervention = self._migrate_intervention_responsibility(intervention, eiv)
intervention = self._migrate_intervention_payment(intervention, eiv)
intervention = self._migrate_documents(intervention, InterventionDocument, eiv)
intervention = self._migrate_log(intervention, eiv)
intervention.save()
num_processed += 1