summaryrefslogtreecommitdiff
path: root/archaeological_finds/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'archaeological_finds/models.py')
-rw-r--r--archaeological_finds/models.py25
1 files changed, 24 insertions, 1 deletions
diff --git a/archaeological_finds/models.py b/archaeological_finds/models.py
index 99a7f14cb..36702b707 100644
--- a/archaeological_finds/models.py
+++ b/archaeological_finds/models.py
@@ -257,6 +257,7 @@ class Find(BaseHistorizedItem, ImageModel, OwnPerms, ShortMenuItem):
mark = models.TextField(_(u"Mark"), blank=True, null=True)
comment = models.TextField(_(u"Comment"), blank=True, null=True)
previous_id = models.TextField(_(u"Previous ID"), blank=True, null=True)
+ index = models.IntegerField(u"Index", default=0)
history = HistoricalRecords()
def __init__(self, *args, **kwargs):
@@ -301,7 +302,7 @@ class Find(BaseHistorizedItem, ImageModel, OwnPerms, ShortMenuItem):
q = self.base_finds
if not q.count():
return
- return q.all()[0]
+ return q.order_by('-pk').all()[0]
@property
def reference(self):
@@ -310,6 +311,15 @@ class Find(BaseHistorizedItem, ImageModel, OwnPerms, ShortMenuItem):
return "00"
return bf.short_id()
+ @property
+ def administrative_index(self):
+ bf = self.get_first_base_find()
+ if not bf:
+ return
+ return "{}-{}".format(
+ bf.context_record.operation.get_reference(),
+ self.index)
+
def get_department(self):
bf = self.get_first_base_find()
if not bf:
@@ -391,6 +401,19 @@ class Find(BaseHistorizedItem, ImageModel, OwnPerms, ShortMenuItem):
def save(self, *args, **kwargs):
super(Find, self).save(*args, **kwargs)
+ q = self.base_finds
+ if not self.index and q.count():
+ # TODO: which base_find to take?
+ operation = q.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
+ self.save()
for base_find in self.base_finds.all():
if not base_find.index:
idx = BaseFind.objects\