我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.utils.encoding.iri_to_uri()。
def __init__( self, request, number, current_number, total_number, querystring_key, label=None, default_number=1, override_path=None): self._request = request self.number = number self.label = utils.text(number) if label is None else label self.querystring_key = querystring_key self.is_current = number == current_number self.is_first = number == 1 self.is_last = number == total_number self.url = utils.get_querystring_for_page( request, number, self.querystring_key, default_number=default_number) path = iri_to_uri(override_path or request.path) self.path = '{0}{1}'.format(path, self.url)
def respond_as_attachment(request, file_path, original_filename, document_root=None): if document_root is not None: file_path = os.path.join(document_root, file_path) try: fp = open(file_path, 'rb') response = HttpResponse(fp.read()) fp.close() type, encoding = mimetypes.guess_type(original_filename) if type is None: type = 'application/octet-stream' response['Content-Type'] = type response['Content-Length'] = str(os.stat(file_path).st_size) if encoding is not None: response['Content-Encoding'] = encoding url_encode() response['Content-Disposition'] = "attachment; filename*=UTF-8''%s" % iri_to_uri(original_filename) return response except Exception as e: raise Http404(e)
def add_domain(domain, url, secure=False): protocol = 'https' if secure else 'http' if url.startswith('//'): # Support network-path reference (see #16753) - RSS requires a protocol url = '%s:%s' % (protocol, url) elif not url.startswith(('http://', 'https://', 'mailto:')): url = iri_to_uri('%s://%s%s' % (protocol, domain, url)) return url
def get_absolute_url(self): # Handle script prefix manually because we bypass reverse() return iri_to_uri(get_script_prefix().rstrip('/') + self.url)
def iriencode(value): """Escapes an IRI value for use in a URL.""" return force_text(iri_to_uri(value))
def _generate_cache_key(request, method, headerlist, key_prefix): """Returns a cache key from the headers given in the header list.""" ctx = hashlib.md5() for header in headerlist: value = request.META.get(header) if value is not None: ctx.update(force_bytes(value)) url = hashlib.md5(force_bytes(iri_to_uri(request.build_absolute_uri()))) cache_key = 'views.decorators.cache.cache_page.%s.%s.%s.%s' % ( key_prefix, method, url.hexdigest(), ctx.hexdigest()) return _i18n_cache_key_suffix(request, cache_key)
def _generate_cache_header_key(key_prefix, request): """Returns a cache key for the header cache.""" url = hashlib.md5(force_bytes(iri_to_uri(request.build_absolute_uri()))) cache_key = 'views.decorators.cache.cache_header.%s.%s' % ( key_prefix, url.hexdigest()) return _i18n_cache_key_suffix(request, cache_key)
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)
def handle_simple(cls, name): try: from django.conf import settings except ImportError: prefix = '' else: prefix = iri_to_uri(getattr(settings, name, '')) return prefix
def build_absolute_uri(location, is_secure=False): # type: (str, bool, saleor.site.models.SiteSettings) -> str host = Site.objects.get_current().domain current_uri = '%s://%s' % ('https' if is_secure else 'http', host) location = urljoin(current_uri, location) return iri_to_uri(location)
def make_cache_key(self, name): return iri_to_uri('%s:%s' % (self.cache_key, name))
def make_cache_key(self, name): return iri_to_uri('%s:%s' % (self.path, name))
def get_permissions_by_username(username, directory): pootle_path = directory.pootle_path path_parts = filter(None, pootle_path.split('/')) key = iri_to_uri('Permissions:%s' % username) permissions_cache = cache.get(key, {}) if pootle_path not in permissions_cache: try: permissionset = PermissionSet.objects.filter( directory__in=directory.trail(), user__username=username).order_by('-directory__pootle_path')[0] except IndexError: permissionset = None if (len(path_parts) > 1 and path_parts[0] != 'projects' and (permissionset is None or len(filter(None, permissionset.directory.pootle_path.split('/'))) < 2)): # Active permission at language level or higher, check project # level permission try: project_path = '/projects/%s/' % path_parts[1] permissionset = PermissionSet.objects.get( directory__pootle_path=project_path, user__username=username) except PermissionSet.DoesNotExist: pass if permissionset: permissions_cache[pootle_path] = permissionset.to_dict() else: permissions_cache[pootle_path] = None cache.set(key, permissions_cache, settings.POOTLE_CACHE_TIMEOUT) return permissions_cache[pootle_path]
def save(self, *args, **kwargs): super(PermissionSet, self).save(*args, **kwargs) # FIXME: can we use `post_save` signals or invalidate caches in model # managers, please? key = iri_to_uri('Permissions:%s' % self.user.username) cache.delete(key)
def delete(self, *args, **kwargs): super(PermissionSet, self).delete(*args, **kwargs) # FIXME: can we use `post_delete` signals or invalidate caches in model # managers, please? key = iri_to_uri('Permissions:%s' % self.user.username) cache.delete(key)
def get_uri_setting(cls, name): try: from django.conf import settings except ImportError: prefix = '' else: prefix = iri_to_uri(getattr(settings, name, '')) return prefix
def show_more(context, label=None, loading=settings.LOADING): """Show the link to get the next page in a Twitter-like pagination. Usage:: {% show_more %} Alternatively you can override the label passed to the default template:: {% show_more "even more" %} You can override the loading text too:: {% show_more "even more" "working" %} Must be called after ``{% paginate objects %}``. """ # This template tag could raise a PaginationError: you have to call # *paginate* or *lazy_paginate* before including the showmore template. data = utils.get_data_from_context(context) page = data['page'] # show the template only if there is a next page if page.has_next(): request = context['request'] page_number = page.next_page_number() # Generate the querystring. querystring_key = data['querystring_key'] querystring = utils.get_querystring_for_page( request, page_number, querystring_key, default_number=data['default_number']) return { 'label': label, 'loading': loading, 'path': iri_to_uri(data['override_path'] or request.path), 'querystring': querystring, 'querystring_key': querystring_key, 'request': request, } # No next page, nothing to see. return {}
def add_query_param(request, key, val): """ Add a query parameter to the current request url, and return the new url. """ iri = request.get_full_path() uri = iri_to_uri(iri) return escape(replace_query_param(uri, key, val))
def add_domain(domain, url, secure=False): protocol = 'https' if secure else 'http' if url.startswith('//'): # Support network-path reference (see #16753) - RSS requires a protocol url = '%s:%s' % (protocol, url) elif not (url.startswith('http://') or url.startswith('https://') or url.startswith('mailto:')): url = iri_to_uri('%s://%s%s' % (protocol, domain, url)) return url