diff options
Diffstat (limited to 'archaeological_finds/models_finds.py')
-rw-r--r-- | archaeological_finds/models_finds.py | 89 |
1 files changed, 66 insertions, 23 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 |