Пример #1
0
    def slugify(self, content):
        """
        Make ``content`` a valid slug.

        It uses ``unicode-slugify`` behind the scenes which works properly with
        Unicode characters.
        """
        if not content:
            return ''

        normalized = self._normalize(content)
        slugified = unicode_slugify(
            normalized,
            only_ascii=True,
            spaces=False,
            lower=True,
            ok=self.ok_chars,
            space_replacement='-',
        )

        # Remove first character wile it's an invalid character for the
        # beginning of the slug
        slugified = slugified.lstrip(self.ok_chars)

        if not slugified:
            return self.fallback_slug
        return slugified
Пример #2
0
    def pre_save(self, instance, add):
        default = super(AutoSlugField, self).pre_save(instance, add)

        if default or not add or not self.populate_from:
            return default

        inst = instance

        for attr in self.populate_from.split('.'):
            value = getattr(inst, attr)
            inst = value

        if value is None:
            return default

        try:
            slug = slugify(smart_text(value), allow_unicode=settings.UNICODE_SLUGS)
        except TypeError: 
            if settings.UNICODE_SLUGS:
                # mark as safe?
                slug = unicode_slugify(smart_text(value), ok='-_')
            else:
                slug = slugify(smart_text(value))

        slug = slug[:self.max_length].strip('-')

        # Update the model’s attribute
        setattr(instance, self.attname, slug)

        return slug
Пример #3
0
    def _serve_docs_nginx(self, request, final_project, version_slug, path,
                          download):
        """
        Serve docs from nginx.

        Returns a response with ``X-Accel-Redirect``, which will cause nginx to
        serve it directly as an internal redirect.
        """

        original_path = copy.copy(path)
        if not path.startswith('/proxito/'):
            if path[0] == '/':
                path = path[1:]
            path = f'/proxito/{path}'

        log.info('[Nginx serve] original_path=%s, proxito_path=%s, project=%s',
                 original_path, path, final_project.slug)

        content_type, encoding = mimetypes.guess_type(path)
        content_type = content_type or 'application/octet-stream'
        response = HttpResponse(f'Serving internal path: {path}',
                                content_type=content_type)
        if encoding:
            response['Content-Encoding'] = encoding

        # NGINX does not support non-ASCII characters in the header, so we
        # convert the IRI path to URI so it's compatible with what NGINX expects
        # as the header value.
        # https://github.com/benoitc/gunicorn/issues/1448
        # https://docs.djangoproject.com/en/1.11/ref/unicode/#uri-and-iri-handling
        x_accel_redirect = iri_to_uri(path)
        response['X-Accel-Redirect'] = x_accel_redirect

        if download:
            filename_ext = urlparse(path).path.rsplit('.', 1)[-1]
            domain = unicode_slugify(final_project.subdomain().replace(
                '.', '-'))
            if final_project.is_subproject:
                alias = final_project.alias
                filename = f'{domain}-{alias}-{final_project.language}-{version_slug}.{filename_ext}'  # noqa
            else:
                filename = f'{domain}-{final_project.language}-{version_slug}.{filename_ext}'
            response['Content-Disposition'] = f'filename={filename}'

        # Add debugging headers to proxito responses
        response['X-RTD-Domain'] = request.get_host()
        response['X-RTD-Project'] = final_project.slug
        response['X-RTD-Version'] = version_slug
        # Needed to strip any GET args, etc.
        response['X-RTD-Path'] = urlparse(path).path
        if hasattr(request, 'rtdheader'):
            response['X-RTD-Version-Method'] = 'rtdheader'
        if hasattr(request, 'subdomain'):
            response['X-RTD-Version-Method'] = 'subdomain'
        if hasattr(request, 'external_domain'):
            response['X-RTD-Version-Method'] = 'external_domain'
        if hasattr(request, 'cname'):
            response['X-RTD-Version-Method'] = 'cname'

        return response
Пример #4
0
    def pre_save(self, instance, add):
        default = super(AutoSlugField, self).pre_save(instance, add)

        if default or not add or not self.populate_from:
            return default

        inst = instance

        for attr in self.populate_from.split('.'):
            value = getattr(inst, attr)
            inst = value

        if value is None:
            return default

        # TODO: Django 1.9 will support unicode slugs
        if settings.ST_UNICODE_SLUGS:
            # TODO: mark as safe?
            slug = unicode_slugify(smart_text(value), ok='-')
        else:
            slug = slugify(smart_text(value))

        slug = slug[:self.max_length].strip('-')

        # Update the model’s attribute
        setattr(instance, self.attname, slug)

        return slug
Пример #5
0
    def pre_save(self, instance, add):
        default = super(AutoSlugField, self).pre_save(instance, add)

        if default or not add or not self.populate_from:
            return default

        inst = instance

        for attr in self.populate_from.split('.'):
            value = getattr(inst, attr)
            inst = value

        if value is None:
            return default

        # TODO: Django 1.9 will support unicode slugs
        if settings.ST_UNICODE_SLUGS:
            # TODO: mark as safe?
            slug = unicode_slugify(smart_text(value), ok='-')
        else:
            slug = slugify(smart_text(value))

        slug = slug[:self.max_length].strip('-')

        # Update the model’s attribute
        setattr(instance, self.attname, slug)

        return slug
Пример #6
0
def slugify_field(value, max_length):
    # TODO: Django 1.9 will support unicode slugs
    if settings.ST_UNICODE_SLUGS:
        # TODO: mark as safe?
        slug = unicode_slugify(smart_text(value), ok='-')
    else:
        slug = slugify(smart_text(value))

    return slug[:max_length].strip('-')
Пример #7
0
def slugify_field(value, max_length):
    # TODO: Django 1.9 will support unicode slugs
    if settings.ST_UNICODE_SLUGS:
        # TODO: mark as safe?
        slug = unicode_slugify(smart_text(value), ok='-')
    else:
        slug = slugify(smart_text(value))

    return slug[:max_length].strip('-')
Пример #8
0
def slugify(text, lang=None):
    """
    slugify a text. Use different method according to language
    "Here COmme the Sun" --> "here-come-the-sun"
    "Voici l'été" --> "voici-l-ete"
    "Миниаль бом" --> "Миниаль-бом"
    Args:
        text: a text to turn into a slug
        lang: the language of this text
    Returns: a slug
    """
    if lang is None:
        lang = get_language()

    if lang in get_eastern_languages():
        return unicode_slugify(text)
    else:
        return ascii_slugify(text)
def slugify(text):
    """Generates an ASCII-only slug."""
    return unicode(unicode_slugify(text, ok=SLUG_OK + ',').replace(',', '--'))
def slugify(text):
    """Generates an ASCII-only slug."""
    return unicode(unicode_slugify(text, ok=SLUG_OK + ',').replace(',', '--'))