Example #1
0
    def set_parents(self, parents):
        from apps.store.search import Collection as SearchCollection
        hier = SearchCollection.taxonomy('coll_hierarchy')
        hier.add_category(self.id)
        changed = False
        new_parents = set()
        for parent in parents:
            if parent == self.id or parent == '':
                continue
            new_parents.add(parent)

        # Remove the collection from old parents which are no longer parents
        for parent in self._parents:
            if parent in new_parents:
                continue
            try:
                pcoll = Collection.objects.get(parent)
            except KeyError:
                continue # Can get KeyError if some of the ancestors don't exist - this would need to be fixed, but failing here would make it impossible for the user to fix it.
            pcoll._remove_child(self.id)
            Collection.objects.set(pcoll)
            hier.remove_parent(self.id, parent)
            changed = True

        for parent in new_parents:
            if parent in self._parents:
                continue
            pcoll = Collection.objects.get(parent)
            pcoll._add_child(self.id)
            Collection.objects.set(pcoll)
            hier.add_parent(self.id, parent)
            changed = True

        self._parents = tuple(sorted(new_parents))
        if changed:
            self._calced_ancestors = False

        return changed