#!/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)" lbl += "{BColors.ENDC}" sys.stdout.write(lbl) sys.stdout.flush() def migrate_item_key(clean_old=False, quiet=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_or_create( 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()): if not quiet: 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" if not quiet: 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"], quiet=quiet)