summaryrefslogtreecommitdiff
path: root/ishtar_common/management/commands/process_initialize_item_keys.py
blob: 4ccaadc0a5f70d41a775d39d5a5dd4b5953d54f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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)