diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-11-16 16:11:46 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-11-16 16:11:46 +0100 |
commit | da867ad495b22c7d9fdaad55e1e96ff7ecd3a963 (patch) | |
tree | 888472399c23073b43e7f179c2f18a35a393ef86 | |
parent | 853a53be8002eed9e545b4bc6641ccf005121b42 (diff) | |
download | Ishtar-da867ad495b22c7d9fdaad55e1e96ff7ecd3a963.tar.bz2 Ishtar-da867ad495b22c7d9fdaad55e1e96ff7ecd3a963.zip |
New version
-rw-r--r-- | CHANGES.md | 2 | ||||
-rw-r--r-- | ishtar_common/management/commands/ishtar_maintenance.py | 86 | ||||
-rw-r--r-- | ishtar_common/version.py | 4 |
3 files changed, 89 insertions, 3 deletions
diff --git a/CHANGES.md b/CHANGES.md index 93e7d1798..88f44ccc2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,7 +1,7 @@ Ishtar changelog ================ -v3.1.40 - 2021-11-16 +v3.1.41 - 2021-11-16 -------------------- ### Features ### - Command - ishtar_maintenance: fix main image diff --git a/ishtar_common/management/commands/ishtar_maintenance.py b/ishtar_common/management/commands/ishtar_maintenance.py new file mode 100644 index 000000000..9ca03738a --- /dev/null +++ b/ishtar_common/management/commands/ishtar_maintenance.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +from argparse import RawTextHelpFormatter +import datetime +import sys + +from django.core.management.base import BaseCommand, CommandError +from django.apps import apps + +from ishtar_common import models_common + +APPS = ( + "ishtar_common", + "archaeological_operations", + "archaeological_context_records", + "archaeological_files", + "archaeological_finds", + "archaeological_warehouse", +) + + +def task_main_image(quiet=False): + for model in apps.get_models(): + if not issubclass(model, models_common.DocumentItem): + continue + if not hasattr(model, "main_image"): + continue + q = model.objects.filter( + main_image__isnull=True, documents__image__isnull=False + ).exclude(documents__image="") + nb = q.count() + if not nb: # no image attached + continue + for item in q.all(): + q_docs = item.documents.filter(image__isnull=False).exclude(image="") + item.main_image = q_docs.order_by("pk").all()[0] + item.skip_history_when_saving = True + item.save() + if not quiet: + sys.stdout.write(f"{nb} main image fixed for {model.__name__}\n") + + +def get_time(): + return datetime.datetime.now().isoformat().split(".")[0] + + +TASKS = { + "main_image": { + "help": "for items with images and no main image, put the first one created as a main image.", + "action": task_main_image, + } +} + + +class Command(BaseCommand): + help = "Launch maintenance tasks: \n" + "\n".join( + [f"* {t}: {TASKS[t]['help']}" for t in TASKS.keys()] + ) + + def create_parser(self, *args, **kwargs): + parser = super(Command, self).create_parser(*args, **kwargs) + parser.formatter_class = RawTextHelpFormatter + return parser + + def add_arguments(self, parser): + parser.add_argument("task") + parser.add_argument( + "--quiet", dest="quiet", action="store_true", help="Quiet output" + ) + + def handle(self, *args, **options): + if options["task"] not in TASKS.keys(): + msg = f"{options['task']} is not a valid task. Available tasks are:\n" + msg += "\n".join(TASKS.keys()) + raise CommandError(msg) + quiet = options["quiet"] + if not quiet: + sys.stdout.write(f"[{get_time()}] Processing task {options['task']}\n") + errors = TASKS[options["task"]]["action"](quiet=quiet) + if not errors: + if not quiet: + sys.stdout.write(f"[{get_time()}] Task {options['task']} finished\n") + sys.exit() + if not quiet: + sys.stdout.write("\n".join(errors)) + sys.exit(1) diff --git a/ishtar_common/version.py b/ishtar_common/version.py index 8b6c64e13..0e205919a 100644 --- a/ishtar_common/version.py +++ b/ishtar_common/version.py @@ -1,5 +1,5 @@ -# 3.1.40 -VERSION = (3, 1, 40) +# 3.1.41 +VERSION = (3, 1, 41) def get_version(): |