summaryrefslogtreecommitdiff
path: root/archaeological_context_records
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2017-07-26 15:05:09 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2017-07-26 15:05:09 +0200
commit8801ab7b7e655d8b831e535381eeea8adc091989 (patch)
treefd1cfe512c94affdfae65e7ec3078648eabffbd7 /archaeological_context_records
parentc42486f81b042d18b9cf825da9f35a83e00bc051 (diff)
parent005f3962ab8d0eef66de6dd335f619702107116a (diff)
downloadIshtar-8801ab7b7e655d8b831e535381eeea8adc091989.tar.bz2
Ishtar-8801ab7b7e655d8b831e535381eeea8adc091989.zip
Merge branch 'master' into v0.9
Diffstat (limited to 'archaeological_context_records')
-rw-r--r--archaeological_context_records/models.py20
-rw-r--r--archaeological_context_records/tests.py64
2 files changed, 69 insertions, 15 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py
index 940330d86..a5f02133e 100644
--- a/archaeological_context_records/models.py
+++ b/archaeological_context_records/models.py
@@ -74,6 +74,20 @@ class Dating(models.Model):
return unicode(self.period)
return u"%s (%s-%s)" % (self.period, start_date, end_date)
+ @classmethod
+ def fix_dating_association(cls, obj):
+ """
+ Fix redundant m2m dating association (usually after imports)
+ """
+ current_datings = []
+ for dating in obj.datings.order_by('pk').all():
+ key = (dating.period.pk, dating.start_date, dating.end_date,
+ dating.dating_type, dating.quality, dating.precise_dating)
+ if key not in current_datings:
+ current_datings.append(key)
+ continue
+ dating.delete()
+
class Unit(GeneralType):
order = models.IntegerField(_(u"Order"))
@@ -493,6 +507,12 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms,
self.save()
return returned
+ def fix(self):
+ """
+ Fix redundant m2m dating association (usually after imports)
+ """
+ Dating.fix_dating_association(self)
+
post_save.connect(cached_label_changed, sender=ContextRecord)
diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py
index b613b837e..7277eaef0 100644
--- a/archaeological_context_records/tests.py
+++ b/archaeological_context_records/tests.py
@@ -29,7 +29,7 @@ from django.test.client import Client
from ishtar_common.models import IshtarSiteProfile, ImporterModel
from archaeological_operations.tests import OperationInitTest, \
- ImportTest, ImportOperationTest
+ ImportTest, FILE_TOWNS_FIXTURES, FILE_FIXTURES, OPERATION_TOWNS_FIXTURES
from archaeological_operations import models as models_ope
from archaeological_context_records import models
@@ -38,12 +38,19 @@ from ishtar_common.tests import WizardTest, WizardTestFormData as FormData, \
from archaeological_context_records import views
-class ImportContextRecordTest(ImportTest, TestCase):
+CONTEXT_RECORD_FIXTURES = FILE_FIXTURES + [
+ settings.ROOT_PATH +
+ '../archaeological_context_records/fixtures/initial_data-fr.json',
+]
- fixtures = ImportOperationTest.fixtures + [
- settings.ROOT_PATH +
- '../archaeological_context_records/fixtures/initial_data-fr.json',
- ]
+CONTEXT_RECORD_TOWNS_FIXTURES = FILE_TOWNS_FIXTURES + [
+ settings.ROOT_PATH +
+ '../archaeological_context_records/fixtures/initial_data-fr.json',
+]
+
+
+class ImportContextRecordTest(ImportTest, TestCase):
+ fixtures = CONTEXT_RECORD_TOWNS_FIXTURES
def test_mcc_import_contextrecords(self):
old_nb = models.ContextRecord.objects.count()
@@ -193,7 +200,7 @@ class ContextRecordInit(OperationInitTest):
class ExportTest(ContextRecordInit, TestCase):
- fixtures = ImportContextRecordTest.fixtures
+ fixtures = CONTEXT_RECORD_TOWNS_FIXTURES
def setUp(self):
self.username, self.password, self.user = create_superuser()
@@ -222,7 +229,7 @@ class ExportTest(ContextRecordInit, TestCase):
class ContextRecordTest(ContextRecordInit, TestCase):
- fixtures = ImportContextRecordTest.fixtures
+ fixtures = CONTEXT_RECORD_TOWNS_FIXTURES
def setUp(self):
IshtarSiteProfile.objects.create()
@@ -352,9 +359,24 @@ class ContextRecordTest(ContextRecordInit, TestCase):
self.assertEqual(response.status_code, 200)
self.assertIn('class="sheet"', response.content)
+ def test_redundant_dating_clean(self):
+ obj = self.context_records[0]
+ values = {'period': models.Period.objects.all()[0]}
+ values_2 = {'period': models.Period.objects.all()[0],
+ 'quality': models.DatingQuality.objects.all()[0]}
+
+ obj.datings.add(models.Dating.objects.create(**values))
+ obj.datings.add(models.Dating.objects.create(**values))
+ obj.datings.add(models.Dating.objects.create(**values_2))
+ obj.datings.add(models.Dating.objects.create(**values_2))
+ self.assertEqual(obj.datings.count(), 4)
+ obj.fix()
+ self.assertEqual(obj.datings.count(), 2)
+
+
class ContextRecordSearchTest(ContextRecordInit, TestCase):
- fixtures = ImportContextRecordTest.fixtures
+ fixtures = CONTEXT_RECORD_TOWNS_FIXTURES
def setUp(self):
IshtarSiteProfile.objects.create()
@@ -496,7 +518,7 @@ class ContextRecordSearchTest(ContextRecordInit, TestCase):
class RecordRelationsTest(ContextRecordInit, TestCase):
- fixtures = ImportOperationTest.fixtures
+ fixtures = OPERATION_TOWNS_FIXTURES
model = models.ContextRecord
def setUp(self):
@@ -546,7 +568,7 @@ class RecordRelationsTest(ContextRecordInit, TestCase):
class ContextRecordWizardCreationTest(WizardTest, ContextRecordInit, TestCase):
- fixtures = ImportOperationTest.fixtures
+ fixtures = OPERATION_TOWNS_FIXTURES
url_name = 'record_creation'
wizard_name = 'record_wizard'
steps = views.record_creation_steps
@@ -558,22 +580,23 @@ class ContextRecordWizardCreationTest(WizardTest, ContextRecordInit, TestCase):
'general': {
'label': "First"
},
- 'relations': []
+ 'relations': [],
},
ignored=('datings',
'interpretation',
)
),
FormData(
- "Create a context record with a relation",
+ "Create a context record with a relation and datings",
form_datas={
'selec': {},
'general': {
'label': "Second"
},
- 'relations': []
+ 'relations': [],
+ 'datings': []
},
- ignored=('datings', 'interpretation',)
+ ignored=('interpretation',)
),
]
@@ -598,6 +621,14 @@ class ContextRecordWizardCreationTest(WizardTest, ContextRecordInit, TestCase):
label="Test").pk}
)
+ period = models.Period.objects.all()[0].pk
+ self.form_datas[1].append(
+ 'datings', {'period': period}
+ )
+ self.form_datas[1].append(
+ 'datings', {'period': period}
+ )
+
self.cr_nb = models.ContextRecord.objects.count()
super(ContextRecordWizardCreationTest, self).pre_wizard()
@@ -606,3 +637,6 @@ class ContextRecordWizardCreationTest(WizardTest, ContextRecordInit, TestCase):
self.cr_nb + 2)
self.assertEqual(self.related_cr.left_relations.count(),
1)
+ # identical datings, only one should be finaly save
+ cr = models.ContextRecord.objects.order_by('-pk')[0]
+ self.assertEqual(cr.datings.count(), 1)