diff options
Diffstat (limited to 'archaeological_warehouse/models.py')
| -rw-r--r-- | archaeological_warehouse/models.py | 56 | 
1 files changed, 37 insertions, 19 deletions
diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index 04936d7a5..17719539b 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -247,6 +247,12 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem,              old_ref = ref['reference']              cpath = current_path[:]              cpath.append((current_division, ref['reference'])) +            remaining_division = list( +                ContainerType.objects.filter( +                    containers__parent__reference=ref['reference'], +                    containers__parent__container_type=current_division, +                    containers__location=self, +                    stationary=True).distinct())              for r in self._get_divisions(cpath, remaining_division[:],                                           depth + 1):                  res.append(r) @@ -256,32 +262,43 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem,      def available_division_tuples(self):          """          :return: ordered list of available paths. Each path is a list of -        tuple with the WarehouseDivisionLink and the reference. +        tuple with the container type and the reference.          """ -        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): +        top_divisions = list( +            ContainerType.objects.filter( +                containers__parent=None, +                containers__location=self, +                stationary=True).distinct()) +        divisions = self._get_divisions([], top_divisions) +        return divisions + +    def _number_of_items_by_place(self, model, division_key, count_filter=None):          res = {}          paths = self.available_division_tuples[:]          for path in paths:              cpath = [] +            cdiv_path = []              for division, ref in path: -                q = model.objects                  cpath.append(ref) -                attrs = { -                    division_key + "container_type": division, -                    division_key + "reference": ref -                } -                q = q.filter(**attrs) -                if tuple(cpath) not in res: -                    res[tuple(cpath)] = q.distinct().count() +                cdiv_path.append(division) +                if tuple(cpath) in res: +                    continue +                q = model.objects +                reversed_cdiv = list(reversed(cdiv_path)) +                for idx, new_ref in enumerate(reversed(cpath)): +                    division = reversed_cdiv[idx] +                    div_key = division_key + "parent__" * idx +                    attrs = { +                        div_key + "container_type": division, +                        div_key + "reference": new_ref +                    } +                    q = q.filter(**attrs) +                if count_filter: +                    q = q.filter(**{count_filter: None}) +                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(              warehouse=self).count()          for path, nb in sorted(res, key=lambda x: (len(x[0]), x[0])): @@ -307,7 +324,7 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem,      def _number_of_containers_by_place(self):          return self._number_of_items_by_place( -            ContainerTree, 'container_parent__') +            ContainerTree, 'container_parent__', 'container__children')      @property      def number_of_containers_by_place(self, update=False): @@ -721,7 +738,8 @@ class Container(DocumentItem, Merge, LightHistorizedItem, QRCodeItem, GeoItem,          help_text=_("Deprecated - do not use")      )      container_type = models.ForeignKey(ContainerType, -                                       verbose_name=_("Container type")) +                                       verbose_name=_("Container type"), +                                       related_name="containers")      reference = models.TextField(_("Container ref."))      comment = models.TextField(_("Comment"), null=True, blank=True)      cached_label = models.TextField(_("Localisation"), null=True, blank=True,  | 
