django.utils.six.moves.urllib.parse.parse_qsl

Here are the examples of the python api django.utils.six.moves.urllib.parse.parse_qsl taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

29 Examples 7

Example 1

Project: django-getpaid
Source File: utils.py
View license
def qs_to_ordered_params(query_string):
    params_list = parse_qsl(unicode(query_string))
    params = OrderedDict()
    for field, value in params_list:
        if isinstance(value, (list, tuple)):
            value = value[0]
        if isinstance(value, six.binary_type):
            value = value.decode('utf8')
        if isinstance(field, six.binary_type):
            field = field.decode('utf8')
        params[field] = value
    return params

Example 2

Project: django-oscar-paypal
Source File: base.py
View license
    @property
    def context(self):
        ctx = {}
        if six.PY2 and isinstance(self.raw_response, six.text_type):
            self.raw_response = self.raw_response.encode('utf8')
        for key, val in parse_qsl(self.raw_response):
            if isinstance(key, six.binary_type):
                key = key.decode('utf8')
            if isinstance(val, six.binary_type):
                val = val.decode('utf8')
            ctx[key] = [val]
        return ctx

Example 3

Project: PyClassLessons
Source File: admin_urls.py
View license
@register.simple_tag(takes_context=True)
def add_preserved_filters(context, url, popup=False, to_field=None):
    opts = context.get('opts')
    preserved_filters = context.get('preserved_filters')

    parsed_url = list(urlparse(url))
    parsed_qs = dict(parse_qsl(parsed_url[4]))
    merged_qs = dict()

    if opts and preserved_filters:
        preserved_filters = dict(parse_qsl(preserved_filters))

        match_url = '/%s' % url.partition(get_script_prefix())[2]
        try:
            match = resolve(match_url)
        except Resolver404:
            pass
        else:
            current_url = '%s:%s' % (match.app_name, match.url_name)
            changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name)
            if changelist_url == current_url and '_changelist_filters' in preserved_filters:
                preserved_filters = dict(parse_qsl(preserved_filters['_changelist_filters']))

        merged_qs.update(preserved_filters)

    if popup:
        from django.contrib.admin.options import IS_POPUP_VAR
        merged_qs[IS_POPUP_VAR] = 1
    if to_field:
        from django.contrib.admin.options import TO_FIELD_VAR
        merged_qs[TO_FIELD_VAR] = to_field

    merged_qs.update(parsed_qs)

    parsed_url[4] = urlencode(merged_qs)
    return urlunparse(parsed_url)

Example 4

Project: PyClassLessons
Source File: request.py
View license
    def __init__(self, query_string, mutable=False, encoding=None):
        super(QueryDict, self).__init__()
        if not encoding:
            encoding = settings.DEFAULT_CHARSET
        self.encoding = encoding
        if six.PY3:
            if isinstance(query_string, bytes):
                # query_string normally contains URL-encoded data, a subset of ASCII.
                try:
                    query_string = query_string.decode(encoding)
                except UnicodeDecodeError:
                    # ... but some user agents are misbehaving :-(
                    query_string = query_string.decode('iso-8859-1')
            for key, value in parse_qsl(query_string or '',
                                        keep_blank_values=True,
                                        encoding=encoding):
                self.appendlist(key, value)
        else:
            for key, value in parse_qsl(query_string or '',
                                        keep_blank_values=True):
                try:
                    value = value.decode(encoding)
                except UnicodeDecodeError:
                    value = value.decode('iso-8859-1')
                self.appendlist(force_text(key, encoding, errors='replace'),
                                value)
        self._mutable = mutable

Example 5

View license
@register.simple_tag(takes_context=True)
def add_preserved_filters(context, url, popup=False, to_field=None):
    opts = context.get('opts')
    preserved_filters = context.get('preserved_filters')

    parsed_url = list(urlparse(url))
    parsed_qs = dict(parse_qsl(parsed_url[4]))
    merged_qs = dict()

    if opts and preserved_filters:
        preserved_filters = dict(parse_qsl(preserved_filters))

        match_url = '/%s' % url.partition(get_script_prefix())[2]
        try:
            match = resolve(match_url)
        except Resolver404:
            pass
        else:
            current_url = '%s:%s' % (match.app_name, match.url_name)
            changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name)
            if changelist_url == current_url and '_changelist_filters' in preserved_filters:
                preserved_filters = dict(parse_qsl(preserved_filters['_changelist_filters']))

        merged_qs.update(preserved_filters)

    if popup:
        from django.contrib.admin.options import IS_POPUP_VAR
        merged_qs[IS_POPUP_VAR] = 1
    if to_field:
        from django.contrib.admin.options import TO_FIELD_VAR
        merged_qs[TO_FIELD_VAR] = to_field

    merged_qs.update(parsed_qs)

    parsed_url[4] = urlencode(merged_qs)
    return urlunparse(parsed_url)

Example 6

Project: Django--an-app-at-a-time
Source File: request.py
View license
    def __init__(self, query_string=None, mutable=False, encoding=None):
        super(QueryDict, self).__init__()
        if not encoding:
            encoding = settings.DEFAULT_CHARSET
        self.encoding = encoding
        if six.PY3:
            if isinstance(query_string, bytes):
                # query_string normally contains URL-encoded data, a subset of ASCII.
                try:
                    query_string = query_string.decode(encoding)
                except UnicodeDecodeError:
                    # ... but some user agents are misbehaving :-(
                    query_string = query_string.decode('iso-8859-1')
            for key, value in parse_qsl(query_string or '',
                                        keep_blank_values=True,
                                        encoding=encoding):
                self.appendlist(key, value)
        else:
            for key, value in parse_qsl(query_string or '',
                                        keep_blank_values=True):
                try:
                    value = value.decode(encoding)
                except UnicodeDecodeError:
                    value = value.decode('iso-8859-1')
                self.appendlist(force_text(key, encoding, errors='replace'),
                                value)
        self._mutable = mutable

Example 7

Project: Django--an-app-at-a-time
Source File: html.py
View license
def smart_urlquote(url):
    "Quotes a URL if it isn't already quoted."
    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)

    # Handle IDN before quoting.
    try:
        scheme, netloc, path, query, fragment = urlsplit(url)
    except ValueError:
        # invalid IPv6 URL (normally square brackets in hostname part).
        return unquote_quote(url)

    try:
        netloc = netloc.encode('idna').decode('ascii')  # IDN -> ACE
    except UnicodeError:  # invalid domain part
        return unquote_quote(url)

    if query:
        # Separately unquoting key/value, so as to not mix querystring separators
        # included in query values. See #22267.
        query_parts = [(unquote(force_str(q[0])), unquote(force_str(q[1])))
                       for q in parse_qsl(query, keep_blank_values=True)]
        # urlencode will take care of quoting
        query = urlencode(query_parts)

    path = unquote_quote(path)
    fragment = unquote_quote(fragment)

    return urlunsplit((scheme, netloc, path, query, fragment))

Example 8

View license
    def assertEqualUrl(self, lhs, rhs):
        """
        Asserts whether the URLs are canonically equal.
        """
        if six.PY2:
            # See those Chinese characters above? Those are quite difficult
            # to match against the generated URLs in portable code. True,
            # this solution is not the nicest, but it works. And it's test
            # code after all.
            lhs = lhs.encode('utf8')

        lhs = urlparse(lhs)
        rhs = urlparse(rhs)
        self.assertEqual(lhs.scheme, rhs.scheme)
        self.assertEqual(lhs.netloc, rhs.netloc)
        self.assertEqual(lhs.path, rhs.path)
        self.assertEqual(lhs.fragment, rhs.fragment)

        # We used parse_qs before, but as query parameter order became
        # significant with Microsoft Authenticator and possibly other
        # authenticator apps, we've switched to parse_qsl.
        self.assertEqual(parse_qsl(lhs.query), parse_qsl(rhs.query))

Example 9

Project: cgstudiomap
Source File: admin_urls.py
View license
@register.simple_tag(takes_context=True)
def add_preserved_filters(context, url, popup=False, to_field=None):
    opts = context.get('opts')
    preserved_filters = context.get('preserved_filters')

    parsed_url = list(urlparse(url))
    parsed_qs = dict(parse_qsl(parsed_url[4]))
    merged_qs = dict()

    if opts and preserved_filters:
        preserved_filters = dict(parse_qsl(preserved_filters))

        match_url = '/%s' % url.partition(get_script_prefix())[2]
        try:
            match = resolve(match_url)
        except Resolver404:
            pass
        else:
            current_url = '%s:%s' % (match.app_name, match.url_name)
            changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name)
            if changelist_url == current_url and '_changelist_filters' in preserved_filters:
                preserved_filters = dict(parse_qsl(preserved_filters['_changelist_filters']))

        merged_qs.update(preserved_filters)

    if popup:
        from django.contrib.admin.options import IS_POPUP_VAR
        merged_qs[IS_POPUP_VAR] = 1
    if to_field:
        from django.contrib.admin.options import TO_FIELD_VAR
        merged_qs[TO_FIELD_VAR] = to_field

    merged_qs.update(parsed_qs)

    parsed_url[4] = urlencode(merged_qs)
    return urlunparse(parsed_url)

Example 10

Project: cgstudiomap
Source File: request.py
View license
    def __init__(self, query_string=None, mutable=False, encoding=None):
        super(QueryDict, self).__init__()
        if not encoding:
            encoding = settings.DEFAULT_CHARSET
        self.encoding = encoding
        if six.PY3:
            if isinstance(query_string, bytes):
                # query_string normally contains URL-encoded data, a subset of ASCII.
                try:
                    query_string = query_string.decode(encoding)
                except UnicodeDecodeError:
                    # ... but some user agents are misbehaving :-(
                    query_string = query_string.decode('iso-8859-1')
            for key, value in parse_qsl(query_string or '',
                                        keep_blank_values=True,
                                        encoding=encoding):
                self.appendlist(key, value)
        else:
            for key, value in parse_qsl(query_string or '',
                                        keep_blank_values=True):
                try:
                    value = value.decode(encoding)
                except UnicodeDecodeError:
                    value = value.decode('iso-8859-1')
                self.appendlist(force_text(key, encoding, errors='replace'),
                                value)
        self._mutable = mutable

Example 11

Project: cgstudiomap
Source File: html.py
View license
def smart_urlquote(url):
    "Quotes a URL if it isn't already quoted."
    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)

    # Handle IDN before quoting.
    try:
        scheme, netloc, path, query, fragment = urlsplit(url)
    except ValueError:
        # invalid IPv6 URL (normally square brackets in hostname part).
        return unquote_quote(url)

    try:
        netloc = netloc.encode('idna').decode('ascii')  # IDN -> ACE
    except UnicodeError:  # invalid domain part
        return unquote_quote(url)

    if query:
        # Separately unquoting key/value, so as to not mix querystring separators
        # included in query values. See #22267.
        query_parts = [(unquote(force_str(q[0])), unquote(force_str(q[1])))
                       for q in parse_qsl(query, keep_blank_values=True)]
        # urlencode will take care of quoting
        query = urlencode(query_parts)

    path = unquote_quote(path)
    fragment = unquote_quote(fragment)

    return urlunsplit((scheme, netloc, path, query, fragment))

Example 12

Project: hue
Source File: admin_urls.py
View license
@register.simple_tag(takes_context=True)
def add_preserved_filters(context, url, popup=False):
    opts = context.get('opts')
    preserved_filters = context.get('preserved_filters')

    parsed_url = list(urlparse(url))
    parsed_qs = dict(parse_qsl(parsed_url[4]))
    merged_qs = dict()

    if opts and preserved_filters:
        preserved_filters = dict(parse_qsl(preserved_filters))

        match_url = '/%s' % url.partition(get_script_prefix())[2]
        try:
            match = resolve(match_url)
        except Resolver404:
            pass
        else:
            current_url = '%s:%s' % (match.app_name, match.url_name)
            changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name)
            if changelist_url == current_url and '_changelist_filters' in preserved_filters:
                preserved_filters = dict(parse_qsl(preserved_filters['_changelist_filters']))

        merged_qs.update(preserved_filters)

    if popup:
        from django.contrib.admin.options import IS_POPUP_VAR
        merged_qs[IS_POPUP_VAR] = 1

    merged_qs.update(parsed_qs)

    parsed_url[4] = urlencode(merged_qs)
    return urlunparse(parsed_url)

Example 13

Project: hue
Source File: __init__.py
View license
def parse_backend_uri(backend_uri):
    """
    Converts the "backend_uri" into a cache scheme ('db', 'memcached', etc), a
    host and any extra params that are required for the backend. Returns a
    (scheme, host, params) tuple.
    """
    if backend_uri.find(':') == -1:
        raise InvalidCacheBackendError("Backend URI must start with scheme://")
    scheme, rest = backend_uri.split(':', 1)
    if not rest.startswith('//'):
        raise InvalidCacheBackendError("Backend URI must start with scheme://")

    host = rest[2:]
    qpos = rest.find('?')
    if qpos != -1:
        params = dict(parse_qsl(rest[qpos+1:]))
        host = rest[2:qpos]
    else:
        params = {}
    if host.endswith('/'):
        host = host[:-1]

    return scheme, host, params

Example 14

Project: hue
Source File: request.py
View license
    def __init__(self, query_string, mutable=False, encoding=None):
        super(QueryDict, self).__init__()
        if not encoding:
            encoding = settings.DEFAULT_CHARSET
        self.encoding = encoding
        if six.PY3:
            if isinstance(query_string, bytes):
                # query_string normally contains URL-encoded data, a subset of ASCII.
                try:
                    query_string = query_string.decode(encoding)
                except UnicodeDecodeError:
                    # ... but some user agents are misbehaving :-(
                    query_string = query_string.decode('iso-8859-1')
            for key, value in parse_qsl(query_string or '',
                                        keep_blank_values=True,
                                        encoding=encoding):
                self.appendlist(key, value)
        else:
            for key, value in parse_qsl(query_string or '',
                                        keep_blank_values=True):
                self.appendlist(force_text(key, encoding, errors='replace'),
                                force_text(value, encoding, errors='replace'))
        self._mutable = mutable

Example 15

Project: django-getpaid
Source File: test_epaydk.py
View license
    def test_get_gateway_url(self):
        payproc = getpaid.backends.epaydk.PaymentProcessor(self.test_payment)
        fake_req = mock.MagicMock(spec=HttpRequest)
        fake_req.scheme = 'https'
        fake_req.COOKIES = {}
        fake_req.META = {}
        actual = payproc.get_gateway_url(fake_req)
        self.assertEqual(actual[1], "GET")
        self.assertEqual(actual[2], {})

        actual = list(urlparse(actual[0]))
        self.assertEqual(actual[0], 'https')
        self.assertEqual(actual[1], 'ssl.ditonlinebetalingssystem.dk')
        self.assertEqual(actual[2], '/integration/ewindow/Default.aspx')
        self.assertEqual(actual[3], '')

        domain = getpaid.utils.get_domain()
        accepturl = u'https://'+ domain +'/getpaid.backends.epaydk/success/'
        callbackurl = u'https://'+ domain +'/getpaid.backends.epaydk/online/'
        cancelurl = u'https://'+ domain +'/getpaid.backends.epaydk/failure/'
        expected = [
            (u'merchantnumber', u'xxxxxxxx'),
            (u'orderid', u'1'),
            (u'currency', u'208'),
            (u'amount', u'12345'),
            (u'windowstate', u'3'),
            (u'mobile', u'1'),
            (u'timeout', u'3'),
            (u'instantcallback', u'0'),
            (u'language', u'2'),
            (u'accepturl', accepturl),
            (u'callbackurl', callbackurl),
            (u'cancelurl', cancelurl),
        ]
        md5hash = payproc.compute_hash(OrderedDict(expected))
        expected.append(('hash', md5hash))
        self.assertListEqual(expected, parse_qsl(actual[4]))
        self.assertEqual(actual[5], '')

Example 16

Project: django
Source File: admin_urls.py
View license
@register.simple_tag(takes_context=True)
def add_preserved_filters(context, url, popup=False, to_field=None):
    opts = context.get('opts')
    preserved_filters = context.get('preserved_filters')

    parsed_url = list(urlparse(url))
    parsed_qs = dict(parse_qsl(parsed_url[4]))
    merged_qs = dict()

    if opts and preserved_filters:
        preserved_filters = dict(parse_qsl(preserved_filters))

        match_url = '/%s' % url.partition(get_script_prefix())[2]
        try:
            match = resolve(match_url)
        except Resolver404:
            pass
        else:
            current_url = '%s:%s' % (match.app_name, match.url_name)
            changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name)
            if changelist_url == current_url and '_changelist_filters' in preserved_filters:
                preserved_filters = dict(parse_qsl(preserved_filters['_changelist_filters']))

        merged_qs.update(preserved_filters)

    if popup:
        from django.contrib.admin.options import IS_POPUP_VAR
        merged_qs[IS_POPUP_VAR] = 1
    if to_field:
        from django.contrib.admin.options import TO_FIELD_VAR
        merged_qs[TO_FIELD_VAR] = to_field

    merged_qs.update(parsed_qs)

    parsed_url[4] = urlencode(merged_qs)
    return urlunparse(parsed_url)

Example 17

Project: django
Source File: html.py
View license
def smart_urlquote(url):
    "Quotes a URL if it isn't already quoted."
    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)

    # Handle IDN before quoting.
    try:
        scheme, netloc, path, query, fragment = urlsplit(url)
    except ValueError:
        # invalid IPv6 URL (normally square brackets in hostname part).
        return unquote_quote(url)

    try:
        netloc = netloc.encode('idna').decode('ascii')  # IDN -> ACE
    except UnicodeError:  # invalid domain part
        return unquote_quote(url)

    if query:
        # Separately unquoting key/value, so as to not mix querystring separators
        # included in query values. See #22267.
        query_parts = [(unquote(force_str(q[0])), unquote(force_str(q[1])))
                       for q in parse_qsl(query, keep_blank_values=True)]
        # urlencode will take care of quoting
        query = urlencode(query_parts)

    path = unquote_quote(path)
    fragment = unquote_quote(fragment)

    return urlunsplit((scheme, netloc, path, query, fragment))

Example 18

View license
def parse_backend_uri(backend_uri):
    """
    Converts the "backend_uri" into a host and any extra params that are
    required for the backend. Returns a (host, params) tuple.
    """
    backend_uri_sliced = backend_uri.split('://')
    if len(backend_uri_sliced) > 2:
        raise InvalidCacheBackendError(
            "Backend URI can't have more than one scheme://")
    elif len(backend_uri_sliced) == 2:
        rest = backend_uri_sliced[1]
    else:
        rest = backend_uri_sliced[0]

    host = rest
    qpos = rest.find('?')
    if qpos != -1:
        params = dict(parse_qsl(rest[qpos + 1:]))
        host = rest[:qpos]
    else:
        params = {}
    if host.endswith('/'):
        host = host[:-1]

    return host, params

Example 19

Project: django-oscar-paypal
Source File: gateway.py
View license
def post(url, params):
    """
    Make a POST request to the URL using the key-value pairs.  Return
    a set of key-value pairs.

    :url: URL to post to
    :params: Dict of parameters to include in post payload
    """
    payload = urlencode(params)
    start_time = time.time()
    response = requests.post(
        url, payload,
        headers={'content-type': 'text/namevalue; charset=utf-8'})
    if response.status_code != requests.codes.ok:
        raise exceptions.PayPalError("Unable to communicate with PayPal")

    # Convert response into a simple key-value format
    pairs = {}
    for key, value in parse_qsl(response.content):
        if isinstance(key, six.binary_type):
            key = key.decode('utf8')
        if isinstance(value, six.binary_type):
            value = value.decode('utf8')
        pairs[key] = value

    # Add audit information
    pairs['_raw_request'] = payload
    pairs['_raw_response'] = response.content
    pairs['_response_time'] = (time.time() - start_time) * 1000.0

    return pairs

Example 20

Project: peer
Source File: entity_urls.py
View license
@register.simple_tag(takes_context=True)
def add_preserved_filters(context, url, popup=False, to_field=None):
    opts = context.get('opts')
    preserved_filters = context.get('preserved_filters')

    parsed_url = list(urlparse(url))
    parsed_qs = dict(parse_qsl(parsed_url[4]))
    merged_qs = dict()

    if opts and preserved_filters:
        preserved_filters = dict(parse_qsl(preserved_filters))

        match_url = '/%s' % url.partition(get_script_prefix())[2]
        try:
            match = resolve(match_url)
        except Resolver404:
            pass
        else:
            current_url = '%s:%s' % (match.app_name, match.url_name)
            changelist_url = 'entities:%s_%s_changelist' % (opts.app_label, opts.model_name)
            if changelist_url == current_url and '_changelist_filters' in preserved_filters:
                preserved_filters = dict(parse_qsl(preserved_filters['_changelist_filters']))

        merged_qs.update(preserved_filters)

    if popup:
        from django.contrib.admin.options import IS_POPUP_VAR
        merged_qs[IS_POPUP_VAR] = 1
    if to_field:
        from django.contrib.admin.options import TO_FIELD_VAR
        merged_qs[TO_FIELD_VAR] = to_field

    merged_qs.update(parsed_qs)

    parsed_url[4] = urlencode(merged_qs)
    return urlunparse(parsed_url)

Example 21

Project: HealthStarter
Source File: admin_urls.py
View license
@register.simple_tag(takes_context=True)
def add_preserved_filters(context, url, popup=False, to_field=None):
    opts = context.get('opts')
    preserved_filters = context.get('preserved_filters')

    parsed_url = list(urlparse(url))
    parsed_qs = dict(parse_qsl(parsed_url[4]))
    merged_qs = dict()

    if opts and preserved_filters:
        preserved_filters = dict(parse_qsl(preserved_filters))

        match_url = '/%s' % url.partition(get_script_prefix())[2]
        try:
            match = resolve(match_url)
        except Resolver404:
            pass
        else:
            current_url = '%s:%s' % (match.app_name, match.url_name)
            changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name)
            if changelist_url == current_url and '_changelist_filters' in preserved_filters:
                preserved_filters = dict(parse_qsl(preserved_filters['_changelist_filters']))

        merged_qs.update(preserved_filters)

    if popup:
        from django.contrib.admin.options import IS_POPUP_VAR
        merged_qs[IS_POPUP_VAR] = 1
    if to_field:
        from django.contrib.admin.options import TO_FIELD_VAR
        merged_qs[TO_FIELD_VAR] = to_field

    merged_qs.update(parsed_qs)

    parsed_url[4] = urlencode(merged_qs)
    return urlunparse(parsed_url)

Example 22

Project: HealthStarter
Source File: request.py
View license
    def __init__(self, query_string=None, mutable=False, encoding=None):
        super(QueryDict, self).__init__()
        if not encoding:
            encoding = settings.DEFAULT_CHARSET
        self.encoding = encoding
        if six.PY3:
            if isinstance(query_string, bytes):
                # query_string normally contains URL-encoded data, a subset of ASCII.
                try:
                    query_string = query_string.decode(encoding)
                except UnicodeDecodeError:
                    # ... but some user agents are misbehaving :-(
                    query_string = query_string.decode('iso-8859-1')
            for key, value in parse_qsl(query_string or '',
                                        keep_blank_values=True,
                                        encoding=encoding):
                self.appendlist(key, value)
        else:
            for key, value in parse_qsl(query_string or '',
                                        keep_blank_values=True):
                try:
                    value = value.decode(encoding)
                except UnicodeDecodeError:
                    value = value.decode('iso-8859-1')
                self.appendlist(force_text(key, encoding, errors='replace'),
                                value)
        self._mutable = mutable

Example 23

Project: HealthStarter
Source File: html.py
View license
def smart_urlquote(url):
    "Quotes a URL if it isn't already quoted."
    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)

    # Handle IDN before quoting.
    try:
        scheme, netloc, path, query, fragment = urlsplit(url)
    except ValueError:
        # invalid IPv6 URL (normally square brackets in hostname part).
        return unquote_quote(url)

    try:
        netloc = netloc.encode('idna').decode('ascii')  # IDN -> ACE
    except UnicodeError:  # invalid domain part
        return unquote_quote(url)

    if query:
        # Separately unquoting key/value, so as to not mix querystring separators
        # included in query values. See #22267.
        query_parts = [(unquote(force_str(q[0])), unquote(force_str(q[1])))
                       for q in parse_qsl(query, keep_blank_values=True)]
        # urlencode will take care of quoting
        query = urlencode(query_parts)

    path = unquote_quote(path)
    fragment = unquote_quote(fragment)

    return urlunsplit((scheme, netloc, path, query, fragment))

Example 24

Project: kitsune
Source File: redis_utils.py
View license
def parse_backend_uri(backend_uri):
    """
    Converts the "backend_uri" into a cache scheme ('db', 'memcached', etc), a
    host and any extra params that are required for the backend. Returns a
    (scheme, host, params) tuple.
    """
    if backend_uri.find(':') == -1:
        raise InvalidCacheBackendError("Backend URI must start with scheme://")
    scheme, rest = backend_uri.split(':', 1)
    if not rest.startswith('//'):
        raise InvalidCacheBackendError("Backend URI must start with scheme://")

    host = rest[2:]
    qpos = rest.find('?')
    if qpos != -1:
        params = dict(parse_qsl(rest[qpos+1:]))
        host = rest[2:qpos]
    else:
        params = {}
    if host.endswith('/'):
        host = host[:-1]

    return scheme, host, params

Example 25

Project: kitsune
Source File: redis_utils.py
View license
def parse_backend_uri(backend_uri):
    """
    Converts the "backend_uri" into a cache scheme ('db', 'memcached', etc), a
    host and any extra params that are required for the backend. Returns a
    (scheme, host, params) tuple.
    """
    if backend_uri.find(':') == -1:
        raise InvalidCacheBackendError("Backend URI must start with scheme://")
    scheme, rest = backend_uri.split(':', 1)
    if not rest.startswith('//'):
        raise InvalidCacheBackendError("Backend URI must start with scheme://")

    host = rest[2:]
    qpos = rest.find('?')
    if qpos != -1:
        params = dict(parse_qsl(rest[qpos+1:]))
        host = rest[2:qpos]
    else:
        params = {}
    if host.endswith('/'):
        host = host[:-1]

    return scheme, host, params

Example 26

Project: kuma
Source File: test_admin.py
View license
    @requests_mock.mock()
    def test_spam_submission_submitted(self, mock_requests):
        admin = User.objects.get(username='admin')
        flag, created = Flag.objects.get_or_create(name=SPAM_SUBMISSIONS_FLAG)
        flag.users.add(admin)
        revision = admin.created_revisions.all()[0]
        url = reverse('admin:wiki_revisionakismetsubmission_add')

        mock_requests.post(VERIFY_URL, content='valid')
        mock_requests.post(SPAM_URL, content=Akismet.submission_success)

        data = {
            'revision': revision.id,
            'type': 'spam',
        }
        self.client.login(username='admin', password='testpass')
        url = reverse('admin:wiki_revisionakismetsubmission_add')
        response = self.client.post(url, data)
        self.assertEqual(response.status_code, 302)

        # successfully created the submission record
        submission = RevisionAkismetSubmission.objects.first()
        self.assertTrue(submission is not None)
        self.assertEqual(submission.sender, admin)
        self.assertTrue(submission.sent)
        self.assertEqual(submission.revision, revision)
        self.assertEqual(submission.type, 'spam')

        self.assertEqual(mock_requests.call_count, 2)
        request_body = mock_requests.request_history[1].body
        self.assertIn('user_ip=0.0.0.0', request_body)
        self.assertIn('user_agent=', request_body)
        self.assertIn(revision.slug, request_body)
        query_pairs = parse_qsl(request_body)
        expected_content = (
            'Seventh revision of the article.\n'
            'article-with-revisions\n'
            'Seventh revision of the article.\n'
            'Seventh revision of the article.'
        )
        expected = [
            ('blog', 'http://testserver/'),
            ('blog_charset', 'UTF-8'),
            ('blog_lang', 'en_us'),
            ('comment_author', 'admin'),
            ('comment_content', expected_content),
            ('comment_type', 'wiki-revision'),
            ('permalink',
             'http://testserver/en-US/docs/article-with-revisions'),
            ('user_ip', '0.0.0.0')
        ]
        self.assertEqual(sorted(query_pairs), expected)

        assert mock_requests.called
        assert mock_requests.call_count == 2

Example 27

Project: kuma
Source File: test_admin.py
View license
    @requests_mock.mock()
    def test_spam_submission_tags(self, mock_requests):
        admin = User.objects.get(username='admin')
        flag, created = Flag.objects.get_or_create(name=SPAM_SUBMISSIONS_FLAG)
        flag.users.add(admin)
        revision = admin.created_revisions.all()[0]
        revision.tags = '"Banana" "Orange" "Apple"'
        revision.save()
        url = reverse('admin:wiki_revisionakismetsubmission_add')

        mock_requests.post(VERIFY_URL, content='valid')
        mock_requests.post(SPAM_URL, content=Akismet.submission_success)

        data = {
            'revision': revision.id,
            'type': 'spam',
        }
        self.client.login(username='admin', password='testpass')
        url = reverse('admin:wiki_revisionakismetsubmission_add')
        response = self.client.post(url, data)
        self.assertEqual(response.status_code, 302)

        request_body = mock_requests.request_history[1].body
        submitted_data = dict(parse_qsl(request_body))
        expected_content = (
            'Seventh revision of the article.\n'
            'article-with-revisions\n'
            'Seventh revision of the article.\n'
            'Seventh revision of the article.\n'
            'Apple\n'
            'Banana\n'
            'Orange'
        )
        self.assertEqual(submitted_data['comment_content'], expected_content)

Example 28

Project: kuma
Source File: test_admin.py
View license
    @requests_mock.mock()
    def test_spam_submission_submitted(self, mock_requests):
        admin = User.objects.get(username='admin')
        flag, created = Flag.objects.get_or_create(name=SPAM_SUBMISSIONS_FLAG)
        flag.users.add(admin)
        revision = admin.created_revisions.all()[0]
        url = reverse('admin:wiki_revisionakismetsubmission_add')

        mock_requests.post(VERIFY_URL, content='valid')
        mock_requests.post(SPAM_URL, content=Akismet.submission_success)

        data = {
            'revision': revision.id,
            'type': 'spam',
        }
        self.client.login(username='admin', password='testpass')
        url = reverse('admin:wiki_revisionakismetsubmission_add')
        response = self.client.post(url, data)
        self.assertEqual(response.status_code, 302)

        # successfully created the submission record
        submission = RevisionAkismetSubmission.objects.first()
        self.assertTrue(submission is not None)
        self.assertEqual(submission.sender, admin)
        self.assertTrue(submission.sent)
        self.assertEqual(submission.revision, revision)
        self.assertEqual(submission.type, 'spam')

        self.assertEqual(mock_requests.call_count, 2)
        request_body = mock_requests.request_history[1].body
        self.assertIn('user_ip=0.0.0.0', request_body)
        self.assertIn('user_agent=', request_body)
        self.assertIn(revision.slug, request_body)
        query_pairs = parse_qsl(request_body)
        expected_content = (
            'Seventh revision of the article.\n'
            'article-with-revisions\n'
            'Seventh revision of the article.\n'
            'Seventh revision of the article.'
        )
        expected = [
            ('blog', 'http://testserver/'),
            ('blog_charset', 'UTF-8'),
            ('blog_lang', 'en_us'),
            ('comment_author', 'admin'),
            ('comment_content', expected_content),
            ('comment_type', 'wiki-revision'),
            ('permalink',
             'http://testserver/en-US/docs/article-with-revisions'),
            ('user_ip', '0.0.0.0')
        ]
        self.assertEqual(sorted(query_pairs), expected)

        assert mock_requests.called
        assert mock_requests.call_count == 2

Example 29

Project: kuma
Source File: test_admin.py
View license
    @requests_mock.mock()
    def test_spam_submission_tags(self, mock_requests):
        admin = User.objects.get(username='admin')
        flag, created = Flag.objects.get_or_create(name=SPAM_SUBMISSIONS_FLAG)
        flag.users.add(admin)
        revision = admin.created_revisions.all()[0]
        revision.tags = '"Banana" "Orange" "Apple"'
        revision.save()
        url = reverse('admin:wiki_revisionakismetsubmission_add')

        mock_requests.post(VERIFY_URL, content='valid')
        mock_requests.post(SPAM_URL, content=Akismet.submission_success)

        data = {
            'revision': revision.id,
            'type': 'spam',
        }
        self.client.login(username='admin', password='testpass')
        url = reverse('admin:wiki_revisionakismetsubmission_add')
        response = self.client.post(url, data)
        self.assertEqual(response.status_code, 302)

        request_body = mock_requests.request_history[1].body
        submitted_data = dict(parse_qsl(request_body))
        expected_content = (
            'Seventh revision of the article.\n'
            'article-with-revisions\n'
            'Seventh revision of the article.\n'
            'Seventh revision of the article.\n'
            'Apple\n'
            'Banana\n'
            'Orange'
        )
        self.assertEqual(submitted_data['comment_content'], expected_content)