Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
3dab9056
Commit
3dab9056
authored
Jul 07, 2017
by
Tom Christie
Committed by
GitHub
Jul 07, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5231 from dmmatson/feature/slugfield-allow-unicode
Fixed tests on Windows. Added unicode support to SlugField
parents
1a31959d
302a9d08
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
16 deletions
+31
-16
rest_framework/fields.py
+8
-4
tests/test_api_client.py
+12
-12
tests/test_fields.py
+11
-0
No files found.
rest_framework/fields.py
View file @
3dab9056
...
@@ -791,13 +791,17 @@ class RegexField(CharField):
...
@@ -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
)
...
...
tests/test_api_client.py
View file @
3dab9056
...
@@ -275,13 +275,13 @@ class APIClientTests(APITestCase):
...
@@ -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):
...
@@ -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'
,
...
...
tests/test_fields.py
View file @
3dab9056
...
@@ -704,6 +704,17 @@ class TestSlugField(FieldValues):
...
@@ -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
):
"""
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment