Пример #1
0
 def test_urlencode(self):
     q = QueryDict(str(''), mutable=True)
     q['next'] = '/a&b/'
     self.assertEqual(q.urlencode(), 'next=%2Fa%26b%2F')
     self.assertEqual(q.urlencode(safe='/'), 'next=/a%26b/')
     q = QueryDict(str(''), mutable=True)
     q['next'] = '/t\xebst&key/'
     self.assertEqual(q.urlencode(), 'next=%2Ft%C3%ABst%26key%2F')
     self.assertEqual(q.urlencode(safe='/'), 'next=/t%C3%ABst%26key/')
Пример #2
0
 def test_non_default_encoding(self):
     """#13572 - QueryDict with a non-default encoding"""
     q = QueryDict(str('cur=%A4'), encoding='iso-8859-15')
     self.assertEqual(q.encoding, 'iso-8859-15')
     self.assertEqual(list(six.iteritems(q)), [('cur', '€')])
     self.assertEqual(q.urlencode(), 'cur=%A4')
     q = q.copy()
     self.assertEqual(q.encoding, 'iso-8859-15')
     self.assertEqual(list(six.iteritems(q)), [('cur', '€')])
     self.assertEqual(q.urlencode(), 'cur=%A4')
     self.assertEqual(copy.copy(q).encoding, 'iso-8859-15')
     self.assertEqual(copy.deepcopy(q).encoding, 'iso-8859-15')
Пример #3
0
 def test_immutable_basic_operations(self):
     q = QueryDict(str(''))
     self.assertEqual(q.getlist('foo'), [])
     if not six.PY3:
         self.assertEqual(q.has_key('foo'), False)
     self.assertEqual('foo' in q, False)
     self.assertEqual(list(six.iteritems(q)), [])
     self.assertEqual(list(six.iterlists(q)), [])
     self.assertEqual(list(six.iterkeys(q)), [])
     self.assertEqual(list(six.itervalues(q)), [])
     self.assertEqual(len(q), 0)
     self.assertEqual(q.urlencode(), '')
Пример #4
0
def redirect_to_login(next, login_url=None,
                      redirect_field_name=REDIRECT_FIELD_NAME):
    """
    Redirects the user to the login page, passing the given 'next' page
    """
    # urlparse chokes on lazy objects in Python 3
    login_url_as_str = force_str(login_url or settings.LOGIN_URL)

    login_url_parts = list(urlparse(login_url_as_str))
    if redirect_field_name:
        querystring = QueryDict(login_url_parts[4], mutable=True)
        querystring[redirect_field_name] = next
        login_url_parts[4] = querystring.urlencode(safe='/')

    return HttpResponseRedirect(urlunparse(login_url_parts))
Пример #5
0
    def test_basic_mutable_operations(self):
        q = QueryDict(str('')).copy()
        q['name'] = 'john'
        self.assertEqual(q.get('foo', 'default'), 'default')
        self.assertEqual(q.get('name', 'default'), 'john')
        self.assertEqual(q.getlist('name'), ['john'])
        self.assertEqual(q.getlist('foo'), [])

        q.setlist('foo', ['bar', 'baz'])
        self.assertEqual(q.get('foo', 'default'), 'baz')
        self.assertEqual(q.getlist('foo'), ['bar', 'baz'])

        q.appendlist('foo', 'another')
        self.assertEqual(q.getlist('foo'), ['bar', 'baz', 'another'])
        self.assertEqual(q['foo'], 'another')
        if not six.PY3:
            self.assertTrue(q.has_key('foo'))
        self.assertTrue('foo' in q)

        self.assertEqual(list(six.iteritems(q)),  [('foo', 'another'), ('name', 'john')])
        self.assertEqual(list(six.iterlists(q)), [('foo', ['bar', 'baz', 'another']), ('name', ['john'])])
        self.assertEqual(list(six.iterkeys(q)), ['foo', 'name'])
        self.assertEqual(list(six.itervalues(q)), ['another', 'john'])
        self.assertEqual(len(q), 2)

        q.update({'foo': 'hello'})
        self.assertEqual(q['foo'], 'hello')
        self.assertEqual(q.get('foo', 'not available'), 'hello')
        self.assertEqual(q.getlist('foo'), ['bar', 'baz', 'another', 'hello'])
        self.assertEqual(q.pop('foo'), ['bar', 'baz', 'another', 'hello'])
        self.assertEqual(q.pop('foo', 'not there'), 'not there')
        self.assertEqual(q.get('foo', 'not there'), 'not there')
        self.assertEqual(q.setdefault('foo', 'bar'), 'bar')
        self.assertEqual(q['foo'], 'bar')
        self.assertEqual(q.getlist('foo'), ['bar'])
        self.assertEqual(q.urlencode(), 'foo=bar&name=john')

        q.clear()
        self.assertEqual(len(q), 0)
Пример #6
0
    def test_single_key_value(self):
        """Test QueryDict with one key/value pair"""

        q = QueryDict(str('foo=bar'))
        self.assertEqual(q['foo'], 'bar')
        self.assertRaises(KeyError, q.__getitem__, 'bar')
        self.assertRaises(AttributeError, q.__setitem__, 'something', 'bar')

        self.assertEqual(q.get('foo', 'default'), 'bar')
        self.assertEqual(q.get('bar', 'default'), 'default')
        self.assertEqual(q.getlist('foo'), ['bar'])
        self.assertEqual(q.getlist('bar'), [])

        self.assertRaises(AttributeError, q.setlist, 'foo', ['bar'])
        self.assertRaises(AttributeError, q.appendlist, 'foo', ['bar'])

        if not six.PY3:
            self.assertTrue(q.has_key('foo'))
        self.assertTrue('foo' in q)
        if not six.PY3:
            self.assertFalse(q.has_key('bar'))
        self.assertFalse('bar' in q)

        self.assertEqual(list(six.iteritems(q)), [('foo', 'bar')])
        self.assertEqual(list(six.iterlists(q)), [('foo', ['bar'])])
        self.assertEqual(list(six.iterkeys(q)), ['foo'])
        self.assertEqual(list(six.itervalues(q)), ['bar'])
        self.assertEqual(len(q), 1)

        self.assertRaises(AttributeError, q.update, {'foo': 'bar'})
        self.assertRaises(AttributeError, q.pop, 'foo')
        self.assertRaises(AttributeError, q.popitem)
        self.assertRaises(AttributeError, q.clear)
        self.assertRaises(AttributeError, q.setdefault, 'foo', 'bar')

        self.assertEqual(q.urlencode(), 'foo=bar')
Пример #7
0
 def test_remote_login_url_with_next_querystring(self):
     login_url = "http://remote.example.com/login/"
     login_required_url = self.get_login_required_url("%s?next=/default/" % login_url)
     querystring = QueryDict("", mutable=True)
     querystring["next"] = "http://testserver/login_required/"
     self.assertEqual(login_required_url, "%s?%s" % (login_url, querystring.urlencode("/")))
Пример #8
0
 def test_login_url_with_querystring(self):
     login_url = "/login/?pretty=1"
     login_required_url = self.get_login_required_url(login_url)
     querystring = QueryDict("pretty=1", mutable=True)
     querystring["next"] = "/login_required/"
     self.assertEqual(login_required_url, "http://testserver/login/?%s" % querystring.urlencode("/"))
Пример #9
0
 def test_https_login_url(self):
     login_url = "https:///login/"
     login_required_url = self.get_login_required_url(login_url)
     querystring = QueryDict("", mutable=True)
     querystring["next"] = "http://testserver/login_required/"
     self.assertEqual(login_required_url, "%s?%s" % (login_url, querystring.urlencode("/")))