summaryrefslogtreecommitdiff
path: root/archaeological_warehouse/management/commands/migrate_to_new_container_management.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2020-03-24 19:04:54 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2021-02-28 12:15:20 +0100
commit8b69114b1932329283f3563077a3cb53149af7c8 (patch)
tree75e1154c53fa775498c3fb91d70c6a7f7a6d59f1 /archaeological_warehouse/management/commands/migrate_to_new_container_management.py
parentd7faab38f7a4ba4b923e6ffbfca81ed6407ad5aa (diff)
downloadIshtar-8b69114b1932329283f3563077a3cb53149af7c8.tar.bz2
Ishtar-8b69114b1932329283f3563077a3cb53149af7c8.zip
Container: add parents - localisation refactoring
Diffstat (limited to 'archaeological_warehouse/management/commands/migrate_to_new_container_management.py')
-rw-r--r--archaeological_warehouse/management/commands/migrate_to_new_container_management.py106
1 files changed, 106 insertions, 0 deletions
diff --git a/archaeological_warehouse/management/commands/migrate_to_new_container_management.py b/archaeological_warehouse/management/commands/migrate_to_new_container_management.py
new file mode 100644
index 000000000..b5885cbf0
--- /dev/null
+++ b/archaeological_warehouse/management/commands/migrate_to_new_container_management.py
@@ -0,0 +1,106 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Copyright (C) 2020 É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
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# See the file COPYING for details.
+
+import csv
+import sys
+
+from django.core.management.base import BaseCommand
+from django.template.defaultfilters import slugify
+from archaeological_warehouse import models
+
+
+class Command(BaseCommand):
+ help = 'Migrate to new container management (v3.0.6)'
+
+ def handle(self, *args, **options):
+ to_update = models.Container.objects.filter(
+ division__pk__isnull=False)
+ container_types = {}
+ created_nb = 0
+ for div_type in models.WarehouseDivision.objects.all():
+ container_type, c = models.ContainerType.objects.get_or_create(
+ txt_idx=slugify(div_type.label),
+ defaults={"label": div_type.label})
+ if c:
+ created_nb += 1
+ sys.stdout.write("-> {} created\n".format(
+ div_type.label))
+ container_types[div_type.pk] = container_type
+ if created_nb:
+ sys.stdout.write("* {} container types created\n".format(
+ created_nb))
+ to_be_done = to_update.count()
+ created_nb = 0
+ potential_duplicate = {}
+ data = [("id", "warehouse", "reference",
+ "old cached division", "new cached division")]
+ for idx, container in enumerate(models.Container.objects.filter(
+ division__pk__isnull=False).all()):
+ sys.stdout.write("* Updating: {}/{}\r".format(idx + 1, to_be_done))
+ sys.stdout.flush()
+ if container.responsible_id not in potential_duplicate:
+ potential_duplicate[container.responsible_id] = {}
+ parent = None
+ cached_division = container.cached_division
+ for division in container.division.order_by(
+ "division__order").all():
+ ref = division.reference.strip()
+ if not ref or ref == "-":
+ continue
+ new_container, created = models.Container.objects.get_or_create(
+ reference=division.reference.strip(),
+ parent=parent,
+ container_type=container_types[
+ division.division.division_id],
+ location=container.responsible,
+ responsible=container.responsible)
+ if created:
+ created_nb += 1
+ ref = "{} || {}".format(str(new_container.container_type),
+ slugify(division.reference.strip()))
+ if ref not in potential_duplicate[container.responsible_id]:
+ potential_duplicate[container.responsible_id][ref] = []
+ if division.reference.strip() not in \
+ potential_duplicate[container.responsible_id][ref]:
+ potential_duplicate[container.responsible_id][
+ ref].append(division.reference.strip())
+ parent = new_container
+ if parent:
+ container.parent = parent
+ container.save()
+ data.append((container.id, str(container.responsible),
+ container.reference, cached_division,
+ container._generate_cached_division()))
+ sys.stdout.write("\n* Potential duplicate:")
+ for warehouse_id in potential_duplicate.keys():
+ warehouse = models.Warehouse.objects.get(pk=warehouse_id)
+ for ref in potential_duplicate[warehouse_id]:
+ items = potential_duplicate[warehouse_id][ref]
+ if len(items) > 1:
+ sys.stdout.write(
+ "\n-> {}: {}".format(warehouse, " ; ".join(items)))
+ print("")
+ sys.stdout.write("* {} container created\n".format(created_nb))
+ if not data:
+ return
+ with open("new_containers.csv", 'w+') as f:
+ w = csv.writer(f)
+ w.writerows(data)
+ sys.stdout.write("-> check new containers in \"new_containers.csv\"\n")
+