diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-10-30 17:52:39 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-10-30 17:52:39 +0100 |
commit | 9d0a2f2042613eb06717c6d1137243408b6ff21f (patch) | |
tree | 7c4f9109478ae74a408c63df383d01305ee45b9b | |
parent | d4c974cd0caf263b4133870f2a57ed99c1c04f73 (diff) | |
download | Ishtar-9d0a2f2042613eb06717c6d1137243408b6ff21f.tar.bz2 Ishtar-9d0a2f2042613eb06717c6d1137243408b6ff21f.zip |
Town: alter insee code for old towns
-rw-r--r-- | ishtar_common/migrations/0020_auto_20171030_1708.py | 26 | ||||
-rw-r--r-- | ishtar_common/models.py | 30 | ||||
-rw-r--r-- | ishtar_common/tests.py | 6 |
3 files changed, 57 insertions, 5 deletions
diff --git a/ishtar_common/migrations/0020_auto_20171030_1708.py b/ishtar_common/migrations/0020_auto_20171030_1708.py new file mode 100644 index 000000000..d83a90295 --- /dev/null +++ b/ishtar_common/migrations/0020_auto_20171030_1708.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11 on 2017-10-30 17:08 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0019_auto_20171026_1827'), + ] + + operations = [ + migrations.AlterField( + model_name='town', + name='departement', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='ishtar_common.Department', verbose_name='Department'), + ), + migrations.AlterField( + model_name='town', + name='numero_insee', + field=models.CharField(max_length=120, verbose_name='Code commune (num\xe9ro INSEE)'), + ), + ] diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 14869e2a5..678564dda 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -44,7 +44,7 @@ from django.core.validators import validate_slug from django.core.urlresolvers import reverse, NoReverseMatch from django.db.utils import DatabaseError from django.db.models import Q, Max, Count -from django.db.models.signals import post_save, post_delete +from django.db.models.signals import post_save, post_delete, m2m_changed from django.utils.functional import lazy from django.utils.translation import ugettext_lazy as _ @@ -2668,10 +2668,10 @@ class Town(Imported, models.Model): center = models.PointField(_(u"Localisation"), srid=settings.SRID, blank=True, null=True) limit = models.MultiPolygonField(_(u"Limit"), blank=True, null=True) - if settings.COUNTRY == 'fr': - numero_insee = models.CharField(u"Numéro INSEE", max_length=6) - departement = models.ForeignKey( - Department, verbose_name=u"Département", null=True, blank=True) + numero_insee = models.CharField(u"Code commune (numéro INSEE)", + max_length=120) + departement = models.ForeignKey( + Department, verbose_name=_(u"Department"), null=True, blank=True) year = models.IntegerField( _("Year of creation"), null=True, blank=True, help_text=_(u"Filling this field is relevant to distinguish old towns " @@ -2745,6 +2745,15 @@ class Town(Imported, models.Model): self.save() return True + def update_town_code(self): + if not self.numero_insee or not self.children.count() or not self.year: + return + old_num = self.numero_insee[:] + numero = old_num.split('-')[0] + self.numero_insee = u"{}-{}".format(numero, self.year) + if self.numero_insee != old_num: + return True + def _generate_cached_label(self): cached_label = self.name if settings.COUNTRY == "fr": @@ -2758,11 +2767,22 @@ def post_save_town(sender, **kwargs): cached_label_changed(sender, **kwargs) town = kwargs['instance'] town.generate_geo() + if town.update_town_code(): + town.save() post_save.connect(post_save_town, sender=Town) +def town_child_changed(sender, **kwargs): + town = kwargs['instance'] + if town.update_town_code(): + town.save() + + +m2m_changed.connect(town_child_changed, sender=Town.children.through) + + class OperationType(GeneralType): order = models.IntegerField(_(u"Order"), default=1) preventive = models.BooleanField(_(u"Is preventive"), default=True) diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index e107bd6fb..63d80d5ab 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -126,6 +126,7 @@ class CommandsTestCase(TestCase): q = models.Town.objects town_nb = q.count() first, union_start, union_end = '', '', [] + new_nums = [] for idx, town in enumerate(q.all()): x1 = float(idx) / 10 if not x1: @@ -140,6 +141,8 @@ class CommandsTestCase(TestCase): union_start += '{x2} 0.1'.format(x1=x1, x2=x2) union_end.append('{x2} 0'.format(x1=x1, x2=x2)) town.limit = l + town.year = 1792 + new_nums.append("{}-{}".format(town.numero_insee, town.year)) town.save() union = 'MULTIPOLYGON (((' + first + ", " + union_start + \ ", " + ", ".join(reversed(union_end)) + ", 0 0, " + first + ")))" @@ -157,6 +160,9 @@ class CommandsTestCase(TestCase): # no new town self.assertEqual(town_nb + 1, models.Town.objects.count()) + for parent_town in new.parents.all(): + self.assertIn(parent_town.numero_insee, new_nums) + class WizardTestFormData(object): """ |