summaryrefslogtreecommitdiff
path: root/ishtar_common/management
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2024-09-11 19:07:07 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2024-09-12 09:15:32 +0200
commit003ae8e3242e8bb2eda8375b6fed1a50ab026db7 (patch)
tree27a9b523723fe36841a601ca46d66de375ab4e54 /ishtar_common/management
parent4e6d13e817d2389d5bb9b2a7848b6e9705e28912 (diff)
downloadIshtar-003ae8e3242e8bb2eda8375b6fed1a50ab026db7.tar.bz2
Ishtar-003ae8e3242e8bb2eda8375b6fed1a50ab026db7.zip
🔧 migration script for find container history
Diffstat (limited to 'ishtar_common/management')
-rw-r--r--ishtar_common/management/commands/migrate_find_container_v4_2.py104
1 files changed, 104 insertions, 0 deletions
diff --git a/ishtar_common/management/commands/migrate_find_container_v4_2.py b/ishtar_common/management/commands/migrate_find_container_v4_2.py
new file mode 100644
index 000000000..ec0d8489e
--- /dev/null
+++ b/ishtar_common/management/commands/migrate_find_container_v4_2.py
@@ -0,0 +1,104 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import csv
+import datetime
+import os
+import sys
+
+from django.conf import settings
+from django.core.management.base import BaseCommand
+from django.db.models import Q
+
+from ishtar_common.utils import get_log_time, get_percent, get_eta, BColors
+from archaeological_finds.models_finds import Find, FindTreatment
+
+log_path = os.sep.join([settings.ROOT_PATH, "logs"])
+if not os.path.exists(log_path):
+ os.mkdir(log_path, mode=0o770)
+
+
+def write_output(base_lbl, idx, total, ref_time):
+ lbl = f"\r{BColors.OKBLUE}[{get_percent(idx, total)}] {base_lbl} {idx + 1}/{total}"
+ lbl += f" ({get_eta(idx, total, ref_time, datetime.datetime.now())} left){BColors.ENDC}"
+ sys.stdout.write(lbl)
+ sys.stdout.flush()
+
+
+def migrate(log=True):
+ errors = []
+ changed = []
+ if not quiet:
+ sys.stdout.write(
+ f"{BColors.OKGREEN}\r[{get_log_time()}] Set initial location for finds\n{BColors.ENDC}"
+ )
+
+ q = Find.objects.filter(Q(container__isnull=False) | Q(container_ref__isnull=False)).filter(
+ container_fisrt_full_location=""
+ )
+ total = q.count()
+ ref_time = datetime.datetime.now()
+ base_lbl = "Migrate find"
+ for idx, find in enumerate(q):
+ if not quiet:
+ write_output(base_lbl, idx, total, ref_time)
+ if find.update_full_location():
+ changed.append(("Find", str(find), find.pk))
+ find.no_post_process()
+ find.save()
+
+ if not quiet:
+ sys.stdout.write(
+ f"\r{BColors.OKGREEN}[{get_log_time()}] Set initial location for packaging treatment\n{BColors.ENDC}"
+ )
+ q = FindTreatment.objects
+ total = q.count()
+ ref_time = datetime.datetime.now()
+ base_lbl = "Migrate treatment"
+ for idx, treatment in enumerate(q.all()):
+ if not quiet:
+ write_output(base_lbl, idx, total, ref_time)
+ if treatment.generate_full_location():
+ changed.append(("Treatment", "-", treatment.pk))
+ treatment.save()
+
+
+ if log and changed:
+ filename = f"init_find_container_historic-{get_log_time().replace(':', '')}.csv"
+ path = os.sep.join([log_path, filename])
+ with open(path, "w+") as fle:
+ writer = csv.writer(fle)
+ writer.writerow(["model", "name", "id"])
+ for change in changed:
+ writer.writerow(change)
+ if not quiet:
+ sys.stdout.write(f"\n[{get_log_time()}] Log: {path} written\n")
+ return errors
+
+
+class Command(BaseCommand):
+ help = "v4.2 - initialize find container historic"
+
+ def add_arguments(self, parser):
+ parser.add_argument(
+ "--quiet", dest="quiet", action="store_true", help="Quiet output"
+ )
+ parser.add_argument(
+ "--log", dest="log", action="store_false", help="Log into a file"
+ )
+
+ def handle(self, *args, **options):
+ log = options["log"]
+ global quiet
+ quiet = options["quiet"]
+ if not quiet:
+ sys.stdout.write(f"{BColors.OKGREEN}[{get_log_time()}] Processing migration\n{BColors.ENDC}")
+ settings.USE_BACKGROUND_TASK = False
+ errors = migrate(log=log)
+ if not errors:
+ if not quiet:
+ sys.stdout.write(f"{BColors.OKGREEN}[{get_log_time()}] Migration finished\n{BColors.ENDC}")
+ sys.exit()
+ if not quiet:
+ sys.stdout.write("\n".join(errors))
+ sys.exit(1)