summaryrefslogtreecommitdiff
path: root/archaeological_warehouse/models.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2021-03-09 11:14:48 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2021-03-09 11:14:48 +0100
commitb6db29397aa518cd084bba3f5933644680604e01 (patch)
treeb742211e062221e851c04f6ddcf580ac3faf395c /archaeological_warehouse/models.py
parent698bc976af6db131a7b0f7468dc12b0313dce198 (diff)
downloadIshtar-b6db29397aa518cd084bba3f5933644680604e01.tar.bz2
Ishtar-b6db29397aa518cd084bba3f5933644680604e01.zip
Container: manage calculated weight
Diffstat (limited to 'archaeological_warehouse/models.py')
-rw-r--r--archaeological_warehouse/models.py46
1 files changed, 44 insertions, 2 deletions
diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py
index b2fa7853c..089a021af 100644
--- a/archaeological_warehouse/models.py
+++ b/archaeological_warehouse/models.py
@@ -36,7 +36,7 @@ from django.apps import apps
from ishtar_common.data_importer import post_importer_action, \
pre_importer_action
from ishtar_common.model_managers import ExternalIdManager, UUIDModelManager
-from ishtar_common.models import ValueGetter
+from ishtar_common.models import ValueGetter, get_current_profile
from ishtar_common.models_common import GeneralType, \
LightHistorizedItem, OwnPerms, Address, post_save_cache, \
DashboardFormItem, document_attached_changed, SearchAltName, \
@@ -590,6 +590,7 @@ class ContainerType(GeneralType):
width = models.IntegerField(_("Width (mm)"), blank=True, null=True)
height = models.IntegerField(_("Height (mm)"), blank=True, null=True)
volume = models.FloatField(_("Volume (l)"), blank=True, null=True)
+ tare_weight = models.FloatField(_("Tare weight (g)"), blank=True, null=True)
reference = models.CharField(_("Ref."), max_length=300, blank=True,
null=True)
order = models.IntegerField(_("Order"), default=10)
@@ -758,7 +759,8 @@ class Container(DocumentItem, Merge, LightHistorizedItem,
"location__name": _("Warehouse")
}
GEO_LABEL = "cached_label"
- CACHED_LABELS = ['cached_division', 'cached_label', 'cached_location', ]
+ CACHED_LABELS = ['cached_division', 'cached_label', 'cached_location',
+ 'cached_weight']
# alternative names of fields for searches
ALT_NAMES = {
@@ -960,6 +962,13 @@ class Container(DocumentItem, Merge, LightHistorizedItem,
related_name="children", blank=True, null=True)
index = models.IntegerField(_("Container ID"), blank=True, null=True,
db_index=True)
+ weight = models.FloatField(_("Weight (g)"), blank=True, null=True)
+ calculated_weight = models.FloatField(
+ _("Calculated weight (g)"), blank=True, null=True)
+ cached_weight = models.FloatField(
+ _("Cached weight (g)"), blank=True, null=True,
+ help_text=_("Entered weight if available otherwise calculated weight.")
+ )
old_reference = models.TextField(_("Old reference"), blank=True, default="")
external_id = models.TextField(_("External ID"), blank=True, default="")
auto_external_id = models.BooleanField(
@@ -1066,6 +1075,37 @@ class Container(DocumentItem, Merge, LightHistorizedItem,
cached_label = " - ".join(items)
return cached_label
+ def _generate_cached_weight(self):
+ return self.weight if self.weight else self.calculated_weight
+
+ def _calculate_weight(self) -> bool:
+ """
+ Calculate the weight of the contained finds + tare weight of the
+ container
+ :return: True if calculated weight is changed
+ """
+ profile = get_current_profile()
+ if profile.calculate_weight_on_full and self.finds.filter(
+ weight__isnull=True).count():
+ weight = None
+ else:
+ weight = sum(
+ w for w in self.finds.filter(weight__isnull=False).values_list(
+ "weight", flat=True).all())
+ weight += self.container_type.tare_weight \
+ if self.container_type.tare_weight else 0
+ if weight != self.calculated_weight:
+ self.calculated_weight = weight
+ return True
+ return False
+
+ @property
+ def get_calculated_weight_percent(self):
+ if not self.calculated_weight or not self.weight:
+ return (self.calculated_weight
+ - self.weight) / self.calculated_weight * 100
+ return 0
+
@property
def get_cached_division(self):
return self._generate_cached_division()
@@ -1564,6 +1604,8 @@ class Container(DocumentItem, Merge, LightHistorizedItem,
self._update_warehouse_max_division()
updated = False
+ updated += self._calculate_weight()
+
if not self.index and not self.container_type.stationary:
self.skip_history_when_saving = True
q = Container.objects.filter(