예제 #1
0
    def add_container(self, container, generate_slug=False):
        """Add a child Container, but only if no extract were previously added and tree depth is < 2.

        .. attention::

            this function will also raise an Exception if article, because it cannot contain child container

        :param container: the new container
        :param generate_slug: if ``True``, ask the top container an unique slug for this object
        :raises InvalidOperationError: if cannot add container to this one. Please use ``can_add_container`` to check\
        this before calling ``add_container``.
        """
        if self.can_add_container():
            if generate_slug:
                container.slug = self.get_unique_slug(container.title)
            else:
                self.add_slug_to_pool(container.slug)
            container.parent = self
            container.position_in_parent = self.get_last_child_position() + 1
            self.children.append(container)
            self.children_dict[container.slug] = container
        else:
            raise InvalidOperationError(
                _(u"Impossible d'ajouter un conteneur au conteneur « {} »").
                format(self.title))
예제 #2
0
    def add_container(self, container, generate_slug=False):
        """Add a child Container, but only if no extract were previously added
        and tree depth is lower than 2.

        .. attention::

            This function will raise an Exception if the publication
            is an article

        :param container: the new container
        :param generate_slug: if ``True``, asks the top container a unique slug for this object
        :raises InvalidOperationError: if the new container cannot be added. Please use\
        ``can_add_container`` first to make sure you can use ``add_container``.
        """
        if self.can_add_container():
            if generate_slug:
                container.slug = self.get_unique_slug(container.title)
            else:
                self.__add_slug_to_pool(container.slug)
            container.parent = self
            container.position_in_parent = self.get_last_child_position() + 1
            self.children.append(container)
            self.children_dict[container.slug] = container
        else:
            raise InvalidOperationError(_("Impossible d'ajouter un conteneur au conteneur « {} »").format(self.title))
예제 #3
0
    def __add_slug_to_pool(self, slug):
        """Add a slug to the slug pool to be taken into account when
        generating a unique slug.

        :param slug: the slug to add
        :raise InvalidOperationErrpr: if the slug already exists

        """
        if slug in self.slug_pool:
            raise InvalidOperationError(
                _('Le slug « {} » est déjà présent dans le conteneur « {} »').format(slug, self.title))
        self.slug_pool[slug] = 1
예제 #4
0
    def add_slug_to_pool(self, slug):
        """Add a slug to the slug pool to be taken into account when generate a unique slug

        :param slug: the slug to add
        :raise InvalidOperationErrpr: if the slug already exists
        """
        try:
            self.slug_pool[slug]  # test access
        except KeyError:
            self.slug_pool[slug] = 1
        else:
            raise InvalidOperationError(
                _(u'Le slug « {} » est déjà présent dans le conteneur « {} »').format(slug, self.title))
예제 #5
0
    def add_extract(self, extract, generate_slug=False):
        """Add a child container, but only if no container were previously added

        :param extract: the new extract
        :param generate_slug: if ``True``, ask the top container a unique slug for this object
        :raise InvalidOperationError: if the extract can't be added to this container.
        """
        if self.can_add_extract():
            if generate_slug:
                extract.slug = self.get_unique_slug(extract.title)
            else:
                self.__add_slug_to_pool(extract.slug)
            extract.container = self
            extract.position_in_parent = self.get_last_child_position() + 1
            self.children.append(extract)
            self.children_dict[extract.slug] = extract
        else:
            raise InvalidOperationError(_("Impossible d'ajouter un extrait au conteneur « {} »").format(self.title))