def urlencode(self, safe=None): """ Returns an encoded string of all query string arguments. :arg safe: Used to specify characters which do not require quoting, for example:: >>> q = QueryDict('', mutable=True) >>> q['next'] = '/a&b/' >>> q.urlencode() 'next=%2Fa%26b%2F' >>> q.urlencode(safe='/') 'next=/a%26b/' """ output = [] if safe: safe = force_bytes(safe, self.encoding) encode = lambda k, v: '%s=%s' % ((quote(k, safe), quote(v, safe))) else: encode = lambda k, v: urlencode({k: v}) for k, list_ in self.lists(): k = force_bytes(k, self.encoding) output.extend(encode(k, force_bytes(v, self.encoding)) for v in list_) return '&'.join(output)
def _api_get_node(self, repository, path, revision, base_commit_id, contents=False): # Unless we're fetching raw content, we optimistically want to # grab the metadata for the file. That's going to be a lot smaller # than the file contents in most cases. However, we can only do that # with a base_commit_id. If we don't have that, we fall back on # fetching the full file contents. is_git = repository.tool.name == "Git" if is_git and (contents or not base_commit_id): url_path = "blob?id=%s&name=%s" % (quote(revision), quote(os.path.basename(path))) raw_content = True else: if is_git: expected_revision = base_commit_id else: expected_revision = revision url_path = "node.json?path=%s&revision=%s" % (quote(path), quote(expected_revision)) if contents: url_path += "&contents=1" raw_content = False url = self._build_api_url( self._get_repository_account_domain(repository), "repositories/%s/%s" % (repository.extra_data["beanstalk_repo_name"], url_path), ) result = self._api_get(url, raw_content=raw_content) if not raw_content and contents: result = result["contents"] return result
def _api_get_src(self, repository, path, revision, base_commit_id): # If a base commit ID is provided, use it. It may not be provided, # though, and in this case, we need to use the provided revision, # which will work for Mercurial but not for Git. # # If not provided, and using Git, we'll give the user a File Not # Found error with some info on what they need to do to correct # this. if base_commit_id: revision = base_commit_id elif repository.tool.name == 'Git': raise FileNotFoundError( path, revision, detail='The necessary revision information needed to find ' 'this file was not provided. Use RBTools 0.5.2 or ' 'newer.') url = self._build_api_url( 'repositories/%s/%s/raw/%s/%s' % (quote(self._get_repository_owner(repository)), quote(self._get_repository_name(repository)), quote(revision), quote(path))) try: return self._api_get(url, raw_content=True) except FileNotFoundError: raise FileNotFoundError(path, revision=revision, base_commit_id=base_commit_id)
def _api_get_svn_props(self, repository, path, revision): """Return the SVN properties for a file in the repository. This will query for all SVN properties set for a particular file, returning them as a dictionary mapping property names to values. Args: repository (reviewboard.scmtools.models.Repository): The Subversion repository containing the file. path (unicode): The path to the file to retrieve properties for. revision (unicode): The revision of the file. Returns: dict: A mapping of property names to values. """ url = self._build_api_url( self._get_repository_account_domain(repository), 'repositories/%s/props.json?path=%s&revision=%s' % (repository.extra_data['beanstalk_repo_name'], quote(path), quote(revision))) result = self._api_get(url) return result.get('svn_properties', {})
def get_file(self, repository, path, revision, base_commit_id=None, *args, **kwargs): """Fetches a file from Unfuddle. This will perform an API request to fetch the contents of a file. If using Git, this will expect a base commit ID to be provided. """ try: commit_id = self._get_commit_id(repository, path, revision, base_commit_id) url = self._build_api_url( self._get_repository_account_domain(repository), 'repositories/%s/download/?path=%s&commit=%s' % (self._get_repository_id(repository), quote(path), quote(commit_id))) return self._api_get(url, raw_content=True) except (HTTPError, URLError): raise FileNotFoundError(path, revision)
def config_url(self): """ A URL for configuring Google Authenticator or similar. See https://github.com/google/google-authenticator/wiki/Key-Uri-Format. The issuer is taken from :setting:`OTP_TOTP_ISSUER`, if available. """ label = self.user.get_username() params = { 'secret': b32encode(self.bin_key), 'algorithm': 'SHA1', 'digits': self.digits, 'period': self.step, } urlencoded_params = urlencode(params) issuer = getattr(settings, 'OTP_TOTP_ISSUER', None) if isinstance(issuer, string_types) and (issuer != ''): issuer = issuer.replace(':', '') label = '{}:{}'.format(issuer, label) urlencoded_params += '&issuer={}'.format( quote(issuer)) # encode issuer as per RFC 3986, not quote_plus url = 'otpauth://totp/{}?{}'.format(quote(label), urlencoded_params) return url
def _build_repository_api_url(self, repository, path='', **kwargs): """Build an API URL for the given repository. This is a wrapper around :py:meth:`_build_api_url` for repository-based APIs. Args: repository (reviewboard.scmtools.models.Repository): The repository. path (unicode, optional): Optional extra path relative to the resource for this repository. If left blank, the repository's resource URL will be returned. **kwargs (dict): Extra positional argument to pass to :py:meth:`_build_api_url`. Returns: unicode: The API URL. """ username = self._get_repository_owner(repository) repo_name = self._get_repository_name(repository) return self._build_api_url( 'repositories/%s/%s/%s' % (quote(username), quote(repo_name), path), **kwargs)
def query_string(self): """returns part of the url to the main page, responsible to display the full text search results, taking into account sort method, selected scope and search tags""" lst = ['scope:' + self.scope, 'sort:' + self.sort] """ ATTN: a copy from urls.py r'(%s)?' % r'/scope:(?P<scope>\w+)' + r'(%s)?' % r'/sort:(?P<sort>[\w\-]+)' + r'(%s)?' % r'/tags:(?P<tags>[\w+.#,-]+)' + # Should match: const.TAG_CHARS + ','; TODO: Is `#` char decoded by the time URLs are processed ?? r'(%s)?' % r'/author:(?P<author>\d+)' + r'(%s)?' % r'/page:(?P<page>\d+)' + r'(%s)?' % r'/query:(?P<query>.+)' + # INFO: query is last, b/c it can contain slash!!! """ # order of items is important!!! if self.tags: lst.append('tags:' + quote(smart_str(const.TAG_SEP.join(self.tags)), safe=self.SAFE_CHARS)) if self.author: lst.append('author:' + str(self.author)) if self.page: lst.append('page:' + str(self.page)) if self.query: lst.append('query:' + quote(smart_str(self.query), safe=self.SAFE_CHARS)) return '/'.join(lst) + '/'
def _build_repository_api_url(self, repository, path='', **kwargs): """Build an API URL for the given repository. This is a wrapper around :py:meth:`_build_api_url` for repository-based APIs. Args: repository (reviewboard.scmtools.models.Repository): The repository. path (unicode, optional): Optional extra path relative to the resource for this repository. If left blank, the repository's resource URL will be returned. **kwargs (dict): Extra positional argument to pass to :py:meth:`_build_api_url`. Returns: unicode: The API URL. """ username = self._get_repository_owner(repository) repo_name = self._get_repository_name(repository) return self._build_api_url('repositories/%s/%s/%s' % (quote(username), quote(repo_name), path), **kwargs)
def urlencode(self, safe=None): """ Returns an encoded string of all query string arguments. :arg safe: Used to specify characters which do not require quoting, for example:: >>> q = QueryDict('', mutable=True) >>> q['next'] = '/a&b/' >>> q.urlencode() 'next=%2Fa%26b%2F' >>> q.urlencode(safe='/') 'next=/a%26b/' """ output = [] if safe: safe = force_bytes(safe, self.encoding) encode = lambda k, v: '%s=%s' % ((quote(k, safe), quote(v, safe))) else: encode = lambda k, v: urlencode({k: v}) for k, list_ in self.lists(): k = force_bytes(k, self.encoding) output.extend( encode(k, force_bytes(v, self.encoding)) for v in list_) return '&'.join(output)
def get_file_exists(self, repository, path, revision, base_commit_id=None, *args, **kwargs): """Determines if a file exists. This will perform an API request to fetch the metadata for a file. If using Git, this will expect a base commit ID to be provided. """ try: commit_id = self._get_commit_id(repository, path, revision, base_commit_id) url = self._build_api_url( self._get_repository_account_domain(repository), 'repositories/%s/history/?path=%s&commit=%s&count=0' % (self._get_repository_id(repository), quote(path), quote(commit_id))) self._api_get(url) return True except (HTTPError, URLError, FileNotFoundError): return False
def _get_file_api_url(self, repo_name, revision, base_commit_id=None, path=None): """Return the URL for accessing information about a file. A revision or a (base commit ID, path) pair is expected to be provided. By default, this will return the URL based on the revision, if both are provided. Args: repo_name (unicode): The name of the repository registered on RB Gateway. revision (unicode): The revision of the file to retrieve. base_commit_id (unicode, optional): The ID of the commit that the file was changed in. This may not be provided, and is dependent on the type of repository. path (unicode, optional): The file path. Returns: unicode: The URL for fetching file information. """ if revision and revision != UNKNOWN: return ('%s/file/%s' % (self._get_repos_api_url(repo_name), quote(revision))) else: return ('%s/path/%s' % (self._get_commits_api_url( repo_name, base_commit_id), quote(path)))
def _api_get_src(self, repository, path, revision, base_commit_id): # If a base commit ID is provided, use it. It may not be provided, # though, and in this case, we need to use the provided revision, # which will work for Mercurial but not for Git. # # If not provided, and using Git, we'll give the user a File Not # Found error with some info on what they need to do to correct # this. if base_commit_id: revision = base_commit_id elif repository.tool.name == 'Git': raise FileNotFoundError( path, revision, detail='The necessary revision information needed to find ' 'this file was not provided. Use RBTools 0.5.2 or ' 'newer.') # NOTE: As of this writing, the 2.0 API does not support fetching # the raw contents of files. We have to use the 1.0 API for # this instead. url = self._build_repository_api_url(repository, 'raw/%s/%s' % (quote(revision), quote(path)), version='1.0') try: return self._api_get(url, raw_content=True) except FileNotFoundError: raise FileNotFoundError(path, revision=revision, base_commit_id=base_commit_id)
def _api_get_src(self, repository, path, revision, base_commit_id): # If a base commit ID is provided, use it. It may not be provided, # though, and in this case, we need to use the provided revision, # which will work for Mercurial but not for Git. # # If not provided, and using Git, we'll give the user a File Not # Found error with some info on what they need to do to correct # this. if base_commit_id: revision = base_commit_id elif repository.tool.name == 'Git': raise FileNotFoundError( path, revision, detail='The necessary revision information needed to find ' 'this file was not provided. Use RBTools 0.5.2 or ' 'newer.') # NOTE: As of this writing, the 2.0 API does not support fetching # the raw contents of files. We have to use the 1.0 API for # this instead. url = self._build_repository_api_url( repository, 'raw/%s/%s' % (quote(revision), quote(path)), version='1.0') try: return self._api_get(url, raw_content=True) except FileNotFoundError: raise FileNotFoundError(path, revision=revision, base_commit_id=base_commit_id)
def get(self, request, *args, **kwargs): #Log the user out. auth.logout(self.request) #Get target url in order of preference. target = LOGOUT_REDIRECT_URL or\ quote(self.request.GET.get(self.redirect_field_name, '')) or\ quote(request.build_absolute_uri()) logout = LOGOUT_URL % target return redirect(logout)
def _encode(self, k, v, safe=None): if safe is None: safe="" if v is None: return quote(k, safe) v = force_bytes(v, self.encoding) return '%s=%s' % ((quote(k, safe), quote(v, safe)))
def _encode(self, k, v, safe=None): if safe is None: safe = "" if v is None: return quote(k, safe) v = force_bytes(v, self.encoding) return '%s=%s' % ((quote(k, safe), quote(v, safe)))
def urlquote(url, safe='/'): """ A version of Python's ``urllib.quote()`` function that can operate on unicode strings. The url is first UTF-8 encoded before quoting. The returned string can safely be used as part of an argument to a subsequent ``iri_to_uri()`` call without double-quoting occurring. """ if six.PY3: return quote(url, safe) else: return latin_to_unicode(quote(force_bytes(url), force_bytes(safe)))
def _api_get_file_meta(self, repository, path, revision, base_commit_id): """Return metadata on a file. Args: repository (reviewboard.scmtools.models.Repository): The repository containing the file. path (unicode): The path to the file. revision (unicode): The revision of the file. base_commit_id (unicode): The SHA1 of the commit to fetch the file at. If provided, this will take precedence over ``revision``. This is needed by Git. Returns: dict: The metadata on the file. Raises: reviewboard.scmtools.errors.FileNotFoundError: The file could not be found. """ # If a base commit ID is provided, use it. It may not be provided, # though, and in this case, we need to use the provided revision, # which will work for Mercurial but not for Git. # # If not provided, and using Git, we'll give the user a File Not # Found error with some info on what they need to do to correct # this. if base_commit_id: revision = base_commit_id elif repository.tool.name == 'Git': raise FileNotFoundError( path, revision, detail='The necessary revision information needed to find ' 'this file was not provided. Use RBTools 0.5.2 or ' 'newer.') url = ('%s?format=meta' % self._build_repository_api_url( repository, 'src/%s/%s' % (quote(revision), quote(path)))) try: return self.api_get(url) except FileNotFoundError: raise FileNotFoundError(path, revision=revision, base_commit_id=base_commit_id)
def get(self, request, *args, **kwargs): #Log the user out. auth.logout(self.request) #Set session key that middleware will use to force #Shibboleth reauthentication. self.request.session[LOGOUT_SESSION_KEY] = True #Get target url in order of preference. target = LOGOUT_REDIRECT_URL or\ quote(self.request.GET.get(self.redirect_field_name, '')) or\ quote(request.build_absolute_uri()) logout = LOGOUT_URL % target return redirect(logout)
def convertit_url(url, from_type=None, to_type=None, proxy=False): if not to_type: to_type = 'application/pdf' mimetype = to_type if '/' not in mimetype: extension = '.' + mimetype if not mimetype.startswith('.') else mimetype mimetype = types_map[extension] fromparam = ("&from=%s" % quote(from_type)) if from_type is not None else '' params = 'url={url}{fromparam}&to={to}'.format(url=quote(url), fromparam=fromparam, to=quote(mimetype)) url = '{server}/?{params}'.format(server=app_settings['CONVERSION_SERVER'], params=params) return url
def get_login_url(request): """Calculate which login_url to use""" default_new_url = '{0}?origin={1}&noaccess'.format( LOGIN_URL, parse.quote(request.get_full_path()) ) remote_loginurl = get_remote_loginurl(request) return remote_loginurl if remote_loginurl else default_new_url
def capture_url(url, width=None, height=None, selector=None, waitfor=None): """Return URL to request a capture from Screamshotter """ server = app_settings['CAPTURE_SERVER'] width = ('&width=%s' % width) if width else '' height = ('&height=%s' % height) if height else '' selector = ('&selector=%s' % quote(selector)) if selector else '' waitfor = ('&waitfor=%s' % quote(waitfor)) if waitfor else '' params = '{width}{height}{selector}{waitfor}'.format(width=width, height=height, selector=selector, waitfor=waitfor) capture_url = '{server}/?url={url}{params}'.format(server=server, url=quote(url), params=params) return capture_url
def test_map_image(self, mock_requests): if self.model is None: return # Abstract test should not run SuperUserFactory.create(username='******', password='******') self.client.login(username='******', password='******') obj = self.modelfactory.create(geom='POINT(0 0)') # Initially, map image does not exists image_path = obj.get_map_image_path() if os.path.exists(image_path): os.remove(image_path) self.assertFalse(os.path.exists(image_path)) # Mock Screenshot response mock_requests.get.return_value.status_code = 200 mock_requests.get.return_value.content = '*' * 100 response = self.client.get(obj.map_image_url) self.assertEqual(response.status_code, 200) self.assertTrue(os.path.exists(image_path)) mapimage_url = '%s%s?context' % (self.live_server_url, obj.get_detail_url()) screenshot_url = 'http://0.0.0.0:8001/?url=%s' % quote(mapimage_url) url_called = mock_requests.get.call_args_list[0] self.assertTrue(url_called.startswith(screenshot_url))
def get_context_data(self, **kwargs): context = super(CalendarByPeriodsView, self).get_context_data(**kwargs) calendar = self.object period_class = self.kwargs['period'] try: date = coerce_date_dict(self.request.GET) except ValueError: raise Http404 if date: try: date = datetime.datetime(**date) except ValueError: raise Http404 else: date = timezone.now() event_list = GET_EVENTS_FUNC(self.request, calendar) local_timezone = timezone.get_current_timezone() period = period_class(event_list, date, tzinfo=local_timezone) context.update({ 'date': date, 'period': period, 'calendar': calendar, 'weekday_names': weekday_names, 'here': quote(self.request.get_full_path()), }) return context
def cacheKey(cls): theme = list(cls.registered_hooks)[0].theme return parse.quote( "{}-{}-{}".format( kolibri.__version__, theme[THEME_NAME], theme[THEME_VERSION] ) )
def unquote_quote(segment): segment = unquote(force_str(segment)) # Tilde is part of RFC3986 Unreserved Characters # http://tools.ietf.org/html/rfc3986#section-2.3 # See also http://bugs.python.org/issue16285 segment = quote(segment, safe=RFC3986_SUBDELIMS + RFC3986_GENDELIMS + str('~')) return force_text(segment)
def config_url(self): """ A URL for configuring Google Authenticator or similar. See https://github.com/google/google-authenticator/wiki/Key-Uri-Format. The issuer is taken from :setting:`OTP_HOTP_ISSUER`, if available. """ label = self.user.get_username() params = { 'secret': b32encode(self.bin_key), 'algorithm': 'SHA1', 'digits': self.digits, 'counter': self.counter, } issuer = getattr(settings, 'OTP_HOTP_ISSUER', None) if isinstance(issuer, string_types) and (issuer != ''): issuer = issuer.replace(':', '') params['issuer'] = issuer label = '{}:{}'.format(issuer, label) url = 'otpauth://hotp/{}?{}'.format(quote(label), urlencode(params)) return url
def _do_on_path(self, cb, path, revision=HEAD): if not path: raise FileNotFoundError(path, revision) try: normpath = self.normalize_path(path) # SVN expects to have URLs escaped. Take care to only # escape the path part of the URL. if self.client.is_url(normpath): pathtuple = urlsplit(normpath) path = pathtuple[2] if isinstance(path, six.text_type): path = path.encode('utf-8', 'ignore') normpath = urlunsplit((pathtuple[0], pathtuple[1], quote(path), '', '')) normrev = self._normalize_revision(revision) return cb(normpath, normrev) except ClientError as e: exc = force_text(e) if 'File not found' in exc or 'path not found' in exc: raise FileNotFoundError(path, revision, detail=exc) elif 'callback_ssl_server_trust_prompt required' in exc: raise SCMError( _('HTTPS certificate not accepted. Please ensure that ' 'the proper certificate exists in %s ' 'for the user that reviewboard is running as.') % os.path.join(self.config_dir, 'auth')) else: raise SVNTool.normalize_error(e)
def _get_commits_api_url(self, repo_name, commit_id=None, branch_name=None): """Return the API URL for commits on a repository. Args: repo_name (unicode): The name of the repository. commit_id (unicode, optional): The optional commit ID to include in the URL. branch_name (unicode, optional): The optional branch to fetch commits from. Returns: unicode: The URL for working with a list of commits or a specific commit. """ if branch_name is None or commit_id is not None: url = '%s/commits' % self._get_repos_api_url(repo_name) else: url = '%s/commits' % self._get_branches_api_url( repo_name, branch_name) if commit_id is not None: url = '%s/%s' % (url, quote(commit_id)) return url
def iri_to_uri(iri): """ Convert an Internationalized Resource Identifier (IRI) portion to a URI portion that is suitable for inclusion in a URL. This is the algorithm from section 3.1 of RFC 3987. However, since we are assuming input is either UTF-8 or unicode already, we can simplify things a little from the full method. Takes an IRI in UTF-8 bytes (e.g. '/I \xe2\x99\xa5 Django/') or unicode (e.g. '/I ♥ Django/') and returns ASCII bytes containing the encoded result (e.g. '/I%20%E2%99%A5%20Django/'). """ # The list of safe characters here is constructed from the "reserved" and # "unreserved" characters specified in sections 2.2 and 2.3 of RFC 3986: # reserved = gen-delims / sub-delims # gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" # sub-delims = "!" / "$" / "&" / "'" / "(" / ")" # / "*" / "+" / "," / ";" / "=" # unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" # Of the unreserved characters, urllib.quote already considers all but # the ~ safe. # The % character is also added to the list of safe characters here, as the # end of section 3.1 of RFC 3987 specifically mentions that % must not be # converted. if iri is None: return iri return quote(force_bytes(iri), safe=b"/#%[]=:;$&()+,!?*@'~")
def send(self): api = "https://api.kavenegar.com/v1/" + API_KEY + "/sms/send.json" coded_message = quote(self.message.encode('utf8')) data = { 'receptor': self.receptor, 'message': coded_message, 'sender': self.sender, } try: response = get(api, params=data) except: raise APIError("Kavenegar", "can not connect") if (response.json()['return']['status'] == 200): self.message_id = int(response.json()['entries'][0]['messageid']) self.message = response.json()['entries'][0]['message'].encode( 'utf-8') self.send_date = timezone.make_aware( datetime.now(), timezone.get_default_timezone()) self.gone = True self.save() self.check_status() return response.json()['return']['status'] else: try: status = STATUS_CODES[str(response.json()['return']['status'])] except: status = str(response.json()['return']['status']) return status
def iri_to_uri(iri): """ Convert an Internationalized Resource Identifier (IRI) portion to a URI portion that is suitable for inclusion in a URL. This is the algorithm from section 3.1 of RFC 3987. However, since we are assuming input is either UTF-8 or unicode already, we can simplify things a little from the full method. Returns an ASCII string containing the encoded result. """ # The list of safe characters here is constructed from the "reserved" and # "unreserved" characters specified in sections 2.2 and 2.3 of RFC 3986: # reserved = gen-delims / sub-delims # gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" # sub-delims = "!" / "$" / "&" / "'" / "(" / ")" # / "*" / "+" / "," / ";" / "=" # unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" # Of the unreserved characters, urllib.quote already considers all but # the ~ safe. # The % character is also added to the list of safe characters here, as the # end of section 3.1 of RFC 3987 specifically mentions that % must not be # converted. if iri is None: return iri return quote(force_bytes(iri), safe=b"/#%[]=:;$&()+,!?*@'~")
def _do_on_path(self, cb, path, revision=HEAD): if not path: raise FileNotFoundError(path, revision) try: normpath = self.normalize_path(path) # SVN expects to have URLs escaped. Take care to only # escape the path part of the URL. if self.client.is_url(normpath): pathtuple = urlsplit(normpath) path = pathtuple[2] if isinstance(path, six.text_type): path = path.encode("utf-8", "ignore") normpath = urlunsplit((pathtuple[0], pathtuple[1], quote(path), "", "")) normrev = self._normalize_revision(revision) return cb(normpath, normrev) except ClientError as e: exc = bytes(e).decode("utf-8") if "File not found" in exc or "path not found" in exc: raise FileNotFoundError(path, revision, detail=exc) elif "callback_ssl_server_trust_prompt required" in exc: raise SCMError( _( "HTTPS certificate not accepted. Please ensure that " "the proper certificate exists in %s " "for the user that reviewboard is running as." ) % os.path.join(self.config_dir, "auth") ) else: raise SVNTool.normalize_error(e)
def get_urls(self, request): location = self.get_absolute_url() if _upload_url is None: put_url = request.build_absolute_uri(location) else: put_url = '%s%s' % (_upload_url, location) if _ws_download is True: get_url = '%s%s/%s/%s' % (settings.MEDIA_URL, _upload_base.strip('/'), self.hash, quote(self.name.encode('utf-8'))) if not urlsplit(get_url).netloc: if _upload_url is None: get_url = request.build_absolute_uri(get_url) else: get_url = '%s%s' % (_upload_url, get_url) else: get_url = put_url if _force_https is True: put_url = put_url.replace('http://', 'https://') get_url = get_url.replace('http://', 'https://') return put_url, get_url
def test_map_image(self, mock_requests): if self.model is None: return # Abstract test should not run SuperUserFactory.create(username='******', password='******') self.client.login(username='******', password='******') obj = self.modelfactory.create(geom='POINT(0 0)') # Initially, map image does not exists image_path = obj.get_map_image_path() if os.path.exists(image_path): os.remove(image_path) self.assertFalse(os.path.exists(image_path)) # Mock Screenshot response mock_requests.get.return_value.status_code = 200 mock_requests.get.return_value.content = b'*' * 100 response = self.client.get(obj.map_image_url) self.assertEqual(response.status_code, 200) self.assertTrue(os.path.exists(image_path)) mapimage_url = '%s%s?context' % (self.live_server_url, obj.get_detail_url()) screenshot_url = 'http://0.0.0.0:8001/?url=%s' % quote(mapimage_url) url_called = mock_requests.get.call_args_list[0] self.assertTrue(url_called.startswith(screenshot_url))
def CalendarioP(request): calendU = request.user.calendarioUser if (calendU is None): return redirect('logout') else: if request.user.is_staff: calendar = Calendar.objects.get(name='root') else: calendar = Calendar.objects.get(id=calendU.id) period_class = Day try: date = coerce_date_dict(request.GET) except ValueError: raise Http404 if date: try: date = datetime.datetime(**date) except ValueError: raise Http404 else: date = timezone.now() event_list = GET_EVENTS_FUNC(request, calendar) local_timezone = timezone.get_current_timezone() period = period_class(event_list, date, tzinfo=local_timezone) print(calendU) return render( request, 'calendarioP.html', { 'date': date, 'period': period, 'calendar': calendar, 'weekday_names': weekday_names, 'here': quote(request.get_full_path()), })
def clean_url(self): data = self.cleaned_data['url'] scheme, netloc, path, params, query, fragment = urlparse(data) if "%" not in path: path = quote(path) cleaned = urlunparse((scheme, netloc, path, '', '', '')) validate_mdn_url(cleaned) return cleaned
def test_watch_login_required(client, root_doc, endpoint): """User must be logged-in to subscribe to a document.""" url = reverse(endpoint, args=[root_doc.slug]) response = client.post(url) assert response.status_code == 302 assert_no_cache_header(response) assert response['Location'].endswith( reverse('account_login') + '?next=' + quote(url))
def urlquote(url, safe='/'): """ A version of Python's urllib.quote() function that can operate on unicode strings. The url is first UTF-8 encoded before quoting. The returned string can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ return force_text(quote(force_str(url), force_str(safe)))
def test_append_slash_quoted(self): """ URLs which require quoting should be redirected to their slash version ok. """ request = self.rf.get(quote("/needsquoting#")) r = CommonMiddleware().process_request(request) self.assertEqual(r.status_code, 301) self.assertEqual(r.url, "/needsquoting%23/")
def test_append_slash_quoted(self): """ URLs which require quoting should be redirected to their slash version. """ request = self.rf.get(quote('/needsquoting#')) response = HttpResponseNotFound() r = CommonMiddleware().process_response(request, response) self.assertEqual(r.status_code, 301) self.assertEqual(r.url, '/needsquoting%23/')
def test_append_slash_quoted_custom_urlconf(self): """ URLs which require quoting should be redirected to their slash version ok. """ request = self.rf.get(quote("/customurlconf/needsquoting#")) request.urlconf = "middleware.extra_urls" r = CommonMiddleware().process_request(request) self.assertIsNotNone(r, "CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf") self.assertEqual(r.status_code, 301) self.assertEqual(r.url, "/customurlconf/needsquoting%23/")
def get_file(self, repository, path, revision, base_commit_id=None, *args, **kwargs): """Fetches a file from Unfuddle. This will perform an API request to fetch the contents of a file. If using Git, this will expect a base commit ID to be provided. """ try: commit_id = self._get_commit_id(repository, path, revision, base_commit_id) url = self._build_api_url( self._get_repository_account_domain(repository), "repositories/%s/download/?path=%s&commit=%s" % (self._get_repository_id(repository), quote(path), quote(commit_id)), ) return self._api_get(url, raw_content=True) except (HTTPError, URLError): raise FileNotFoundError(path, revision)
def test_append_slash_quoted(self): """ Tests that URLs which require quoting are redirected to their slash version ok. """ request = self.rf.get(quote('/needsquoting#')) r = CommonMiddleware().process_request(request) self.assertEqual(r.status_code, 301) self.assertEqual( r.url, 'http://testserver/needsquoting%23/')
def test_append_slash_quoted_custom_urlconf(self): """ URLs which require quoting should be redirected to their slash version. """ request = self.rf.get(quote('/customurlconf/needsquoting#')) request.urlconf = 'middleware.extra_urls' response = HttpResponseNotFound() r = CommonMiddleware().process_response(request, response) self.assertIsNotNone(r, "CommonMiddleware failed to return APPEND_SLASH redirect using request.urlconf") self.assertEqual(r.status_code, 301) self.assertEqual(r.url, '/customurlconf/needsquoting%23/')
def get_file_exists(self, repository, path, revision, base_commit_id=None, *args, **kwargs): """Determines if a file exists. This will perform an API request to fetch the metadata for a file. If using Git, this will expect a base commit ID to be provided. """ try: commit_id = self._get_commit_id(repository, path, revision, base_commit_id) url = self._build_api_url( self._get_repository_account_domain(repository), "repositories/%s/history/?path=%s&commit=%s&count=0" % (self._get_repository_id(repository), quote(path), quote(commit_id)), ) self._api_get(url) return True except (HTTPError, URLError, FileNotFoundError): return False
def repercent_broken_unicode(path): """ As per section 3.2 of RFC 3987, step three of converting a URI into an IRI, we need to re-percent-encode any octet produced that is not part of a strictly legal UTF-8 octet sequence. """ try: path.decode('utf-8') except UnicodeDecodeError as e: repercent = quote(path[e.start:e.end], safe=b"/#%[]=:;$&()+,!?*@'~") path = repercent_broken_unicode( path[:e.start] + force_bytes(repercent) + path[e.end:]) return path
def smart_urlquote(url): """Quotes a URL if it isn't already quoted. Returns None on errors.""" # Handle IDN before quoting. try: scheme, netloc, path, query, fragment = urlsplit(url) except ValueError: # invalid IPv6 URL (normally square brackets in hostname part). return None try: netloc = netloc.encode('idna').decode('ascii') # IDN -> ACE except UnicodeError: # invalid domain part return None if not url_path_re.search(path): path = quote(force_str(path), safe=SAFE_PATH_CHARS) if not url_query_fragment_re.search(query): query = quote(force_str(query), safe=SAFE_QUERY_FRAGMENT_CHARS) if not url_query_fragment_re.search(fragment): fragment = quote(force_str(fragment), safe=SAFE_QUERY_FRAGMENT_CHARS) url = urlunsplit((scheme, netloc, path, query, fragment)) return force_text(url)
def test_append_slash_quoted_custom_urlconf(self): """ Tests that URLs which require quoting are redirected to their slash version ok. """ request = self.rf.get(quote('/customurlconf/needsquoting#')) request.urlconf = 'middleware.extra_urls' r = CommonMiddleware().process_request(request) self.assertIsNotNone(r, "CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf") self.assertEqual(r.status_code, 301) self.assertEqual( r.url, 'http://testserver/customurlconf/needsquoting%23/')
def escape_uri_path(path): """ Escape the unsafe characters from the path portion of a Uniform Resource Identifier (URI). """ # These are the "reserved" and "unreserved" characters specified in # sections 2.2 and 2.3 of RFC 2396: # reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," # unreserved = alphanum | mark # mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" # The list of safe characters here is constructed subtracting ";", "=", # and "?" according to section 3.3 of RFC 2396. # The reason for not subtracting and escaping "/" is that we are escaping # the entire path, not a path segment. return quote(force_bytes(path), safe=b"/:@&+$,-_.!~*'()")