diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-05-15 13:33:53 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-05-15 13:33:53 +0200 |
commit | ca5f4c71fc93662fe56aa7588bf5a21fa4033571 (patch) | |
tree | 53e764a95ef24c63341e3280acd48fd59e23c4f9 /archaeological_finds | |
parent | 209a201cd920692b9d7f3f33b42cc56d3bb27a1f (diff) | |
parent | b82aae46b7af06fa7dc8b52a68b91784cea0118f (diff) | |
download | Ishtar-ca5f4c71fc93662fe56aa7588bf5a21fa4033571.tar.bz2 Ishtar-ca5f4c71fc93662fe56aa7588bf5a21fa4033571.zip |
Merge branch 'master' into develop
Diffstat (limited to 'archaeological_finds')
-rw-r--r-- | archaeological_finds/models_finds.py | 89 | ||||
-rw-r--r-- | archaeological_finds/tests.py | 62 |
2 files changed, 127 insertions, 24 deletions
diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py index 4c02b170f..8d4945a0b 100644 --- a/archaeological_finds/models_finds.py +++ b/archaeological_finds/models_finds.py @@ -31,7 +31,8 @@ from ishtar_common.utils import cached_label_changed, post_save_point from ishtar_common.models import GeneralType, ImageModel, BaseHistorizedItem, \ ShortMenuItem, LightHistorizedItem, HistoricalRecords, OwnPerms, Source, \ - Person, Basket, get_external_id, post_save_cache, ValueGetter + Person, Basket, get_external_id, post_save_cache, ValueGetter, \ + get_current_profile from archaeological_operations.models import AdministrativeAct from archaeological_context_records.models import ContextRecord, Dating @@ -213,13 +214,31 @@ class BaseFind(BulkUpdatedItem, BaseHistorizedItem, OwnPerms): finds = self.find.filter().order_by("-order").all() return finds and finds[0] - @classmethod - def get_max_index(cls, operation): - q = BaseFind.objects \ - .filter(context_record__operation=operation) + def generate_index(self): + """ + Generate index based on operation or context record (based on + the configuration) + + :return: True if index has been changed. + """ + profile = get_current_profile() + if profile.find_index == u'O': + operation = self.context_record.operation + q = Find.objects \ + .filter(base_finds__context_record__operation=operation) + elif profile.find_index == u'CR': + cr = self.context_record + q = Find.objects \ + .filter(base_finds__context_record=cr) + else: + return False + if self.pk: + q = q.exclude(pk=self.pk) if q.count(): - return q.aggregate(Max('index'))['index__max'] - return 0 + self.index = q.aggregate(Max('index'))['index__max'] + 1 + else: + self.index = 1 + return True def _ope_code(self): if not self.context_record.operation: @@ -998,6 +1017,43 @@ class Find(BulkUpdatedItem, ValueGetter, BaseHistorizedItem, ImageModel, with connection.cursor() as c: c.execute(sql, args) + def generate_index(self): + """ + Generate index based on operation or context record (based on + the configuration) + + :return: True if index has been changed. + """ + bfs = self.base_finds + profile = get_current_profile() + if profile.find_index == u'O': + bfs = bfs.filter( + context_record__operation__pk__isnull=False).order_by( + '-context_record__operation__start_date') + if not bfs.count(): + return False + operation = bfs.all()[0].context_record.operation + q = Find.objects \ + .filter(base_finds__context_record__operation=operation) + elif profile.find_index == u'CR': + bfs = bfs.filter( + context_record__pk__isnull=False).order_by( + 'context_record__pk') + if not bfs.count(): + return False + cr = bfs.all()[0].context_record + q = Find.objects \ + .filter(base_finds__context_record=cr) + else: + return False + if self.pk: + q = q.exclude(pk=self.pk) + if q.count(): + self.index = q.aggregate(Max('index'))['index__max'] + 1 + else: + self.index = 1 + return True + def save(self, *args, **kwargs): super(Find, self).save(*args, **kwargs) @@ -1016,28 +1072,15 @@ class Find(BulkUpdatedItem, ValueGetter, BaseHistorizedItem, ImageModel, q = self.base_finds if not self.index and q.count(): - operation = q.filter( - context_record__operation__pk__isnull=False).order_by( - '-context_record__operation__start_date') - if operation.count(): - operation = operation.all()[0].context_record.operation - q = Find.objects \ - .filter(base_finds__context_record__operation=operation) - if self.pk: - q = q.exclude(pk=self.pk) - if q.count(): - self.index = q.aggregate(Max('index'))['index__max'] + 1 - else: - self.index = 1 + changed = self.generate_index() + if changed: self._cached_label_checked = False self.save() for base_find in self.base_finds.filter( context_record__operation__pk__isnull=False).all(): modified = False if not base_find.index: - modified = True - base_find.index = BaseFind.get_max_index( - base_find.context_record.operation) + 1 + modified = base_find.generate_index() short_id = base_find.short_id() if base_find.cache_short_id != short_id: base_find.cache_short_id = short_id diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index 432945ecb..10860c0f3 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -28,7 +28,7 @@ from django.test.client import Client from ishtar_common.models import ImporterType, IshtarUser, ImporterColumn,\ FormaterType, ImportTarget -from ishtar_common.models import Person +from ishtar_common.models import Person, get_current_profile from archaeological_context_records.models import Period, Dating from archaeological_finds import models, views from archaeological_warehouse.models import Warehouse, WarehouseType @@ -317,6 +317,66 @@ class FindTest(FindInit, TestCase): base_find.context_record.external_id, base_find.label)) + def testIndex(self): + profile = get_current_profile() + profile.find_index = u"O" + profile.save() + + op1 = self.create_operation()[-1] + op2 = self.create_operation()[-1] + op1_cr1 = self.create_context_record(data={'label': "CR1", + 'operation': op1})[-1] + op1_cr2 = self.create_context_record(data={'label': "CR2", + 'operation': op1})[-1] + op2_cr1 = self.create_context_record(data={'label': "CR3", + 'operation': op2})[-1] + self.create_finds(data_base={'context_record': op1_cr1}) + find_1 = self.finds[-1] + bf_1 = models.BaseFind.objects.get(pk=self.base_finds[-1].pk) + self.assertEqual(find_1.index, 1) + self.assertEqual(bf_1.index, 1) + + # index is based on operations + self.create_finds(data_base={'context_record': op1_cr2}) + find_2 = self.finds[-1] + bf_2 = models.BaseFind.objects.get(pk=self.base_finds[-1].pk) + self.assertEqual(find_2.index, 2) + self.assertEqual(bf_2.index, 2) + + self.create_finds(data_base={'context_record': op2_cr1}) + find_3 = self.finds[-1] + bf_3 = models.BaseFind.objects.get(pk=self.base_finds[-1].pk) + self.assertEqual(find_3.index, 1) + self.assertEqual(bf_3.index, 1) + + profile = get_current_profile() + profile.find_index = u"CR" + profile.save() + + op3 = self.create_operation()[-1] + op3_cr1 = self.create_context_record(data={'label': "CR1", + 'operation': op3})[-1] + op3_cr2 = self.create_context_record(data={'label': "CR2", + 'operation': op3})[-1] + self.create_finds(data_base={'context_record': op3_cr1}) + find_1b = self.finds[-1] + bf_1b = models.BaseFind.objects.get(pk=self.base_finds[-1].pk) + self.assertEqual(find_1b.index, 1) + self.assertEqual(bf_1b.index, 1) + + # index now based on context records + self.create_finds(data_base={'context_record': op3_cr2}) + find_2b = self.finds[-1] + bf_2b = models.BaseFind.objects.get(pk=self.base_finds[-1].pk) + self.assertEqual(find_2b.index, 1) + self.assertEqual(bf_2b.index, 1) + + self.create_finds(data_base={'context_record': op3_cr2}) + find_3b = self.finds[-1] + bf_3b = models.BaseFind.objects.get(pk=self.base_finds[-1].pk) + self.assertEqual(find_3b.index, 2) + self.assertEqual(bf_3b.index, 2) + def testShowFind(self): find = self.finds[0] response = self.client.get(reverse('display-find', args=[find.pk])) |