diff options
Diffstat (limited to 'ishtar_common/tasks.py')
-rw-r--r-- | ishtar_common/tasks.py | 35 |
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 |