diff options
-rw-r--r-- | archaeological_finds/models.py | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/archaeological_finds/models.py b/archaeological_finds/models.py index 4259c7883..3cfa4b5e2 100644 --- a/archaeological_finds/models.py +++ b/archaeological_finds/models.py @@ -137,6 +137,14 @@ class BaseFind(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) + if q.count(): + return q.aggregate(Max('index'))['index__max'] + return 0 + def complete_id(self): # OPE|MAT.CODE|UE|FIND_index if not self.context_record.operation: @@ -152,7 +160,8 @@ class BaseFind(BaseHistorizedItem, OwnPerms): materials.add(mat.code) c_id.append(u'-'.join(sorted(list(materials)))) c_id.append(self.context_record.label) - c_id.append(unicode(self.index)) + max_index = str(self.get_max_index(ope)) + c_id.append((u'{:0' + str(len(max_index)) + 'd}').format(self.index)) return settings.JOINT.join(c_id) def short_id(self): @@ -162,7 +171,8 @@ class BaseFind(BaseHistorizedItem, OwnPerms): ope = self.context_record.operation c_id = [(ope.code_patriarche and unicode(ope.code_patriarche)) or (unicode(ope.year) + "-" + unicode(ope.operation_code))] - c_id.append(unicode(self.index)) + max_index = str(self.get_max_index(ope)) + c_id.append((u'{:0' + str(len(max_index)) + 'd}').format(self.index)) return settings.JOINT.join(c_id) def full_label(self): @@ -472,14 +482,8 @@ class Find(BaseHistorizedItem, ImageModel, OwnPerms, ShortMenuItem): modified = False if not base_find.index: modified = True - cond = { - 'context_record__operation': - base_find.context_record.operation} - idx = BaseFind.objects.filter(**cond)\ - .aggregate(Max('index')) - base_find.index = 1 - if idx and idx['index__max']: - base_find.index = idx['index__max'] + 1 + base_find.index = BaseFind.get_max_index( + base_find.context_record.operation) + 1 if not base_find.cache_short_id: base_find.cache_short_id = base_find.short_id() if base_find.cache_short_id: |