mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-03 21:24:33 +03:00
Merge pull request #5231 from dmmatson/feature/slugfield-allow-unicode
Fixed tests on Windows. Added unicode support to SlugField
This commit is contained in:
commit
3dab905656
|
@ -791,13 +791,17 @@ class RegexField(CharField):
|
||||||
|
|
||||||
class SlugField(CharField):
|
class SlugField(CharField):
|
||||||
default_error_messages = {
|
default_error_messages = {
|
||||||
'invalid': _('Enter a valid "slug" consisting of letters, numbers, underscores or hyphens.')
|
'invalid': _('Enter a valid "slug" consisting of letters, numbers, underscores or hyphens.'),
|
||||||
|
'invalid_unicode': _('Enter a valid "slug" consisting of Unicode letters, numbers, underscores, or hyphens.')
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, allow_unicode=False, **kwargs):
|
||||||
super(SlugField, self).__init__(**kwargs)
|
super(SlugField, self).__init__(**kwargs)
|
||||||
slug_regex = re.compile(r'^[-a-zA-Z0-9_]+$')
|
self.allow_unicode = allow_unicode
|
||||||
validator = RegexValidator(slug_regex, message=self.error_messages['invalid'])
|
if self.allow_unicode:
|
||||||
|
validator = RegexValidator(re.compile(r'^[-\w]+\Z', re.UNICODE), message=self.error_messages['invalid_unicode'])
|
||||||
|
else:
|
||||||
|
validator = RegexValidator(re.compile(r'^[-a-zA-Z0-9_]+$'), message=self.error_messages['invalid'])
|
||||||
self.validators.append(validator)
|
self.validators.append(validator)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -275,13 +275,13 @@ class APIClientTests(APITestCase):
|
||||||
client = CoreAPIClient()
|
client = CoreAPIClient()
|
||||||
schema = client.get('http://api.example.com/')
|
schema = client.get('http://api.example.com/')
|
||||||
|
|
||||||
temp = tempfile.NamedTemporaryFile()
|
with tempfile.NamedTemporaryFile() as temp:
|
||||||
temp.write(b'example file content')
|
temp.write(b'example file content')
|
||||||
temp.flush()
|
temp.flush()
|
||||||
|
temp.seek(0)
|
||||||
|
|
||||||
with open(temp.name, 'rb') as upload:
|
name = os.path.basename(temp.name)
|
||||||
name = os.path.basename(upload.name)
|
data = client.action(schema, ['encoding', 'multipart'], params={'example': temp})
|
||||||
data = client.action(schema, ['encoding', 'multipart'], params={'example': upload})
|
|
||||||
|
|
||||||
expected = {
|
expected = {
|
||||||
'method': 'POST',
|
'method': 'POST',
|
||||||
|
@ -407,13 +407,13 @@ class APIClientTests(APITestCase):
|
||||||
client = CoreAPIClient()
|
client = CoreAPIClient()
|
||||||
schema = client.get('http://api.example.com/')
|
schema = client.get('http://api.example.com/')
|
||||||
|
|
||||||
temp = tempfile.NamedTemporaryFile()
|
with tempfile.NamedTemporaryFile(delete=False) as temp:
|
||||||
temp.write(b'example file content')
|
temp.write(b'example file content')
|
||||||
temp.flush()
|
temp.flush()
|
||||||
|
temp.seek(0)
|
||||||
|
|
||||||
with open(temp.name, 'rb') as upload:
|
name = os.path.basename(temp.name)
|
||||||
name = os.path.basename(upload.name)
|
data = client.action(schema, ['encoding', 'raw_upload'], params={'example': temp})
|
||||||
data = client.action(schema, ['encoding', 'raw_upload'], params={'example': upload})
|
|
||||||
|
|
||||||
expected = {
|
expected = {
|
||||||
'method': 'POST',
|
'method': 'POST',
|
||||||
|
|
|
@ -704,6 +704,17 @@ class TestSlugField(FieldValues):
|
||||||
outputs = {}
|
outputs = {}
|
||||||
field = serializers.SlugField()
|
field = serializers.SlugField()
|
||||||
|
|
||||||
|
def test_allow_unicode_true(self):
|
||||||
|
field = serializers.SlugField(allow_unicode=True)
|
||||||
|
|
||||||
|
validation_error = False
|
||||||
|
try:
|
||||||
|
field.run_validation(u'slug-99-\u0420')
|
||||||
|
except serializers.ValidationError:
|
||||||
|
validation_error = True
|
||||||
|
|
||||||
|
assert not validation_error
|
||||||
|
|
||||||
|
|
||||||
class TestURLField(FieldValues):
|
class TestURLField(FieldValues):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user