Esempio n. 1
0
 def __init__(self, title, link, description, language=None, author_email=None,
         author_name=None, author_link=None, subtitle=None, categories=None,
         feed_url=None, feed_copyright=None, feed_guid=None, ttl=None, **kwargs):
     to_unicode = lambda s: force_unicode(s, strings_only=True)
     if categories:
         categories = [force_unicode(c) for c in categories]
     if ttl is not None:
         # Force ints to unicode
         ttl = force_unicode(ttl)
     self.feed = {
         'title': to_unicode(title),
         'link': iri_to_uri(link),
         'description': to_unicode(description),
         'language': to_unicode(language),
         'author_email': to_unicode(author_email),
         'author_name': to_unicode(author_name),
         'author_link': iri_to_uri(author_link),
         'subtitle': to_unicode(subtitle),
         'categories': categories or (),
         'feed_url': iri_to_uri(feed_url),
         'feed_copyright': to_unicode(feed_copyright),
         'id': feed_guid or link,
         'ttl': ttl,
     }
     self.feed.update(kwargs)
     self.items = []
Esempio n. 2
0
 def add_item(self, title, link, description, author_email=None,
     author_name=None, author_link=None, pubdate=None, comments=None,
     unique_id=None, enclosure=None, categories=(), item_copyright=None,
     ttl=None, **kwargs):
     """
     Adds an item to the feed. All args are expected to be Python Unicode
     objects except pubdate, which is a datetime.datetime object, and
     enclosure, which is an instance of the Enclosure class.
     """
     to_unicode = lambda s: force_unicode(s, strings_only=True)
     if categories:
         categories = [to_unicode(c) for c in categories]
     if ttl is not None:
         # Force ints to unicode
         ttl = force_unicode(ttl)
     item = {
         'title': to_unicode(title),
         'link': iri_to_uri(link),
         'description': to_unicode(description),
         'author_email': to_unicode(author_email),
         'author_name': to_unicode(author_name),
         'author_link': iri_to_uri(author_link),
         'pubdate': pubdate,
         'comments': to_unicode(comments),
         'unique_id': to_unicode(unique_id),
         'enclosure': enclosure,
         'categories': categories or (),
         'item_copyright': to_unicode(item_copyright),
         'ttl': ttl,
     }
     item.update(kwargs)
     self.items.append(item)
Esempio n. 3
0
 def get_full_path(self):
     # RFC 3986 requires query string arguments to be in the ASCII range.
     # Rather than crash if this doesn't happen, we encode defensively.
     return "%s%s" % (
         self.path,
         self.environ.get("QUERY_STRING", "") and ("?" + iri_to_uri(self.environ.get("QUERY_STRING", ""))) or "",
     )
Esempio n. 4
0
def reverse(viewname,
            urlconf=None,
            args=None,
            kwargs=None,
            prefix=None,
            current_app=None):
    if urlconf is None:
        urlconf = get_urlconf()
    resolver = get_resolver(urlconf)
    args = args or []
    kwargs = kwargs or {}

    if prefix is None:
        prefix = get_script_prefix()

    if not isinstance(viewname, six.string_types):
        view = viewname
    else:
        parts = viewname.split(':')
        parts.reverse()
        view = parts[0]
        path = parts[1:]

        resolved_path = []
        while path:
            ns = path.pop()

            # Lookup the name to see if it could be an app identifier
            try:
                app_list = resolver.app_dict[ns]
                # Yes! Path part matches an app in the current Resolver
                if current_app and current_app in app_list:
                    # If we are reversing for a particular app, use that namespace
                    ns = current_app
                elif ns not in app_list:
                    # The name isn't shared by one of the instances (i.e., the default)
                    # so just pick the first instance as the default.
                    ns = app_list[0]
            except KeyError:
                pass

            try:
                extra, resolver = resolver.namespace_dict[ns]
                resolved_path.append(ns)
                prefix = prefix + extra
            except KeyError as key:
                if resolved_path:
                    raise NoReverseMatch(
                        "%s is not a registered namespace inside '%s'" %
                        (key, ':'.join(resolved_path)))
                else:
                    raise NoReverseMatch("%s is not a registered namespace" %
                                         key)

    return iri_to_uri('%s%s' %
                      (prefix, resolver.reverse(view, *args, **kwargs)))
Esempio n. 5
0
def _generate_cache_key(request, headerlist, key_prefix):
    """Returns a cache key from the headers given in the header list."""
    ctx = md5_constructor()
    for header in headerlist:
        value = request.META.get(header, None)
        if value is not None:
            ctx.update(value)
    path = md5_constructor(iri_to_uri(request.path))
    cache_key = 'views.decorators.cache.cache_page.%s.%s.%s' % (
        key_prefix, path.hexdigest(), ctx.hexdigest())
    return _i18n_cache_key_suffix(request, cache_key)
Esempio n. 6
0
def _generate_cache_key(request, headerlist, key_prefix):
    """Returns a cache key from the headers given in the header list."""
    ctx = md5_constructor()
    for header in headerlist:
        value = request.META.get(header, None)
        if value is not None:
            ctx.update(value)
    path = md5_constructor(iri_to_uri(request.path))
    cache_key = 'views.decorators.cache.cache_page.%s.%s.%s' % (
        key_prefix, path.hexdigest(), ctx.hexdigest())
    return _i18n_cache_key_suffix(request, cache_key)
Esempio n. 7
0
 def __init__(
     self,
     title,
     link,
     description,
     language=None,
     author_email=None,
     author_name=None,
     author_link=None,
     subtitle=None,
     categories=None,
     feed_url=None,
     feed_copyright=None,
     feed_guid=None,
     ttl=None,
     **kwargs
 ):
     to_unicode = lambda s: force_unicode(s, strings_only=True)
     if categories:
         categories = [force_unicode(c) for c in categories]
     if ttl is not None:
         # Force ints to unicode
         ttl = force_unicode(ttl)
     self.feed = {
         "title": to_unicode(title),
         "link": iri_to_uri(link),
         "description": to_unicode(description),
         "language": to_unicode(language),
         "author_email": to_unicode(author_email),
         "author_name": to_unicode(author_name),
         "author_link": iri_to_uri(author_link),
         "subtitle": to_unicode(subtitle),
         "categories": categories or (),
         "feed_url": iri_to_uri(feed_url),
         "feed_copyright": to_unicode(feed_copyright),
         "id": feed_guid or link,
         "ttl": ttl,
     }
     self.feed.update(kwargs)
     self.items = []
def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current_app=None):
    if urlconf is None:
        urlconf = get_urlconf()
    resolver = get_resolver(urlconf)
    args = args or []
    kwargs = kwargs or {}

    if prefix is None:
        prefix = get_script_prefix()

    if not isinstance(viewname, str):
        view = viewname
    else:
        parts = viewname.split(':')
        parts.reverse()
        view = parts[0]
        path = parts[1:]

        resolved_path = []
        while path:
            ns = path.pop()

            # Lookup the name to see if it could be an app identifier
            try:
                app_list = resolver.app_dict[ns]
                # Yes! Path part matches an app in the current Resolver
                if current_app and current_app in app_list:
                    # If we are reversing for a particular app, use that namespace
                    ns = current_app
                elif ns not in app_list:
                    # The name isn't shared by one of the instances (i.e., the default)
                    # so just pick the first instance as the default.
                    ns = app_list[0]
            except KeyError:
                pass

            try:
                extra, resolver = resolver.namespace_dict[ns]
                resolved_path.append(ns)
                prefix = prefix + extra
            except KeyError as key:
                if resolved_path:
                    raise NoReverseMatch("%s is not a registered namespace inside '%s'" % (key, ':'.join(resolved_path)))
                else:
                    raise NoReverseMatch("%s is not a registered namespace" % key)

    return iri_to_uri('%s%s' % (prefix, resolver.reverse(view,
            *args, **kwargs)))
Esempio n. 9
0
 def __init__(self, url, length, mime_type):
     "All args are expected to be Python Unicode objects"
     self.length, self.mime_type = length, mime_type
     self.url = iri_to_uri(url)
Esempio n. 10
0
 def get_full_path(self):
     # RFC 3986 requires query string arguments to be in the ASCII range.
     # Rather than crash if this doesn't happen, we encode defensively.
     return '%s%s' % (
         self.path, self.environ.get('QUERY_STRING', '') and
         ('?' + iri_to_uri(self.environ.get('QUERY_STRING', ''))) or '')
Esempio n. 11
0
 def get_full_path(self):
     # RFC 3986 requires self._req.args to be in the ASCII range, but this
     # doesn't always happen, so rather than crash, we defensively encode it.
     return '%s%s' % (self.path, self._req.args and
                      ('?' + iri_to_uri(self._req.args)) or '')
Esempio n. 12
0
def iriencode(value):
    """Escapes an IRI value for use in a URL."""
    return force_unicode(iri_to_uri(value))
Esempio n. 13
0
                    # so just pick the first instance as the default.
                    ns = app_list[0]
            except KeyError:
                pass

            try:
                extra, resolver = resolver.namespace_dict[ns]
                resolved_path.append(ns)
                prefix = prefix + extra
            except KeyError, key:
                if resolved_path:
                    raise NoReverseMatch("%s is not a registered namespace inside '%s'" % (key, ':'.join(resolved_path)))
                else:
                    raise NoReverseMatch("%s is not a registered namespace" % key)

    return iri_to_uri(u'%s%s' % (prefix, resolver.reverse(view,
            *args, **kwargs)))

def clear_url_caches():
    global _resolver_cache
    global _callable_cache
    _resolver_cache.clear()
    _callable_cache.clear()

def set_script_prefix(prefix):
    """
    Sets the script prefix for the current thread.
    """
    if not prefix.endswith('/'):
        prefix += '/'
    _prefixes[currentThread()] = prefix
Esempio n. 14
0
 def get_full_path(self):
     # RFC 3986 requires query string arguments to be in the ASCII range.
     # Rather than crash if this doesn't happen, we encode defensively.
     return '%s%s' % (self.path, self.environ.get('QUERY_STRING', '') and ('?' + iri_to_uri(self.environ.get('QUERY_STRING', ''))) or '')
def iriencode(value):
    """Escapes an IRI value for use in a URL."""
    return force_unicode(iri_to_uri(value))
Esempio n. 16
0
 def get_full_path(self):
     # RFC 3986 requires self._req.args to be in the ASCII range, but this
     # doesn't always happen, so rather than crash, we defensively encode it.
     return '%s%s' % (self.path, self._req.args and ('?' + iri_to_uri(self._req.args)) or '')
Esempio n. 17
0
def _generate_cache_header_key(key_prefix, request):
    """Returns a cache key for the header cache."""
    path = md5_constructor(iri_to_uri(request.path))
    cache_key = 'views.decorators.cache.cache_header.%s.%s' % (
        key_prefix, path.hexdigest())
    return _i18n_cache_key_suffix(request, cache_key)
Esempio n. 18
0
def _generate_cache_header_key(key_prefix, request):
    """Returns a cache key for the header cache."""
    path = md5_constructor(iri_to_uri(request.path))
    cache_key = 'views.decorators.cache.cache_header.%s.%s' % (
        key_prefix, path.hexdigest())
    return _i18n_cache_key_suffix(request, cache_key)
Esempio n. 19
0
            except KeyError:
                pass

            try:
                extra, resolver = resolver.namespace_dict[ns]
                resolved_path.append(ns)
                prefix = prefix + extra
            except KeyError, key:
                if resolved_path:
                    raise NoReverseMatch(
                        "%s is not a registered namespace inside '%s'" % (key, ":".join(resolved_path))
                    )
                else:
                    raise NoReverseMatch("%s is not a registered namespace" % key)

    return iri_to_uri(u"%s%s" % (prefix, resolver.reverse(view, *args, **kwargs)))


def clear_url_caches():
    global _resolver_cache
    global _callable_cache
    _resolver_cache.clear()
    _callable_cache.clear()


def set_script_prefix(prefix):
    """
    Sets the script prefix for the current thread.
    """
    if not prefix.endswith("/"):
        prefix += "/"