diff options
author | Étienne Loks <etienne.loks@proxience.com> | 2015-09-30 19:25:48 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@proxience.com> | 2015-09-30 19:26:09 +0200 |
commit | 2e148adc5ee132424d670ae94445f79994ef605b (patch) | |
tree | 484641822798e7ab088ebcf89473a944062f6bcb /archaeological_finds/models.py | |
parent | ad8c05a61b47839d7d386ef5adc76231b581b395 (diff) | |
download | Ishtar-2e148adc5ee132424d670ae94445f79994ef605b.tar.bz2 Ishtar-2e148adc5ee132424d670ae94445f79994ef605b.zip |
Add administrative index to finds
Diffstat (limited to 'archaeological_finds/models.py')
-rw-r--r-- | archaeological_finds/models.py | 25 |
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\ |