summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2018-08-21 12:21:58 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2018-08-21 12:21:58 +0200
commit2fde9cc0367c35052068c000f5e3ee26e54b3e20 (patch)
treede2ae36f8c87a50c0ca91b74df183af1ec287075
parent521b2c11e07bcd373275ddf918e523fcdac8be03 (diff)
downloadIshtar-2fde9cc0367c35052068c000f5e3ee26e54b3e20.tar.bz2
Ishtar-2fde9cc0367c35052068c000f5e3ee26e54b3e20.zip
External ID: general filters - deduplicate filter
-rw-r--r--archaeological_finds/tests.py25
-rw-r--r--archaeological_operations/tests.py2
-rw-r--r--ishtar_common/models.py21
3 files changed, 44 insertions, 4 deletions
diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py
index c62922499..353ebbdbb 100644
--- a/archaeological_finds/tests.py
+++ b/archaeological_finds/tests.py
@@ -349,7 +349,7 @@ class FindTest(FindInit, TestCase):
self.client = Client()
self.client.login(username=self.username, password=self.password)
- def testExternalID(self):
+ def test_external_id(self):
find = self.finds[0]
base_find = find.base_finds.all()[0]
self.assertEqual(
@@ -389,6 +389,29 @@ class FindTest(FindInit, TestCase):
self.assertIn("PAT", find.external_id)
self.assertIn("PAT", base_find.external_id)
+ find.label = "hop"
+ find.save()
+ find = models.Find.objects.get(pk=find.pk)
+ # default: {get_first_base_find__context_record__external_id}-{label}
+ self.assertEqual(find.external_id, u"PAT-12345-A1-new-label-too-hop")
+ profile = get_current_profile(force=True)
+ profile.find_external_id = \
+ u"{get_first_base_find__context_record__external_id}-{label}-"\
+ u"{label}"
+ profile.save()
+ find.save()
+ find = models.Find.objects.get(pk=find.pk)
+ self.assertEqual(find.external_id,
+ u"PAT-12345-A1-new-label-too-hop-hop")
+ profile.find_external_id = \
+ u"{get_first_base_find__context_record__external_id}-{label}-" \
+ u"{label}||lower||deduplicate"
+ profile.save()
+ find.save()
+ find = models.Find.objects.get(pk=find.pk)
+ self.assertEqual(find.external_id,
+ u"pat-12345-a1-new-label-too-hop")
+
def testIndex(self):
profile = get_current_profile()
profile.find_index = u"O"
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py
index e1bf43f5a..e5c77e501 100644
--- a/archaeological_operations/tests.py
+++ b/archaeological_operations/tests.py
@@ -946,7 +946,7 @@ class OperationTest(TestCase, OperationInitTest):
ContextRecord.objects.create(label='CR-{}'.format(idx),
operation=self.item)
- def testExternalID(self):
+ def test_external_id(self):
self.item.code_patriarche = '123456789'
self.item.save()
parcel = self.get_default_parcel()
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 8d0545885..d14e9b346 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -1634,11 +1634,21 @@ class LightHistorizedItem(BaseHistorizedItem):
PARSE_FORMULA = re.compile("{([^}]*)}")
+
+def _deduplicate(value):
+ new_values = []
+ for v in value.split(u'-'):
+ if v not in new_values:
+ new_values.append(v)
+ return u'-'.join(new_values)
+
+
FORMULA_FILTERS = {
'upper': lambda x: x.upper(),
'lower': lambda x: x.lower(),
'capitalize': lambda x: x.capitalize(),
- 'slug': lambda x: slugify(x)
+ 'slug': lambda x: slugify(x),
+ 'deduplicate': _deduplicate
}
@@ -1675,7 +1685,14 @@ def get_external_id(key, item):
dct[initial_key] = unicode(obj)
for filtr in filters:
dct[initial_key] = filtr(dct[initial_key])
- return formula.format(**dct)
+ values = formula.format(**dct).split('||')
+ value = values[0]
+ for filtr in values[1:]:
+ if filtr not in FORMULA_FILTERS:
+ value += u'||' + filtr
+ continue
+ value = FORMULA_FILTERS[filtr](value)
+ return value
CURRENCY = ((u"€", _(u"Euro")),