summaryrefslogtreecommitdiff
path: root/archaeological_warehouse/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'archaeological_warehouse/models.py')
-rw-r--r--archaeological_warehouse/models.py164
1 files changed, 157 insertions, 7 deletions
diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py
index 389cc4f5a..c3c159550 100644
--- a/archaeological_warehouse/models.py
+++ b/archaeological_warehouse/models.py
@@ -17,10 +17,16 @@
# See the file COPYING for details.
-from django.db.models.signals import post_save, post_delete
+import datetime
+
+from django.conf import settings
from django.contrib.gis.db import models
+from django.db.models.signals import post_save, post_delete
+from django.template.defaultfilters import slugify
from django.utils.translation import ugettext_lazy as _, ugettext
+from ishtar_common.utils import cached_label_changed
+
from ishtar_common.models import GeneralType, \
LightHistorizedItem, OwnPerms, Address, Person, post_save_cache
@@ -35,13 +41,20 @@ post_delete.connect(post_save_cache, sender=WarehouseType)
class Warehouse(Address, OwnPerms):
- name = models.CharField(_(u"Name"), max_length=40)
+ SHOW_URL = 'show-warehouse'
+ name = models.CharField(_(u"Name"), max_length=200)
warehouse_type = models.ForeignKey(WarehouseType,
verbose_name=_(u"Warehouse type"))
person_in_charge = models.ForeignKey(
Person, on_delete=models.SET_NULL, related_name='warehouse_in_charge',
verbose_name=_(u"Person in charge"), null=True, blank=True)
comment = models.TextField(_(u"Comment"), null=True, blank=True)
+ associated_divisions = models.ManyToManyField(
+ 'WarehouseDivision', verbose_name=_("Divisions"), blank=True,
+ through='WarehouseDivisionLink'
+ )
+
+ TABLE_COLS = ['name', 'warehouse_type']
class Meta:
verbose_name = _(u"Warehouse")
@@ -57,6 +70,54 @@ class Warehouse(Address, OwnPerms):
def __unicode__(self):
return u"%s (%s)" % (self.name, unicode(self.warehouse_type))
+ @property
+ def associated_filename(self):
+ return datetime.date.today().strftime('%Y-%m-%d') + '-' + \
+ slugify(unicode(self))
+
+ def save(self, *args, **kwargs):
+ super(Warehouse, self).save(*args, **kwargs)
+ for container in self.containers.all():
+ cached_label_changed(Container, instance=container)
+
+
+class Collection(LightHistorizedItem):
+ name = models.CharField(_(u"Name"), max_length=200,
+ null=True, blank=True)
+ description = models.TextField(_(u"Description"), null=True, blank=True)
+ warehouse = models.ForeignKey(Warehouse, verbose_name=_(u"Warehouse"),
+ related_name='collections')
+
+ class Meta:
+ verbose_name = _(u"Collection")
+ verbose_name_plural = _(u"Collection")
+ ordering = ('name',)
+
+ def __unicode__(self):
+ return self.name
+
+
+class WarehouseDivision(GeneralType):
+ class Meta:
+ verbose_name = _(u"Warehouse division")
+ verbose_name_plural = _(u"Warehouse divisions")
+post_save.connect(post_save_cache, sender=WarehouseDivision)
+post_delete.connect(post_save_cache, sender=WarehouseDivision)
+
+
+class WarehouseDivisionLink(models.Model):
+ RELATIVE_MODELS = {Warehouse: 'warehouse'}
+ warehouse = models.ForeignKey(Warehouse)
+ division = models.ForeignKey(WarehouseDivision)
+ order = models.IntegerField(_("Order"), default=10)
+
+ class Meta:
+ ordering = ('warehouse', 'order')
+ unique_together = ('warehouse', 'division')
+
+ def __unicode__(self):
+ return u"{} - {}".format(self.warehouse, self.division)
+
class ContainerType(GeneralType):
length = models.IntegerField(_(u"Length (mm)"), blank=True, null=True)
@@ -74,7 +135,8 @@ post_delete.connect(post_save_cache, sender=ContainerType)
class Container(LightHistorizedItem):
- TABLE_COLS = ['reference', 'container_type', 'location']
+ TABLE_COLS = ['reference', 'container_type__label', 'cached_location',
+ 'divisions_lbl']
# search parameters
EXTRA_REQUEST_KEYS = {
@@ -82,19 +144,107 @@ class Container(LightHistorizedItem):
'container_type': 'container_type__pk',
'reference': 'reference__icontains',
}
+ SHOW_URL = 'show-container'
+ COL_LABELS = {
+ 'cached_location': _(u"Location - index"),
+ 'divisions_lbl': _(u"Precise localisation"),
+ 'container_type__label': _(u"Type")
+ }
+ CACHED_LABELS = ['cached_label', 'cached_location']
# fields
- location = models.ForeignKey(Warehouse, verbose_name=_(u"Warehouse"))
+ location = models.ForeignKey(
+ Warehouse, verbose_name=_(u"Location (warehouse)"),
+ related_name='containers')
+ responsible = models.ForeignKey(
+ Warehouse, verbose_name=_(u"Responsible warehouse"),
+ related_name='owned_containers', blank=True, null=True)
container_type = models.ForeignKey(ContainerType,
verbose_name=_("Container type"))
reference = models.CharField(_(u"Container ref."), max_length=40)
- comment = models.TextField(_(u"Comment"))
+ comment = models.TextField(_(u"Comment"), null=True, blank=True)
+ cached_label = models.CharField(_(u"Localisation"), max_length=500,
+ null=True, blank=True)
+ cached_location = models.CharField(_(u"Cached location"), max_length=500,
+ null=True, blank=True)
+ index = models.IntegerField(u"Index", default=0)
class Meta:
verbose_name = _(u"Container")
verbose_name_plural = _(u"Containers")
+ ordering = ('cached_label',)
+ unique_together = ('index', 'location')
def __unicode__(self):
- lbl = u" - ".join((self.reference, unicode(self.container_type),
- unicode(self.location)))
+ lbl = u"{} ({})".format(self.reference, self.container_type)
return lbl
+
+ def _generate_cached_label(self):
+ items = [self.reference, self.precise_location]
+ cached_label = u" | ".join(items)
+ return cached_label
+
+ def _generate_cached_location(self):
+ items = [self.location.name, unicode(self.index)]
+ cached_label = u" - ".join(items)
+ return cached_label
+
+ @property
+ def associated_filename(self):
+ filename = datetime.date.today().strftime('%Y-%m-%d')
+ filename += u'-' + self.reference
+ filename += u"-" + self.location.name
+ filename += u"-" + unicode(self.index)
+ filename += u"-" + self.divisions_lbl
+ return slugify(filename)
+
+ @property
+ def precise_location(self):
+ return self.location.name + u" - " + self.divisions_lbl
+
+ @property
+ def divisions_lbl(self):
+ locas = [
+ u"{} {}".format(loca.division.division, loca.reference)
+ for loca in ContainerLocalisation.objects.filter(
+ container=self)
+ ]
+ return u" | ".join(locas)
+
+ def save(self, *args, **kwargs):
+ super(Container, self).save(*args, **kwargs)
+
+ if not self.index:
+ self.skip_history_when_saving = True
+ q = Container.objects.filter(location=self.location).order_by(
+ '-index')
+ if q.count():
+ self.index = q.all()[0].index + 1
+ else:
+ self.index = 1
+ self._cached_label_checked = False
+ self.save()
+
+post_save.connect(cached_label_changed, sender=Container)
+
+
+class ContainerLocalisation(models.Model):
+ container = models.ForeignKey(Container, verbose_name=_(u"Container"))
+ division = models.ForeignKey(WarehouseDivisionLink,
+ verbose_name=_(u"Division"))
+ reference = models.CharField(_(u"Reference"), max_length=200, default='')
+
+ class Meta:
+ verbose_name = _(u"Container localisation")
+ verbose_name_plural = _(u"Container localisations")
+ unique_together = ('container', 'division')
+ ordering = ('container', 'division__order')
+
+ def __unicode__(self):
+ lbl = u" - ".join((unicode(self.container),
+ unicode(self.division), self.reference))
+ return lbl
+
+ def save(self, *args, **kwargs):
+ super(ContainerLocalisation, self).save(*args, **kwargs)
+ cached_label_changed(Container, instance=self.container)