From a77cfa7fe30a6d167f5955f636e0e9641f41dca1 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Thu, 24 Mar 2022 13:42:05 +0100 Subject: [PATCH] #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 --- .../management/commands/update_codelist.py | 11 ++-- codelist/models.py | 4 +- codelist/settings.py | 1 - .../commands/kspMigrater/base_migrater.py | 57 ++++++++++++++++++- .../kspMigrater/compensation_migrater.py | 1 + .../kspMigrater/eco_account_migrater.py | 1 + .../commands/kspMigrater/ema_migrater.py | 1 + .../kspMigrater/intervention_migrater.py | 1 + 8 files changed, 67 insertions(+), 10 deletions(-) diff --git a/codelist/management/commands/update_codelist.py b/codelist/management/commands/update_codelist.py index aba524b7..84e43e17 100644 --- a/codelist/management/commands/update_codelist.py +++ b/codelist/management/commands/update_codelist.py @@ -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: diff --git a/codelist/models.py b/codelist/models.py index 7d51ae78..ac9f2941 100644 --- a/codelist/models.py +++ b/codelist/models.py @@ -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})" diff --git a/codelist/settings.py b/codelist/settings.py index 4196c061..c3e620ef 100644 --- a/codelist/settings.py +++ b/codelist/settings.py @@ -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) diff --git a/konova/management/commands/kspMigrater/base_migrater.py b/konova/management/commands/kspMigrater/base_migrater.py index 0053af41..84c7c033 100644 --- a/konova/management/commands/kspMigrater/base_migrater.py +++ b/konova/management/commands/kspMigrater/base_migrater.py @@ -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 \ No newline at end of file + 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 diff --git a/konova/management/commands/kspMigrater/compensation_migrater.py b/konova/management/commands/kspMigrater/compensation_migrater.py index d986e7cd..9e9ea634 100644 --- a/konova/management/commands/kspMigrater/compensation_migrater.py +++ b/konova/management/commands/kspMigrater/compensation_migrater.py @@ -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) diff --git a/konova/management/commands/kspMigrater/eco_account_migrater.py b/konova/management/commands/kspMigrater/eco_account_migrater.py index 9b404db2..02fb0955 100644 --- a/konova/management/commands/kspMigrater/eco_account_migrater.py +++ b/konova/management/commands/kspMigrater/eco_account_migrater.py @@ -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() diff --git a/konova/management/commands/kspMigrater/ema_migrater.py b/konova/management/commands/kspMigrater/ema_migrater.py index f7a7787d..249868a3 100644 --- a/konova/management/commands/kspMigrater/ema_migrater.py +++ b/konova/management/commands/kspMigrater/ema_migrater.py @@ -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() diff --git a/konova/management/commands/kspMigrater/intervention_migrater.py b/konova/management/commands/kspMigrater/intervention_migrater.py index 4291952e..25e2af24 100644 --- a/konova/management/commands/kspMigrater/intervention_migrater.py +++ b/konova/management/commands/kspMigrater/intervention_migrater.py @@ -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