summaryrefslogtreecommitdiff
path: root/scripts/2016-09-29-clean-dup-towns-pdl.py
blob: f454a7cac3591975bd8689c57ed32ba5efce160c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from ishtar_common import Town


dup_nb = 0
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
                p = item.save()
            for item in dup.parcels.all():
                item.main_town = town
                p = 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
            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)