diff options
| -rw-r--r-- | archaeological_context_records/models.py | 12 | ||||
| -rw-r--r-- | archaeological_finds/models_finds.py | 12 | ||||
| -rw-r--r-- | archaeological_finds/templates/ishtar/sheet_find.html | 4 | ||||
| -rw-r--r-- | archaeological_finds/tests.py | 42 | 
4 files changed, 50 insertions, 20 deletions
| diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index cc4e22cd5..5ba631dba 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -110,11 +110,17 @@ class Dating(models.Model):          res = {}          for idx, val in enumerate(value.split(cls.SEP)):              key = cls.HISTORY_ATTR[idx] -            if key in ("period", "dating_type", "quality"): +            if val == '': +                val = None +            elif key in ("period", "dating_type", "quality"):                  field = cls._meta.get_field(key) -                val = field.to.model.objects.get(txt_idx=val) +                q = field.related_model.objects.filter(txt_idx=val) +                if q.count(): +                    val = q.all()[0] +                else:  # do not exist anymore in db +                    val = None              elif key in ("start_date", "end_date"): -                val = datetime.datetime.fromisoformat(val) +                val = int(val)              res[key] = val          return res diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py index c7aec5dee..f8cc34e61 100644 --- a/archaeological_finds/models_finds.py +++ b/archaeological_finds/models_finds.py @@ -32,7 +32,7 @@ from django.utils.translation import ugettext_lazy as _, pgettext_lazy, \  from ishtar_common.data_importer import post_importer_action, ImporterError  from ishtar_common.utils import cached_label_changed, post_save_point, \ -    m2m_historization_changed +    m2m_historization_changed, HISTORY_M2M_SPLIT  from ishtar_common.alternative_configs import ALTERNATE_CONFIGS @@ -1233,6 +1233,16 @@ class Find(BulkUpdatedItem, ValueGetter, BaseHistorizedItem, OwnPerms,          return u" ; ".join([unicode(dating) for dating in self.datings.all()])      @property +    def dating_list(self): +        if self.historical_datings: +            return [ +                Dating.history_decompress(v) +                for v in self.historical_datings.split(HISTORY_M2M_SPLIT) +            ] +        else: +            return self.datings.all() + +    @property      def excavation_ids(self):          return u" - ".join(              [base_find['excavation_id'] diff --git a/archaeological_finds/templates/ishtar/sheet_find.html b/archaeological_finds/templates/ishtar/sheet_find.html index 3036c2cb6..2d234b2d9 100644 --- a/archaeological_finds/templates/ishtar/sheet_find.html +++ b/archaeological_finds/templates/ishtar/sheet_find.html @@ -200,7 +200,7 @@        </div>        {% endif %} -      {% if item.datings.count or item.dating_comment %} +      {% if item.historical_datings or item.datings.count or item.dating_comment %}        <h3>{% trans "Dating" %}</h3>        {% if item.datings.count %}        <table id='{{window_id}}-datings' class="table table-striped"> @@ -212,7 +212,7 @@                <th>{% trans "Quality" %}</th>                <th>{% trans "Precise dating" %}</th>            </tr> -          {% for dating in item.datings.all %} +          {% for dating in item.dating_list %}            <tr>                <td>                    {{dating.period}} diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index ba53719bd..cefeb1649 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -827,16 +827,7 @@ class FindHistoryTest(FindInit, TestCase):          self.client = Client()          self.client.login(username=self.username, password=self.password) -    def test_m2m_history_save(self): -        find = self.finds[0] -        user = self.get_default_user() -        nb_hist = find.history.count() - -        ceram = models.MaterialType.objects.get(txt_idx='ceramic').pk -        glass = models.MaterialType.objects.get(txt_idx='glass').pk -        find.material_types.add(ceram) -        find.material_types.add(glass) - +    def _add_datings(self, find):          d1_attrs = {              "period": Period.objects.get(txt_idx='neolithic'),              "start_date": 5000, @@ -865,6 +856,18 @@ class FindHistoryTest(FindInit, TestCase):          d2 = Dating.objects.create(**d2_attrs)          find.datings.add(d1)          find.datings.add(d2) +        return d1_txt, d2_txt + +    def test_m2m_history_save(self): +        find = self.finds[0] +        user = self.get_default_user() +        nb_hist = find.history.count() + +        ceram = models.MaterialType.objects.get(txt_idx='ceramic').pk +        glass = models.MaterialType.objects.get(txt_idx='glass').pk +        find.material_types.add(ceram) +        find.material_types.add(glass) +        d1_txt, d2_txt = self._add_datings(find)          find = models.Find.objects.get(pk=find.pk)          self.assertIn( @@ -884,6 +887,7 @@ class FindHistoryTest(FindInit, TestCase):          find = models.Find.objects.get(pk=find.pk)          find.material_types.remove(ceram) +        find.datings.clear()          find.label = "hop hop hop2"          find.history_modifier = user          if hasattr(find, 'skip_history_when_saving'): @@ -893,6 +897,7 @@ class FindHistoryTest(FindInit, TestCase):          find = models.Find.objects.get(pk=find.pk)          self.assertEqual(find.historical_material_types, 'glass') +        self.assertEqual(find.historical_datings, "")          self.assertEqual(find.history.count(), nb_hist + 2)          self.assertEqual(find.history.all()[1].historical_material_types,                           historical_material_types) @@ -909,6 +914,7 @@ class FindHistoryTest(FindInit, TestCase):          glass = models.MaterialType.objects.get(txt_idx='glass').pk          find.material_types.add(ceram)          find.material_types.add(glass) +        self._add_datings(find)          find.history_modifier = user          find.label = "hop hop hop1"          find._force_history = True @@ -920,6 +926,7 @@ class FindHistoryTest(FindInit, TestCase):          find.material_types.remove(ceram)          find.history_modifier = user          find.label = "hop hop hop2" +        find.datings.clear()          find._force_history = True          if hasattr(find, 'skip_history_when_saving'):              delattr(find, 'skip_history_when_saving') @@ -930,22 +937,29 @@ class FindHistoryTest(FindInit, TestCase):              '%Y-%m-%dT%H:%M:%S.%f')          c.login(username=self.username, password=self.password) -        response = c.get(reverse('show-historized-find', +        response = c.get(reverse('show-find',                                   kwargs={'pk': find.pk}))          self.assertEqual(response.status_code, 200)          self.assertIn('class="card sheet"', response.content) +        content = response.content.decode('utf-8')          self.assertNotIn(              models.MaterialType.objects.get(txt_idx='ceramic').label, -            response.content.decode('utf-8')) +            content) +        self.assertNotIn( +            Period.objects.get(txt_idx='neolithic').label, content) +        self.assertNotIn("5001", content)          response = c.get(reverse('show-historized-find',                                   kwargs={'pk': find.pk,                                           'date': history_date}))          self.assertEqual(response.status_code, 200)          self.assertIn('class="card sheet"', response.content) +        content = response.content.decode('utf-8')          self.assertIn( -            models.MaterialType.objects.get(txt_idx='ceramic').label, -            response.content.decode('utf-8')) +            models.MaterialType.objects.get(txt_idx='ceramic').label, content) +        self.assertIn("5001", content) +        self.assertIn( +            Period.objects.get(txt_idx='neolithic').label, content)      def test_m2m_history_restore(self):          pass | 
