diff options
| -rw-r--r-- | ishtar_common/migrations/0016_auto_20171016_1104.py | 30 | ||||
| -rw-r--r-- | ishtar_common/models.py | 26 | 
2 files changed, 53 insertions, 3 deletions
| diff --git a/ishtar_common/migrations/0016_auto_20171016_1104.py b/ishtar_common/migrations/0016_auto_20171016_1104.py new file mode 100644 index 000000000..1d9209bdd --- /dev/null +++ b/ishtar_common/migrations/0016_auto_20171016_1104.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11 on 2017-10-16 11:04 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + +    dependencies = [ +        ('ishtar_common', '0015_auto_20171011_1644'), +    ] + +    operations = [ +        migrations.AddField( +            model_name='town', +            name='cached_label', +            field=models.CharField(blank=True, db_index=True, max_length=500, null=True, verbose_name='Cached name'), +        ), +        migrations.AddField( +            model_name='town', +            name='children', +            field=models.ManyToManyField(blank=True, related_name='parents', to='ishtar_common.Town', verbose_name='Town children'), +        ), +        migrations.AddField( +            model_name='town', +            name='year', +            field=models.IntegerField(blank=True, help_text='If not filled considered as the older town known.', null=True, verbose_name='Year of creation'), +        ), +    ] diff --git a/ishtar_common/models.py b/ishtar_common/models.py index ac5c29a0f..3c2972cab 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -59,7 +59,7 @@ from simple_history.models import HistoricalRecords as BaseHistoricalRecords  from ishtar_common.model_merging import merge_model_objects  from ishtar_common.utils import get_cache, disable_for_loaddata, create_slug,\ -    get_all_field_names, merge_tsvectors +    get_all_field_names, merge_tsvectors, cached_label_changed  from ishtar_common.models_imports import ImporterModel, ImporterType, \      ImporterDefault, ImporterDefaultValues, ImporterColumn, \ @@ -2575,6 +2575,14 @@ class Town(Imported, models.Model):              Department, verbose_name=u"Département", null=True, blank=True)          canton = models.ForeignKey(Canton, verbose_name=u"Canton", null=True,                                     blank=True) +    year = models.IntegerField( +        _("Year of creation"), null=True, blank=True, +        help_text=_("If not filled considered as the older town known.")) +    children = models.ManyToManyField( +        'Town', verbose_name=_(u"Town children"), blank=True, +        related_name='parents') +    cached_label = models.CharField(_(u"Cached name"), max_length=500, +                                    null=True, blank=True, db_index=True)      objects = models.GeoManager()      class Meta: @@ -2584,9 +2592,21 @@ class Town(Imported, models.Model):              ordering = ['numero_insee']      def __unicode__(self): +        if self.cached_label: +            return self.cached_label +        self.save() +        return self.cached_label + +    def _generate_cached_label(self): +        cached_label = self.name          if settings.COUNTRY == "fr": -            return u"%s (%s)" % (self.name, self.numero_insee[:2]) -        return self.name +            cached_label = u"%s - %s" % (self.name, self.numero_insee[:2]) +        if self.year: +            cached_label += " ({})".format(self.year) +        return cached_label + + +post_save.connect(cached_label_changed, sender=Town)  class OperationType(GeneralType): | 
