From 3e5348bd79ae3575710a8aa9b58589754665d917 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 29 Sep 2016 13:34:00 +0200 Subject: Operation: add a related name for towns - temp maintenance script for de-dup towns --- archaeological_operations/models.py | 3 ++- scripts/2016-09-29-clean-dup-towns-pdl.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 scripts/2016-09-29-clean-dup-towns-pdl.py diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index 926e27b3b..0cebcf292 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -212,7 +212,8 @@ class Operation(ClosedItem, BaseHistorizedItem, ImageModel, OwnPerms, surface = models.IntegerField(_(u"Surface (m2)"), blank=True, null=True) remains = models.ManyToManyField("RemainType", verbose_name=_(u'Remains'), null=True, blank=True) - towns = models.ManyToManyField(Town, verbose_name=_(u"Towns")) + towns = models.ManyToManyField(Town, verbose_name=_(u"Towns"), + related_name='operations') cost = models.IntegerField(_(u"Cost (euros)"), blank=True, null=True) # preventive periods = models.ManyToManyField(Period, verbose_name=_(u"Periods"), diff --git a/scripts/2016-09-29-clean-dup-towns-pdl.py b/scripts/2016-09-29-clean-dup-towns-pdl.py new file mode 100644 index 000000000..e0fa1ffec --- /dev/null +++ b/scripts/2016-09-29-clean-dup-towns-pdl.py @@ -0,0 +1,23 @@ +from ishtar_common import Town + + +dup_nb = 0 +for dpt in ('44', '49', '53', '72', '85'): + for town in Town.objects.filter(numero_insee__ilike=dpt): + for dup in Town.objects.filter(name=town.name).exclude(pk=town.pk): + for item in dup.file_main.all(): + item.main_town = town + item.save() + for item in dup.parcels.all(): + item.main_town = town + item.save() + + for item in dup.file.all(): + item.towns.remove(dup) + item.towns.add(town) + for item in dup.operations.all(): + item.towns.remove(dup) + item.towns.add(town) + dup_nb += 1 + +print("{} items cleaned".format(dup_nb)) -- cgit v1.2.3 From 63f6e4badfd7bc8aade33296195f6cf90ecb3dc4 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 29 Sep 2016 14:10:23 +0200 Subject: Maintenance script for de-dup towns --- scripts/2016-09-29-clean-dup-towns-pdl.py | 33 ++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/scripts/2016-09-29-clean-dup-towns-pdl.py b/scripts/2016-09-29-clean-dup-towns-pdl.py index e0fa1ffec..f454a7cac 100644 --- a/scripts/2016-09-29-clean-dup-towns-pdl.py +++ b/scripts/2016-09-29-clean-dup-towns-pdl.py @@ -2,16 +2,25 @@ from ishtar_common import Town dup_nb = 0 -for dpt in ('44', '49', '53', '72', '85'): - for town in Town.objects.filter(numero_insee__ilike=dpt): +pdl = [] +DPTS = ('44', '49', '53', '72', '85') + +for dpt in DPTS: + for town in Town.objects.filter(numero_insee__startswith=dpt): + pdl.append(town.pk) for dup in Town.objects.filter(name=town.name).exclude(pk=town.pk): + not_dup = False + for d in DPTS: + if dup.numero_insee.startswith(d): + not_dup = True + if not_dup: + continue for item in dup.file_main.all(): item.main_town = town - item.save() + p = item.save() for item in dup.parcels.all(): item.main_town = town - item.save() - + p = item.save() for item in dup.file.all(): item.towns.remove(dup) item.towns.add(town) @@ -19,5 +28,19 @@ for dpt in ('44', '49', '53', '72', '85'): item.towns.remove(dup) item.towns.add(town) dup_nb += 1 + dup.delete() + print("{} items cleaned".format(dup_nb)) + +strange = [] +for town in Town.objects.exclude(pk__in=pdl): + if (town.file_main.count() or town.parcels.count() or town.file.count() or + town.operations.count()): + strange.append(town) + continue + town.delete() + +print('* Problems with:') +for t in strange: + print(t) -- cgit v1.2.3 From 71cfdd1c406e97757546d20ba4e189246d68734e Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 3 Oct 2016 10:08:55 +0200 Subject: Maintenance script for de-dup towns (2) --- scripts/2016-09-29-clean-dup-towns-pdl.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/scripts/2016-09-29-clean-dup-towns-pdl.py b/scripts/2016-09-29-clean-dup-towns-pdl.py index f454a7cac..ebacb2fdf 100644 --- a/scripts/2016-09-29-clean-dup-towns-pdl.py +++ b/scripts/2016-09-29-clean-dup-towns-pdl.py @@ -1,4 +1,4 @@ -from ishtar_common import Town +from ishtar_common.models import Town dup_nb = 0 @@ -37,10 +37,15 @@ strange = [] for town in Town.objects.exclude(pk__in=pdl): if (town.file_main.count() or town.parcels.count() or town.file.count() or town.operations.count()): - strange.append(town) + strange.append((town, town.file_main.count(), town.parcels.count(), + town.file.count(), town.operations.count())) continue town.delete() + print('* Problems with:') for t in strange: - print(t) + print("{}: \n\t* {} ville principale dossier\n\t* {} parcelles\n\t* {} " + "villes pour" + " dossier\n\t* {} ville pour operation".format(t[0], t[1], t[2], + t[3], t[4])) -- cgit v1.2.3