diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-01-15 17:51:10 +0100 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-01-15 17:51:10 +0100 | 
| commit | 04b76c34eacbd9ca39e4d4f61c7c57adf6412107 (patch) | |
| tree | dbbf01b44482b7d17d0e7008de43774ea11b6df0 | |
| parent | 83f721df61b06963e2751d8590f8d5fd64d15dac (diff) | |
| download | Ishtar-04b76c34eacbd9ca39e4d4f61c7c57adf6412107.tar.bz2 Ishtar-04b76c34eacbd9ca39e4d4f61c7c57adf6412107.zip | |
File: settings for M2M history
| -rw-r--r-- | archaeological_files/models.py | 14 | ||||
| -rw-r--r-- | archaeological_files/templates/ishtar/sheet_file.html | 4 | ||||
| -rw-r--r-- | ishtar_common/models.py | 40 | 
3 files changed, 52 insertions, 6 deletions
| diff --git a/archaeological_files/models.py b/archaeological_files/models.py index 4d0422f3c..16f3e4de9 100644 --- a/archaeological_files/models.py +++ b/archaeological_files/models.py @@ -28,12 +28,12 @@ from django.utils.translation import ugettext_lazy as _, pgettext_lazy, \      activate, deactivate  from ishtar_common.utils import cached_label_changed, get_cache, \ -    get_current_year +    get_current_year, m2m_historization_changed  from ishtar_common.models import GeneralType, BaseHistorizedItem, \      HistoricalRecords, OwnPerms, Person, Organization, Department, Town, \      Dashboard, DashboardFormItem, ValueGetter, ShortMenuItem, \ -    OperationType, get_external_id, post_save_cache, Document +    OperationType, get_external_id, post_save_cache, Document, HistoryModel  from archaeological_operations.models import get_values_town_related, \      ClosedItem @@ -217,6 +217,8 @@ class File(ClosedItem, BaseHistorizedItem, OwnPerms, ValueGetter,          'towns__numero_insee__startswith': '_get_department_code',      } +    HISTORICAL_M2M = ['towns', 'departments'] +      # fields      year = models.IntegerField(_(u"Year"), default=get_current_year)      numeric_reference = models.IntegerField( @@ -325,7 +327,7 @@ class File(ClosedItem, BaseHistorizedItem, OwnPerms, ValueGetter,                                      db_index=True)      imported_line = models.TextField(_(u"Imported line"), null=True,                                       blank=True) -    history = HistoricalRecords() +    history = HistoricalRecords(bases=[HistoryModel])      GET_VALUES_EXTRA = [          'general_contractor_address_1', @@ -692,9 +694,15 @@ class File(ClosedItem, BaseHistorizedItem, OwnPerms, ValueGetter,      def is_preventive(self):          return FileType.is_preventive(self.file_type.pk) +  m2m_changed.connect(cached_label_changed, sender=File.towns.through) +  post_save.connect(cached_label_changed, sender=File) +for attr in File.HISTORICAL_M2M: +    m2m_changed.connect(m2m_historization_changed, +                        sender=getattr(File, attr).through) +  class FileByDepartment(models.Model):      """ diff --git a/archaeological_files/templates/ishtar/sheet_file.html b/archaeological_files/templates/ishtar/sheet_file.html index 062dbc5a8..989c2cdfd 100644 --- a/archaeological_files/templates/ishtar/sheet_file.html +++ b/archaeological_files/templates/ishtar/sheet_file.html @@ -67,8 +67,8 @@  <h3>{% trans "Localisation"%}</h3>  <div class="row"> -    {% field_flex_multiple "Towns" item.towns %} -    {% field_flex_multiple "Departments" item.departments %} +    {% field_flex_multiple_obj "Towns" item 'towns' %} +    {% field_flex_multiple_obj "Departments" item 'departments' %}      {% field_flex "Main address" item.address %}      {% field_flex "Complement" item.address_complement %}      {% field_flex "Postal code" item.postal_code %} diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 528ba6512..c241b4fad 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -159,7 +159,10 @@ class HistoryModel(models.Model):      def m2m_listing(self, key, create=False):          if not self.history_m2m or key not in self.history_m2m:              return -        models = import_module(self.__class__.__module__ + ".models") +        models = self.__class__.__module__ +        if not models.endswith('.models'): +            models += ".models" +        models = import_module(models)          model = getattr(              models, self.__class__.__name__[len('Historical'):])          related_model = getattr(model, key).rel.model @@ -2656,6 +2659,22 @@ class Department(models.Model):      def natural_key(self):          return (self.number,) +    def history_compress(self): +        return self.number + +    @classmethod +    def history_decompress(cls, full_value, create=False): +        if not full_value: +            return [] +        res = [] +        for value in full_value: +            try: +                res.append(cls.objects.get(number=value)) +            except cls.DoesNotExist: +                continue +        return res + +  class Address(BaseHistorizedItem):      address = models.TextField(_(u"Address"), null=True, blank=True) @@ -4217,6 +4236,25 @@ class Town(Imported, models.Model):      def natural_key(self):          return (self.numero_insee, self.year) +    def history_compress(self): +        values = {'numero_insee': self.numero_insee, +                  'year': self.year or ""} +        return values + +    @classmethod +    def history_decompress(cls, full_value, create=False): +        if not full_value: +            return [] +        res = [] +        for value in full_value: +            try: +                res.append( +                    cls.objects.get(numero_insee=value['numero_insee'], +                                    year=value['year'] or None)) +            except cls.DoesNotExist: +                continue +        return res +      def __unicode__(self):          if self.cached_label:              return self.cached_label | 
