diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-06-16 13:34:38 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-06-17 15:17:09 +0200 |
commit | 25f23ed03ad48fb4e5ca2f14527c0bc381dc8c9b (patch) | |
tree | ba6cff80e3e426ac57cce299c36760fe87806bba /ishtar_common | |
parent | a8349afc7ec5e7919108ceca7027cf054a23caff (diff) | |
download | Ishtar-25f23ed03ad48fb4e5ca2f14527c0bc381dc8c9b.tar.bz2 Ishtar-25f23ed03ad48fb4e5ca2f14527c0bc381dc8c9b.zip |
Context records: Optimize record relations - management command - tests
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/management/commands/relations_update_cache_tables.py | 66 | ||||
-rw-r--r-- | ishtar_common/models.py | 23 |
2 files changed, 44 insertions, 45 deletions
diff --git a/ishtar_common/management/commands/relations_update_cache_tables.py b/ishtar_common/management/commands/relations_update_cache_tables.py index ab7f134ff..3e2dfaef5 100644 --- a/ishtar_common/management/commands/relations_update_cache_tables.py +++ b/ishtar_common/management/commands/relations_update_cache_tables.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (C) 2013-2018 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2021 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -24,60 +24,36 @@ from django.core.management.base import BaseCommand from django.apps import apps -APPS = ['ishtar_common', 'archaeological_operations', - 'archaeological_context_records', 'archaeological_finds', - 'archaeological_warehouse'] +CACHE_TABLES = [ + "archaeological_context_records.ContextRecordTree" +] class Command(BaseCommand): args = '' - help = 'Regenerate geo, cached labels and search vectors' + help = 'Update all relations for cache tables' def add_arguments(self, parser): - parser.add_argument('app_name', nargs='?', default=None, - choices=APPS) - parser.add_argument('model_name', nargs='?', default=None) + parser.add_argument('table', nargs='?', default=None, + choices=CACHE_TABLES) parser.add_argument( '--quiet', dest='quiet', action='store_true', help='Quiet output') def handle(self, *args, **options): quiet = options['quiet'] - limit = options['app_name'] - model_name = options['model_name'] - if model_name: - model_name = model_name.lower() - for app in APPS: - if limit and app != limit: - continue + tables = CACHE_TABLES + if options.get("table", None): + table = options.get("table", None) + if table not in CACHE_TABLES: + sys.stdout.write("{} not a valid cache table\n".format(table)) + return + tables = [table] + for table in tables: if not quiet: - print("* app: {}".format(app)) - for model in apps.get_app_config(app).get_models(): - if model_name and model.__name__.lower() != model_name: - continue - if model.__name__.startswith('Historical'): - continue - if not bool( - [k for k in dir(model) - if k.startswith('_generate_') or - k == "search_vector"]): - continue - msg = "-> processing {}: ".format(model._meta.verbose_name) - ln = model.objects.count() - for idx, obj_id in enumerate(model.objects.values('pk').all()): - obj = model.objects.get(pk=obj_id['pk']) - obj.skip_history_when_saving = True - obj._no_move = True - if hasattr(obj, "point_source") and obj.point_source in ( - "M", "T"): - obj.point = None - obj.point_2d = None - obj.x = None - obj.y = None - cmsg = "\r{} {}/{}".format(msg, idx + 1, ln) - if not quiet: - sys.stdout.write(cmsg) - sys.stdout.flush() - obj.save() - if not quiet: - sys.stdout.write("\n") + print("* table: {}".format(table)) + app, tablename = table.split(".") + model = apps.get_app_config(app).get_model(tablename) + model.regenerate_all(quiet=quiet) + if not quiet: + sys.stdout.write("\n") diff --git a/ishtar_common/models.py b/ishtar_common/models.py index fdeba5f26..19c432053 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -20,6 +20,8 @@ """ Models description """ +import sys + from bs4 import BeautifulSoup import copy import datetime @@ -862,6 +864,27 @@ class RelationsViews(models.Model): return relation_view_update.delay(sender, kwargs) @classmethod + def _get_base_children(cls): + raise NotImplemented() + + @classmethod + def regenerate_all(cls, quiet=True): + cls.check_engine() + profile = get_current_profile(force=True) + if profile.parent_relations_engine == "V": + return + cls.objects.filter(pk__isnull=False).delete() + base_children = list(cls._get_base_children()) + total = len(base_children) + for idx, cr_id in enumerate(base_children): + if not quiet: + sys.stdout.write(f"Processing: {idx + 1} / {total}\t\t{cr_id}\r") + sys.stdout.flush() + cls.update(cr_id) + if not quiet: + sys.stdout.write("\n") + + @classmethod def create_table(cls): raise NotImplemented() |