summaryrefslogtreecommitdiff
path: root/ishtar_common/tasks.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@proxience.com>2014-12-30 16:59:44 +0100
committerÉtienne Loks <etienne.loks@proxience.com>2015-05-06 15:38:32 +0200
commit25ec2a9794786ac83fa8ce743078305682d8298d (patch)
tree52e8f0c1333377628b60f0feefad64861980519f /ishtar_common/tasks.py
parent155b3890c7938e98f6c1596551260a92041bea68 (diff)
downloadIshtar-25ec2a9794786ac83fa8ce743078305682d8298d.tar.bz2
Ishtar-25ec2a9794786ac83fa8ce743078305682d8298d.zip
Work on new town field (with state and department) - work on new UI for files
Diffstat (limited to 'ishtar_common/tasks.py')
-rw-r--r--ishtar_common/tasks.py35
1 files changed, 32 insertions, 3 deletions
diff --git a/ishtar_common/tasks.py b/ishtar_common/tasks.py
index a9db26087..a8db97bb1 100644
--- a/ishtar_common/tasks.py
+++ b/ishtar_common/tasks.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
-# Copyright (C) 2013 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet>
+# Copyright (C) 2013-2014 É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
@@ -17,13 +17,15 @@
# See the file COPYING for details.
+import sys
+
from django.conf import settings
from django.db.models import Q
-from geodjangofla.models import Commune
-from ishtar_common.models import Town
+from ishtar_common.models import Town, Department
def load_towns():
+ from geodjangofla.models import Commune
q = None
for dpt_number in settings.ISHTAR_DPTS:
query = Q(insee_com__istartswith=dpt_number)
@@ -51,3 +53,30 @@ def load_towns():
setattr(town, k, defaults[k])
town.save()
return nb, updated
+
+def update_towns():
+ nb, updated = 0, 0
+ dpts = dict([(dpt.number, dpt) for dpt in Department.objects.all()])
+ q = Town.objects.filter(numero_insee__isnull=False)
+ total = q.count()
+ for idx, town in enumerate(q.all()):
+ sys.stdout.write('\rProcessing... %s/%d' % (
+ str(idx+1).zfill(len(str(total))), total))
+ if len(town.numero_insee) < 2:
+ continue
+ dpt_code = town.numero_insee[:2]
+ if dpt_code.startswith('9') and int(dpt_code) > 95:
+ dpt_code = town.numero_insee[:3]
+ if dpt_code not in dpts:
+ sys.stdout.write('Missing department with INSEE code: %s' % dpt_code)
+ continue
+ if town.departement == dpts[dpt_code]:
+ continue
+ if town.departement:
+ updated += 1
+ else:
+ nb += 1
+ town.departement = dpts[dpt_code]
+ town.save()
+ sys.stdout.write('\n')
+ return nb, updated