Skip to content

Fixed tests on Windows. Added unicode support to SlugField #5231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,13 +791,17 @@ def __init__(self, regex, **kwargs):

class SlugField(CharField):
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)
slug_regex = re.compile(r'^[-a-zA-Z0-9_]+$')
validator = RegexValidator(slug_regex, message=self.error_messages['invalid'])
self.allow_unicode = allow_unicode
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)


Expand Down
24 changes: 12 additions & 12 deletions tests/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,13 @@ def test_multipart_encoding(self):
client = CoreAPIClient()
schema = client.get('http://api.example.com/')

temp = tempfile.NamedTemporaryFile()
temp.write(b'example file content')
temp.flush()
with tempfile.NamedTemporaryFile() as temp:
temp.write(b'example file content')
temp.flush()
temp.seek(0)

with open(temp.name, 'rb') as upload:
name = os.path.basename(upload.name)
data = client.action(schema, ['encoding', 'multipart'], params={'example': upload})
name = os.path.basename(temp.name)
data = client.action(schema, ['encoding', 'multipart'], params={'example': temp})

expected = {
'method': 'POST',
Expand Down Expand Up @@ -407,13 +407,13 @@ def test_raw_upload(self):
client = CoreAPIClient()
schema = client.get('http://api.example.com/')

temp = tempfile.NamedTemporaryFile()
temp.write(b'example file content')
temp.flush()
with tempfile.NamedTemporaryFile(delete=False) as temp:
temp.write(b'example file content')
temp.flush()
temp.seek(0)

with open(temp.name, 'rb') as upload:
name = os.path.basename(upload.name)
data = client.action(schema, ['encoding', 'raw_upload'], params={'example': upload})
name = os.path.basename(temp.name)
data = client.action(schema, ['encoding', 'raw_upload'], params={'example': temp})

expected = {
'method': 'POST',
Expand Down
11 changes: 11 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,17 @@ class TestSlugField(FieldValues):
outputs = {}
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):
"""
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy