diff options
Diffstat (limited to 'archaeological_warehouse/models.py')
-rw-r--r-- | archaeological_warehouse/models.py | 57 |
1 files changed, 36 insertions, 21 deletions
diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index a99db2ef7..41ec901a4 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -208,7 +208,7 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem, @property def number_of_finds(self): from archaeological_finds.models import Find - return Find.objects.filter(container__responsible=self).count() + return Find.objects.filter(container_ref__location=self).count() @property def number_of_finds_hosted(self): @@ -223,14 +223,16 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem, if not remaining_division: return [current_path] current_division = remaining_division.pop(0) - q = ContainerLocalisation.objects.filter( - division=current_division, + + base_q = Container.objects.filter( + container_type=current_division, + location=self ) + q = base_q for div, ref in current_path: - q = q.filter( - container__division__division=div, - container__division__reference=ref - ) + q = base_q.filter( + parent__container_type=div, + parent__reference=ref) res = [] old_ref = None if not q.count(): @@ -252,26 +254,28 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem, :return: ordered list of available paths. Each path is a list of tuple with the WarehouseDivisionLink and the reference. """ - divisions = list( - WarehouseDivisionLink.objects.filter(warehouse=self - ).order_by('order').all()) + divisions = [ + wd.container_type + for wd in WarehouseDivisionLink.objects.filter( + warehouse=self).order_by('order').all() + ] return self._get_divisions([], divisions) - def _number_of_items_by_place(self, model, division_key='division'): + def _number_of_items_by_place(self, model, division_key): res = {} paths = self.available_division_tuples[:] for path in paths: - q = model.objects cpath = [] for division, ref in path: + q = model.objects cpath.append(ref) attrs = { - division_key + "__division": division, - division_key + "__reference": ref + division_key + "container_type": division, + division_key + "reference": ref } q = q.filter(**attrs) if tuple(cpath) not in res: - res[tuple(cpath)] = q.count() + res[tuple(cpath)] = q.distinct().count() res = [(k, res[k]) for k in res] final_res, current_res, depth = [], [], 1 len_divisions = WarehouseDivisionLink.objects.filter( @@ -283,7 +287,7 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem, depth = len(path) if path[-1] == '-': continue - path = list(path) + ['' for idx in range(len_divisions - len(path))] + path = list(path) + ['' for __ in range(len_divisions - len(path))] current_res.append((path, nb)) final_res.append(current_res[:]) return final_res @@ -291,14 +295,15 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem, def _number_of_finds_by_place(self): from archaeological_finds.models import Find return self._number_of_items_by_place( - Find, division_key='container__division') + Find, division_key='inside_container__container__') @property def number_of_finds_by_place(self, update=False): return self._get_or_set_stats('_number_of_finds_by_place', update) def _number_of_containers_by_place(self): - return self._number_of_items_by_place(Container) + return self._number_of_items_by_place( + ContainerTree, 'container_parent__') @property def number_of_containers_by_place(self, update=False): @@ -421,7 +426,7 @@ class WarehouseDivisionLink(models.Model): return self.warehouse.uuid, self.container_type.txt_idx -class ContainerTree: +class ContainerTree(models.Model): CREATE_SQL = """ CREATE VIEW containers_tree AS WITH RECURSIVE rel_tree AS ( @@ -458,6 +463,17 @@ class ContainerTree: DROP VIEW IF EXISTS container_tree; DROP VIEW IF EXISTS containers_tree; """ + container = models.OneToOneField( + "archaeological_warehouse.Container", verbose_name=_("Container"), + related_name="container_tree_child", primary_key=True) + container_parent = models.ForeignKey( + "archaeological_warehouse.Container", + verbose_name=_("Container parent"), + related_name="container_tree_parent") + + class Meta: + managed = False + db_table = 'containers_tree' class Container(DocumentItem, Merge, LightHistorizedItem, QRCodeItem, GeoItem, @@ -795,8 +811,7 @@ class Container(DocumentItem, Merge, LightHistorizedItem, QRCodeItem, GeoItem, @classmethod def get_query_owns(cls, ishtaruser): return Q(history_creator=ishtaruser.user_ptr) | \ - Q(location__person_in_charge__ishtaruser=ishtaruser) | \ - Q(responsible__person_in_charge__ishtaruser=ishtaruser) + Q(location__person_in_charge__ishtaruser=ishtaruser) def get_precise_points(self): precise_points = super(Container, self).get_precise_points() |