summaryrefslogtreecommitdiff
path: root/ishtar/furnitures/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar/furnitures/models.py')
-rw-r--r--ishtar/furnitures/models.py56
1 files changed, 46 insertions, 10 deletions
diff --git a/ishtar/furnitures/models.py b/ishtar/furnitures/models.py
index 44e0e9391..1e0072c45 100644
--- a/ishtar/furnitures/models.py
+++ b/ishtar/furnitures/models.py
@@ -27,7 +27,7 @@ from django.core.validators import validate_slug
from django.utils.translation import ugettext_lazy as _, ugettext
from django.db.utils import DatabaseError
from django.utils.safestring import SafeUnicode
-from django.db.models import Q
+from django.db.models import Q, Max
from django.contrib.auth.models import User
from django.contrib.gis.db import models
@@ -804,6 +804,8 @@ class BaseItem(BaseHistorizedItem, OwnPerms):
related_name='base_items', verbose_name=_(u"Context Record"))
is_isolated = models.NullBooleanField(_(u"Is isolated?"), blank=True,
null=True)
+ index = models.IntegerField(u"Index", default=0)
+ material_index = models.IntegerField(u"Material index", default=0)
documentations = models.ManyToManyField(Source)
history = HistoricalRecords()
@@ -822,25 +824,45 @@ class BaseItem(BaseHistorizedItem, OwnPerms):
def get_last_item(self):
#TODO: manage virtuals - property(last_item) ?
- return self.item.filter().order_by("-order").all()[0]
+ items = self.item.filter().order_by("-order").all()
+ return items and items[0]
def full_label(self):
return self._real_label() or self._temp_label()
+ def material_type_label(self):
+ item = self.get_last_item()
+ lbl = item and (unicode(item.material_type) + unicode(_(": "))) or ''
+ if self.context_record.parcel.operation.code_patriarche:
+ return lbl + JOINT.join([unicode(it) for it in (
+ self.context_record.parcel.operation.code_patriarche,
+ self.context_record.label,
+ self.material_index,
+ self.label)])
+ return lbl + JOINT.join([unicode(it) for it in (
+ self.context_record.parcel.year,
+ self.index,
+ self.context_record.label,
+ self.material_index,
+ self.label)])
+
+
def _real_label(self):
if not self.context_record.parcel.operation.code_patriarche:
return
- return JOINT.join((self.context_record.parcel.operation.code_patriarche,
+ return JOINT.join([unicode(it) for it in (
+ self.context_record.parcel.operation.code_patriarche,
self.context_record.label,
- self.label))
+ self.label)])
def _temp_label(self):
if self.context_record.parcel.operation.code_patriarche:
return
- return JOINT.join((self.context_record.parcel.year,
- #TODO:self.index
+ return JOINT.join([unicode(it) for it in (
+ self.context_record.parcel.year,
+ self.index,
self.context_record.label,
- self.label))
+ self.label)])
class Item(BaseHistorizedItem, OwnPerms):
TABLE_COLS = ['base_items.context_record.parcel.town',
@@ -881,9 +903,23 @@ class Item(BaseHistorizedItem, OwnPerms):
def __unicode__(self):
return self.label
- def material_type_label(self):
- #TODO to complete cf. #372 and sheet_contextrecord template
- return unicode(self.material_type)
+ def save(self, *args, **kwargs):
+ if not self.pk:
+ super(Item, self).save(*args, **kwargs)
+ for base_item in self.base_items.all():
+ if not base_item.index:
+ idx = BaseItem.objects.filter(context_record=\
+ base_item.context_record).aggregate(Max('index'))
+ base_item.index = idx and idx['index__max'] + 1 or 1
+ if not base_item.material_index:
+ idx = BaseItem.objects.filter(context_record=\
+ base_item.context_record,
+ item__material_type=self.material_type).aggregate(
+ Max('material_index'))
+ base_item.material_index = idx and \
+ idx['material_index__max'] + 1 or 1
+ base_item.save()
+ super(Item, self).save(*args, **kwargs)
class ParcelOwner(LightHistorizedItem):