示例#1
0
    def normalize(self,
                  text,
                  locale=None,
                  max_length=MAX_URL_LENGTH,
                  max_words=MAX_URL_WORDS,
                  orphans=URL_ORPHANS):
        """
        Override plone.i18n URLNormalizer to accept cutting by words.
        """
        if not isinstance(text, unicode):
            try:
                text = text.decode('utf-8')
            except Exception:
                logger.info("Can't decode URL to be normalized")

        text = urlnormalizer.normalize(text, locale, max_length)
        if not max_words:
            return text

        m = FILENAME_REGEX.match(text)
        if m is not None:
            text = m.groups()[0]
            ext = m.groups()[1]
        else:
            ext = ''

        new_text = text.split('-')
        if len(new_text) <= max_words + orphans:
            new_text = text
        else:
            new_text = '-'.join(new_text[:max_words])

        if ext:
            new_text = '.'.join((new_text, ext))
        return new_text
示例#2
0
    def _findUniqueName(self, name, obj):
        """Find a unique name in the parent folder, based on the given id, by
        appending -n, where n is a number greater than 1, or just the id if
        it's ok.
        """
        check_id = self._getCheckId(obj)

        if not check_id(name, required=1):
            return name

        ext = ''
        m = FILENAME_REGEX.match(name)
        if m is not None:
            name = m.groups()[0]
            ext = '.' + m.groups()[1]

        idx = 1
        while idx <= ATTEMPTS:
            new_name = "%s-%d%s" % (name, idx, ext)
            if not check_id(new_name, required=1):
                return new_name
            idx += 1

        # give one last attempt using the current date time before giving up
        new_name = "%s-%s%s" % (name, time.time(), ext)
        if not check_id(new_name, required=1):
            return new_name

        raise ValueError(
            "Cannot find a unique name based on %s after %d attemps." % (
                name,
                ATTEMPTS,
            )
        )
示例#3
0
def findUniqueId(context, name):
    """Find a unique name in the parent folder, based on the given id, by
    appending -n, where n is a number greater than 1, or just the id if
    it's ok.
    """
    parent = aq_inner(context)
    parent_ids = parent.objectIds()
    check_id = lambda id: id in parent_ids

    if not check_id(name):
        return name

    ext  = ''
    m = FILENAME_REGEX.match(name)
    if m is not None:
        name = m.groups()[0]
        ext  = '.' + m.groups()[1]

    idx = 1
    while idx <= ATTEMPTS:
        new_name = "%s-%d%s" % (name, idx, ext)
        if not check_id(new_name):
            return new_name
        idx += 1

    raise ValueError("Cannot find a unique name based on %s after %d attemps." % (name, ATTEMPTS,))
示例#4
0
    def normalize(self, text, locale=None, max_length=MAX_URL_LENGTH,
                  max_words=MAX_URL_WORDS, orphans=URL_ORPHANS):
        """
        Override plone.i18n URLNormalizer to accept cutting by words.
        """
        if not isinstance(text, unicode):
            try:
                text = text.decode('utf-8')
            except Exception:
                logger.info("Can't decode URL to be normalized")

        text = urlnormalizer.normalize(text, locale, max_length)
        if not max_words:
            return text

        m = FILENAME_REGEX.match(text)
        if m is not None:
            text = m.groups()[0]
            ext = m.groups()[1]
        else:
            ext = ''

        new_text = text.split('-')
        if len(new_text) <= max_words + orphans:
            new_text = text
        else:
            new_text = '-'.join(new_text[:max_words])

        if ext:
            new_text = '.'.join((new_text, ext))
        return new_text