summaryrefslogtreecommitdiff
path: root/ishtar_common
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2024-04-16 18:18:13 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2024-04-17 12:12:19 +0200
commit57ce249c3e853e0f8292a18ceeae2509a9feadc3 (patch)
tree03a3b1633cc09ad0814e96d6e98a75d9a5b6c7b7 /ishtar_common
parent3d567dac887b3768432483e1840279e0aed61317 (diff)
downloadIshtar-57ce249c3e853e0f8292a18ceeae2509a9feadc3.tar.bz2
Ishtar-57ce249c3e853e0f8292a18ceeae2509a9feadc3.zip
✨ ishtar_maintenance: fix_geographic_items task - 🐛 Geodata: fix default attachment for find, context records, warehouse, container
Diffstat (limited to 'ishtar_common')
-rw-r--r--ishtar_common/management/commands/ishtar_maintenance.py39
-rw-r--r--ishtar_common/models_common.py5
-rw-r--r--ishtar_common/utils.py6
3 files changed, 45 insertions, 5 deletions
diff --git a/ishtar_common/management/commands/ishtar_maintenance.py b/ishtar_common/management/commands/ishtar_maintenance.py
index e4943ea8d..106575570 100644
--- a/ishtar_common/management/commands/ishtar_maintenance.py
+++ b/ishtar_common/management/commands/ishtar_maintenance.py
@@ -18,7 +18,7 @@ from django.core.management.base import BaseCommand, CommandError
from django.template.defaultfilters import slugify
from ishtar_common import models_common
-from ishtar_common.utils import create_default_areas
+from ishtar_common.utils import create_default_areas, BColors
APPS = (
"ishtar_common",
@@ -184,6 +184,33 @@ def _end_task(changed_nb, msg, quiet, store_results, log, log_name, csv_cols):
sys.stdout.write(f"log: {path} written.\n")
+def task_fix_geographic_items(options):
+ quiet = options.get("quiet", False)
+ filtr = options.get("filter", "")
+ model_filter = options.get('model', "").lower()
+ for model in apps.get_models():
+ if not issubclass(model, models_common.GeographicItem) or not hasattr(model, "post_save_geo"):
+ continue
+ if model_filter and model_filter not in (model.__name__.lower(), model._meta.model_name.lower()):
+ continue
+ q = model.objects.exclude(
+ main_geodata__source_content_type__app_label=model._meta.app_label,
+ main_geodata__source_content_type__model=model._meta.model_name,
+ ) # main geo is set for the current model -> do not change
+ if filtr:
+ q = q.filter(**filtr)
+ nb = q.count()
+ total = 0
+ for idx, item in enumerate(q.all()):
+ if not quiet:
+ msg = BColors.format("OKBLUE", f"\r→ {model.__name__}: {idx + 1}/{nb}" + " " * 20)
+ sys.stdout.write(msg)
+ total += 1 if item.post_save_geo() else 0
+ if not quiet:
+ msg = BColors.format("OKGREEN", f"\n* {total} geographic(s) item(s) fixed for {model.__name__}\n")
+ sys.stdout.write(msg)
+
+
def task_main_image(options):
quiet = options.get("quiet", False)
filtr = options.get("filter", "")
@@ -300,6 +327,10 @@ TASKS = {
"help": "create default areas from department and states",
"action": task_default_areas,
},
+ "fix_geographic_items": {
+ "help": "check and fix geographic association and main geographic iem",
+ "action": task_fix_geographic_items,
+ },
"fix_main_image": {
"help": "for items with images and no main image, put the first one created as a main image",
"action": task_main_image,
@@ -393,11 +424,13 @@ class Command(BaseCommand):
quiet = options["quiet"]
if not quiet:
- sys.stdout.write(f"[{get_time()}] Processing task {options['task']}\n")
+ msg = BColors.format("HEADER", f"[{get_time()}] Processing task {options['task']}\n")
+ sys.stdout.write(msg)
errors = TASKS[options["task"]]["action"](options)
if not errors:
if not quiet:
- sys.stdout.write(f"\n[{get_time()}] Task {options['task']} finished\n")
+ msg = BColors.format("HEADER", f"[{get_time()}] Task {options['task']} finished\n")
+ sys.stdout.write(msg)
if options["test"]:
return
sys.exit()
diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py
index 9c081a022..ddf5d46d5 100644
--- a/ishtar_common/models_common.py
+++ b/ishtar_common/models_common.py
@@ -3302,9 +3302,10 @@ class MainItem(ShortMenuItem, SerializeItem, SheetItem):
self._cached_label_checked = False
cached_label_changed(self.__class__, instance=self, created=False)
- def post_save_geo(self):
+ def post_save_geo(self, save=True):
+ if getattr(self, "_post_saved_geo", False):
+ return
self.no_post_process()
- self._post_saved_geo = False
post_save_geo(self.__class__, instance=self, created=False)
return False
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py
index dd21bec06..f734a9b2d 100644
--- a/ishtar_common/utils.py
+++ b/ishtar_common/utils.py
@@ -151,6 +151,12 @@ class BColors:
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
+ @classmethod
+ def format(cls, color, value):
+ if not hasattr(cls, color):
+ return value
+ return f"{getattr(cls, color)}{value}{cls.ENDC}"
+
class Round(models.Func):
function = "ROUND"