#132 WIP Migrate Revocations

* adds revocation migrating into intervention data
* adds intervention recording migration (MUST BE RUN AFTER MAIN MIGRATION!!!)
   * WIP other data types need recording migration logic as well!
This commit is contained in:
mpeltriaux 2022-03-31 16:27:38 +02:00
parent c3cce6eb28
commit 5c15f84fba
3 changed files with 121 additions and 32 deletions

View File

@ -148,20 +148,7 @@ class BaseMigrater:
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()
# Make sure user has at least the default group set
default_group = Group.objects.get(name=DEFAULT_GROUP)
user.groups.add(default_group)
user = self._get_migrate_user(user_name)
try:
action = instance.log.get(
@ -208,3 +195,26 @@ class BaseMigrater:
instance.share_with_team(team)
return instance
def _migrate_recorded(self, instance, db_result):
raise NotImplementedError("Must be implemented in specific subtype classes!")
def _get_migrate_user(self, username):
""" Returns a migrated user
"""
user = User.objects.get_or_create(
username=username,
)
is_new = user[1]
user = user[0]
if is_new:
user.is_active = False
user.first_name = "MIGRIERT"
user.last_name = "MIGRIERT"
user.save()
# Make sure user has at least the default group set
default_group = Group.objects.get(name=DEFAULT_GROUP)
user.groups.add(default_group)
return user

View File

@ -327,20 +327,7 @@ class EcoAccountMigrater(CompensationMigrater):
create_ts = fetch_results[0][0]
create_user = fetch_results[0][1]
user = User.objects.get_or_create(
username=create_user,
)
is_new = user[1]
user = user[0]
if is_new:
user.is_active = False
user.first_name = "MIGRIERT"
user.last_name = "MIGRIERT"
user.save()
# Make sure user has at least the default group set
default_group = Group.objects.get(name=DEFAULT_GROUP)
user.groups.add(default_group)
user = self._get_migrate_user(create_user)
tmp_cursor.close()
create_action = UserActionLogEntry.get_created_action(user, comment="[Migriert] Abbuchung angelegt")

View File

@ -1,14 +1,17 @@
import datetime
from django.contrib.auth.models import Group
from django.core.exceptions import ObjectDoesNotExist
from django.db import transaction
from django.utils import timezone
from codelist.models import KonovaCode
from codelist.settings import CODELIST_LAW_ID, CODELIST_PROCESS_TYPE_ID, CODELIST_HANDLER_ID, \
CODELIST_CONSERVATION_OFFICE_ID, CODELIST_REGISTRATION_OFFICE_ID
from compensation.models import Payment
from intervention.models import Intervention, InterventionDocument, Legal, Handler, Responsibility
from intervention.models import Intervention, InterventionDocument, Legal, Handler, Responsibility, Revocation
from konova.management.commands.kspMigrater.base_migrater import BaseMigrater
from konova.settings import ETS_GROUP
class InterventionMigrater(BaseMigrater):
@ -188,15 +191,104 @@ class InterventionMigrater(BaseMigrater):
)[0]
intervention.title = eiv[1]
intervention.comment = eiv_comment
intervention = self._migrate_geometry(intervention, eiv)
intervention = self._migrate_intervention_legal(intervention, eiv)
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 = self._migrate_revocation(intervention, eiv)
intervention.save()
num_processed += 1
num_processed += 1
cursor.close()
def _migrate_revocation(self, intervention, eiv):
identifier = f"'{eiv[0]}'"
tmp_cursor = self.db_connection.cursor()
tmp_cursor.execute(
'select '
'v.widerspruchvom '
'from "OBJ_MASTER" om '
'join vorgang v on om."GISPADID"=v.gispadid '
'where '
'v.widerspruchvom is not null and '
f'om."KENNUNG"={identifier}'
)
fetch_result = tmp_cursor.fetchall()
intervention.legal.revocations.all().delete()
for result in fetch_result:
revoc = Revocation.objects.get_or_create(
date=timezone.make_aware(result[0]).date(),
legal=intervention.legal,
comment="Migrierter Widerspruch"
)
tmp_cursor.close()
return intervention
def _migrate_recorded(self, instance, db_result):
""" Recording must be run after all data (including compensations) has been migrated initially!
"""
identifier = f"'{db_result[0]}'"
tmp_cursor = self.db_connection.cursor()
tmp_cursor.execute(
'select '
'v.verzeichnet, '
'om.alteintrag '
'from "OBJ_MASTER" om '
'left join vorgang v on om."GISPADID"=v.gispadid '
'where '
f'om."KENNUNG"={identifier} '
)
recorded_state_result = tmp_cursor.fetchone()
recorded_str = "'%verzeichnet%'"
tmp_cursor.execute(
'select '
'lb.erstelltam, '
'lb.erstelltvon '
'from "OBJ_MASTER" om '
'join logbuch lb on om."GISPADID"=lb.gispadid '
'where '
f'bemerkung like {recorded_str} and '
f'om."KENNUNG"={identifier} '
'order by lb.erstelltam limit 1'
)
recorded_state_ts_user = tmp_cursor.fetchone()
if instance.recorded is not None:
# Drop old info about recording
instance.recorded.delete()
instance.recorded = None
if recorded_state_result[0] is None:
# There are old entries which hold alteintrag=false but are old entries.
# This might be due to very poor migration in the past. Therefore, if there is no entry in table vorgang
# we act as if this is an old entry anyway, which needs to be recorded
to_be_recorded = True
else:
to_be_recorded = recorded_state_result[0] or recorded_state_result[1]
if to_be_recorded:
quality_checker = instance.quality_check()
if quality_checker.valid:
if recorded_state_result[1]:
recorded_on = instance.created.timestamp
recorded_by = instance.created.user.username
else:
recorded_on = timezone.make_aware(recorded_state_ts_user[0])
recorded_by = recorded_state_ts_user[1]
user = self._get_migrate_user(recorded_by)
# If user recorded an entry, he/she is an ets member
user.groups.add(
Group.objects.get(name=ETS_GROUP)
)
action = instance.set_recorded(user)
action.timestamp = recorded_on
action.save()
tmp_cursor.close()
return instance