summaryrefslogtreecommitdiff
path: root/ishtar_common/management/commands/process_initialize_item_keys.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/management/commands/process_initialize_item_keys.py')
-rw-r--r--ishtar_common/management/commands/process_initialize_item_keys.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/ishtar_common/management/commands/process_initialize_item_keys.py b/ishtar_common/management/commands/process_initialize_item_keys.py
new file mode 100644
index 000000000..4ccaadc0a
--- /dev/null
+++ b/ishtar_common/management/commands/process_initialize_item_keys.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import datetime
+import sys
+
+from django.apps import apps
+from django.conf import settings
+from django.contrib.contenttypes.models import ContentType
+from django.core.management.base import BaseCommand
+from django.utils.text import slugify
+
+from ishtar_common.models import ItemKey
+from ishtar_common.utils import get_log_time, get_percent, get_eta, BColors
+
+
+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_item_key(clean_old=False):
+ """
+ clean_old=False: set to True to migrate from non unicode to unicode
+ """
+ SPACE = " " * 30
+ for app in ("ishtar_common", "archaeological_context_records", "archaeological_files",
+ "archaeological_finds", "archaeological_operations",
+ "archaeological_warehouse"):
+ app_models = apps.get_app_config(app).get_models()
+ for model in app_models:
+ if any(1 for attr in ("available", "txt_idx", "comment", "available")
+ if not hasattr(model, attr)):
+ continue # not a general type
+ content_type = ContentType.objects.get(app_label=app,
+ model=model._meta.model_name)
+ ref_time = datetime.datetime.now()
+ q = model.objects
+ nb = q.count()
+ for idx, item in enumerate(q.all()):
+ write_output(model._meta.verbose_name, idx, nb, ref_time)
+ if clean_old:
+ ItemKey.objects.filter(
+ key=slugify(item.label),
+ content_type=content_type, importer_type=None,
+ ishtar_import=None, user=None, group=None).delete()
+ ItemKey.objects.get_or_create(
+ key=slugify(item.label, allow_unicode=True),
+ content_type=content_type, object_id=item.pk,
+ importer_type=None, ishtar_import=None, user=None,
+ group=None)
+ lbl = f"\r{BColors.OKGREEN}* {model._meta.verbose_name} - OK{SPACE}{BColors.ENDC}\n"
+ sys.stdout.write(lbl)
+
+
+class Command(BaseCommand):
+ help = "Re-initialize ItemKey"
+
+ def add_arguments(self, parser):
+ parser.add_argument(
+ "--quiet", dest="quiet", action="store_true", help="Quiet output"
+ )
+ parser.add_argument(
+ "--clean-old", dest="clean_old", action="store_true",
+ help="V4.4 migration - clean old non-utf-8 keys"
+ )
+
+ def handle(self, *args, **options):
+ global quiet
+ quiet = options["quiet"]
+ if not quiet:
+ sys.stdout.write(f"{BColors.OKGREEN}[{get_log_time()}] Processing{BColors.ENDC}")
+ settings.USE_BACKGROUND_TASK = False
+ migrate_item_key(clean_old=options["clean_old"])
+ sys.exit(1)