Merge branch 'master' into test_is_url_py3
This commit is contained in:
commit
7c9653ae23
4 files changed
+59
-5
No files matched your search
+2
-2
@@ -349,14 +349,14 @@ class Request(Storage):
|
|||||||
current.session.forget()
|
current.session.forget()
|
||||||
redirect(URL(scheme='https', args=self.args, vars=self.vars))
|
redirect(URL(scheme='https', args=self.args, vars=self.vars))
|
||||||
|
|
||||||
def restful(self):
|
def restful(self, ignore_extension=False):
|
||||||
def wrapper(action, request=self):
|
def wrapper(action, request=self):
|
||||||
def f(_action=action, *a, **b):
|
def f(_action=action, *a, **b):
|
||||||
request.is_restful = True
|
request.is_restful = True
|
||||||
env = request.env
|
env = request.env
|
||||||
is_json = env.content_type=='application/json'
|
is_json = env.content_type=='application/json'
|
||||||
method = env.request_method
|
method = env.request_method
|
||||||
if len(request.args) and '.' in request.args[-1]:
|
if not ignore_extension and len(request.args) and '.' in request.args[-1]:
|
||||||
request.args[-1], _, request.extension = request.args[-1].rpartition('.')
|
request.args[-1], _, request.extension = request.args[-1].rpartition('.')
|
||||||
current.response.headers['Content-Type'] = \
|
current.response.headers['Content-Type'] = \
|
||||||
contenttype('.' + request.extension.lower())
|
contenttype('.' + request.extension.lower())
|
||||||
|
|||||||
@@ -10,9 +10,11 @@ import re
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from gluon.globals import Request, Response, Session
|
from gluon.globals import Request, Response, Session
|
||||||
|
from gluon.rewrite import regex_url_in
|
||||||
from gluon import URL
|
from gluon import URL
|
||||||
from gluon._compat import basestring
|
from gluon._compat import basestring
|
||||||
|
|
||||||
|
|
||||||
def setup_clean_session():
|
def setup_clean_session():
|
||||||
request = Request(env={})
|
request = Request(env={})
|
||||||
request.application = 'a'
|
request.application = 'a'
|
||||||
@@ -28,6 +30,53 @@ def setup_clean_session():
|
|||||||
current.session = session
|
current.session = session
|
||||||
return current
|
return current
|
||||||
|
|
||||||
|
|
||||||
|
class testRequest(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
from gluon.globals import current
|
||||||
|
current.response = Response()
|
||||||
|
|
||||||
|
def test_restful_simple(self):
|
||||||
|
env = {'request_method': 'GET', 'PATH_INFO': '/welcome/default/index/1.pdf'}
|
||||||
|
r = Request(env)
|
||||||
|
regex_url_in(r, env)
|
||||||
|
|
||||||
|
@r.restful()
|
||||||
|
def simple_rest():
|
||||||
|
def GET(*args, **vars):
|
||||||
|
return args[0]
|
||||||
|
return locals()
|
||||||
|
|
||||||
|
self.assertEqual(simple_rest(), '1')
|
||||||
|
|
||||||
|
def test_restful_calls_post(self):
|
||||||
|
env = {'request_method': 'POST', 'PATH_INFO': '/welcome/default/index'}
|
||||||
|
r = Request(env)
|
||||||
|
regex_url_in(r, env)
|
||||||
|
|
||||||
|
@r.restful()
|
||||||
|
def post_rest():
|
||||||
|
def POST(*args, **vars):
|
||||||
|
return 'I posted'
|
||||||
|
return locals()
|
||||||
|
|
||||||
|
self.assertEqual(post_rest(), 'I posted')
|
||||||
|
|
||||||
|
def test_restful_ignore_extension(self):
|
||||||
|
env = {'request_method': 'GET', 'PATH_INFO': '/welcome/default/index/127.0.0.1'}
|
||||||
|
r = Request(env)
|
||||||
|
regex_url_in(r, env)
|
||||||
|
|
||||||
|
@r.restful(ignore_extension=True)
|
||||||
|
def ignore_rest():
|
||||||
|
def GET(*args, **vars):
|
||||||
|
return args[0]
|
||||||
|
return locals()
|
||||||
|
|
||||||
|
self.assertEqual(ignore_rest(), '127.0.0.1')
|
||||||
|
|
||||||
|
|
||||||
class testResponse(unittest.TestCase):
|
class testResponse(unittest.TestCase):
|
||||||
|
|
||||||
# port from python 2.7, needed for 2.5 and 2.6 tests
|
# port from python 2.7, needed for 2.5 and 2.6 tests
|
||||||
@@ -115,7 +164,6 @@ class testResponse(unittest.TestCase):
|
|||||||
'<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>'
|
'<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
response = Response()
|
response = Response()
|
||||||
response.files.append(URL('a', 'static', 'css/file.js'))
|
response.files.append(URL('a', 'static', 'css/file.js'))
|
||||||
response.files.append(URL('a', 'static', 'css/file.css'))
|
response.files.append(URL('a', 'static', 'css/file.css'))
|
||||||
|
|||||||
@@ -235,6 +235,11 @@ class TestValidators(unittest.TestCase):
|
|||||||
self.assertEqual(sorted(rtn), [('%d' % george_id, 'george'), ('%d' % costanza_id, 'costanza')])
|
self.assertEqual(sorted(rtn), [('%d' % george_id, 'george'), ('%d' % costanza_id, 'costanza')])
|
||||||
rtn = IS_IN_DB(db, db.person.id, db.person.name, error_message='oops', sort=True).options(zero=True)
|
rtn = IS_IN_DB(db, db.person.id, db.person.name, error_message='oops', sort=True).options(zero=True)
|
||||||
self.assertEqual(rtn, [('', ''), ('%d' % costanza_id, 'costanza'), ('%d' % george_id, 'george')])
|
self.assertEqual(rtn, [('', ''), ('%d' % costanza_id, 'costanza'), ('%d' % george_id, 'george')])
|
||||||
|
# Test None
|
||||||
|
rtn = IS_IN_DB(db, 'person.id', '%(name)s', error_message='oops')(None)
|
||||||
|
self.assertEqual(rtn, (None, 'oops'))
|
||||||
|
rtn = IS_IN_DB(db, 'person.name', '%(name)s', error_message='oops')(None)
|
||||||
|
self.assertEqual(rtn, (None, 'oops'))
|
||||||
# Test using the set it made for options
|
# Test using the set it made for options
|
||||||
vldtr = IS_IN_DB(db, 'person.name', '%(name)s', error_message='oops')
|
vldtr = IS_IN_DB(db, 'person.name', '%(name)s', error_message='oops')
|
||||||
vldtr.options()
|
vldtr.options()
|
||||||
|
|||||||
+3
-2
@@ -20,7 +20,8 @@ import urllib
|
|||||||
import struct
|
import struct
|
||||||
import decimal
|
import decimal
|
||||||
import unicodedata
|
import unicodedata
|
||||||
from gluon._compat import StringIO, long, basestring, unicodeT, to_unicode, urllib_unquote, unichr, to_bytes, PY2, to_unicode, to_native, urlparse
|
|
||||||
|
from gluon._compat import StringIO, long, basestring, unicodeT, to_unicode, urllib_unquote, unichr, to_bytes, PY2, to_unicode, to_native, string_types, urlparse
|
||||||
from gluon.utils import simple_hash, web2py_uuid, DIGEST_ALG_BY_SIZE
|
from gluon.utils import simple_hash, web2py_uuid, DIGEST_ALG_BY_SIZE
|
||||||
from pydal.objects import Field, FieldVirtual, FieldMethod
|
from pydal.objects import Field, FieldVirtual, FieldMethod
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
@@ -658,7 +659,7 @@ class IS_IN_DB(Validator):
|
|||||||
return (values, None)
|
return (values, None)
|
||||||
else:
|
else:
|
||||||
if field.type in ('id', 'integer'):
|
if field.type in ('id', 'integer'):
|
||||||
if isinstance(value, (int, long)) or value.isdigit():
|
if isinstance(value, (int, long)) or (isinstance(value, string_types) and value.isdigit()):
|
||||||
value = int(value)
|
value = int(value)
|
||||||
elif self.auto_add:
|
elif self.auto_add:
|
||||||
value = self.maybe_add(table, self.fieldnames[0], value)
|
value = self.maybe_add(table, self.fieldnames[0], value)
|
||||||
|
|||||||
Reference in new issue
Block a user