示例#1
0
    def invalid_slug_is_invalid(self):
        """ensure that an exception is raised when it should"""

        # exception are raised when title are invalid
        invalid_titles = [u'-', u'_', u'__', u'-_-', u'$', u'@', u'&', u'{}', u'    ', u'...']

        for t in invalid_titles:
            self.assertRaises(InvalidSlugError, slugify_raise_on_invalid, t)

        # Those slugs are recognized as wrong slug
        invalid_slugs = [
            u'',  # empty
            u'----',  # empty
            u'___',  # empty
            u'-_-',  # empty (!)
            u'&;',  # invalid characters
            u'!{',  # invalid characters
            u'@',  # invalid character
            u'a '  # space !
        ]

        for s in invalid_slugs:
            self.assertFalse(check_slug(s))

        # too long slugs are forbidden :
        too_damn_long_slug = 'a' * (settings.ZDS_APP['content']['maximum_slug_size'] + 1)
        self.assertFalse(check_slug(too_damn_long_slug))
示例#2
0
    def invalid_slug_is_invalid(self):
        """ensure that an exception is raised when it should"""

        # exception are raised when title are invalid
        invalid_titles = [
            "-", "_", "__", "-_-", "$", "@", "&", "{}", "    ", "..."
        ]

        for t in invalid_titles:
            self.assertRaises(InvalidSlugError, slugify_raise_on_invalid, t)

        # Those slugs are recognized as wrong slug
        invalid_slugs = [
            "",  # empty
            "----",  # empty
            "___",  # empty
            "-_-",  # empty (!)
            "&;",  # invalid characters
            "!{",  # invalid characters
            "@",  # invalid character
            "a ",  # space !
        ]

        for s in invalid_slugs:
            self.assertFalse(check_slug(s))

        # too long slugs are forbidden :
        too_damn_long_slug = "a" * (
            self.overridden_zds_app["content"]["maximum_slug_size"] + 1)
        self.assertFalse(check_slug(too_damn_long_slug))
示例#3
0
    def invalid_slug_is_invalid(self):
        """ensure that an exception is raised when it should"""

        # exception are raised when title are invalid
        invalid_titles = ['-', '_', '__', '-_-', '$', '@', '&', '{}', '    ', '...']

        for t in invalid_titles:
            self.assertRaises(InvalidSlugError, slugify_raise_on_invalid, t)

        # Those slugs are recognized as wrong slug
        invalid_slugs = [
            '',  # empty
            '----',  # empty
            '___',  # empty
            '-_-',  # empty (!)
            '&;',  # invalid characters
            '!{',  # invalid characters
            '@',  # invalid character
            'a '  # space !
        ]

        for s in invalid_slugs:
            self.assertFalse(check_slug(s))

        # too long slugs are forbidden :
        too_damn_long_slug = 'a' * (self.overridden_zds_app['content']['maximum_slug_size'] + 1)
        self.assertFalse(check_slug(too_damn_long_slug))
示例#4
0
    def get_unique_slug(self, title):
        """Generate a slug from the title and check if it is already in slug
        pool. If true, add a "-x" to the end, where "x" is a number
        starting from 1. When generated, it is added to the slug pool.

        Note that the slug cannot be larger than
        ``settings.ZDS_APP['content']['max_slug_size']``, due to maximum
        file size limitation.

        :param title: title from which the slug is generated (with ``slugify()``)
        :return: the unique slug
        :rtype: str
        """
        base = slugify(title)

        if not check_slug(base):
            raise InvalidSlugError(base, source=title)

        max_slug_size = settings.ZDS_APP['content']['maximum_slug_size']

        # Slugs may look like `some-article-title-1234`.
        max_suffix_digit_count = 4

        if len(base) > max_slug_size - 1 - max_suffix_digit_count:
            # There is a `- 1` because of the dash between the slug
            # itself and the numeric suffix.
            base = base[:max_slug_size] - 1 - max_suffix_digit_count

        if base not in self.slug_pool:
            self.slug_pool[base] = 1
            return base

        n = self.slug_pool[base]
        while True:
            new_slug = base + '-' + str(n)
            self.slug_pool[base] += 1
            if new_slug not in self.slug_pool:
                self.slug_pool[new_slug] = 1
                return new_slug
            n = self.slug_pool[base]
示例#5
0
    def get_unique_slug(self, title):
        """Generate a slug from the title and check if it is already in slug
        pool. If true, add a "-x" to the end, where "x" is a number
        starting from 1. When generated, it is added to the slug pool.

        Note that the slug cannot be larger than
        ``settings.ZDS_APP['content']['max_slug_size']``, due to maximum
        file size limitation.

        :param title: title from which the slug is generated (with ``slugify()``)
        :return: the unique slug
        :rtype: str
        """
        base = slugify(title)

        if not check_slug(base):
            raise InvalidSlugError(base, source=title)

        max_slug_size = settings.ZDS_APP['content']['maximum_slug_size']

        # Slugs may look like `some-article-title-1234`.
        max_suffix_digit_count = 4

        if len(base) > max_slug_size - 1 - max_suffix_digit_count:
            # There is a `- 1` because of the dash between the slug
            # itself and the numeric suffix.
            base = base[:max_slug_size] - 1 - max_suffix_digit_count

        if base not in self.slug_pool:
            self.slug_pool[base] = 1
            return base

        n = self.slug_pool[base]
        while True:
            new_slug = base + '-' + str(n)
            self.slug_pool[base] += 1
            if new_slug not in self.slug_pool:
                self.slug_pool[new_slug] = 1
                return new_slug
            n = self.slug_pool[base]
示例#6
0
    def get_unique_slug(self, title):
        """Generate a slug from title, and check if it is already in slug pool. If it is the case, recursively add a
        "-x" to the end, where "x" is a number starting from 1. When generated, it is added to the slug pool.

        Note that the slug cannot be larger than `settings.ZDS_APP['content']['max_slug_size']`, due to maximum file
        size limitation.

        :param title: title from which the slug is generated (with ``slugify()``)
        :return: the unique slug
        :rtype: str
        """
        base = slugify(title)

        if not check_slug(base):
            raise InvalidSlugError(base, source=title)

        if len(base) > settings.ZDS_APP['content']['maximum_slug_size'] - 5:
            # "-5" gives possibility to add "-xxxx" (up to 9999 possibility should be enough !)
            base = base[:settings.ZDS_APP['content']['maximum_slug_size']] - 5

        find_slug = False
        new_slug = base

        while not find_slug:  # will run until a new slug is found !
            try:
                n = self.slug_pool[base]
            except KeyError:
                self.slug_pool[base] = 1
                find_slug = True
            else:
                new_slug = base + '-' + str(n)
                self.slug_pool[base] += 1
                if new_slug not in self.slug_pool:
                    self.slug_pool[new_slug] = 1
                    find_slug = True

        return new_slug