Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3dab905

Browse files
Merge pull request#5231 from dmmatson/feature/slugfield-allow-unicode
Fixed tests on Windows. Added unicode support to SlugField
2 parents1a31959 +302a9d0 commit3dab905

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

‎rest_framework/fields.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -791,13 +791,17 @@ def __init__(self, regex, **kwargs):
791791

792792
classSlugField(CharField):
793793
default_error_messages= {
794-
'invalid':_('Enter a valid "slug" consisting of letters, numbers, underscores or hyphens.')
794+
'invalid':_('Enter a valid "slug" consisting of letters, numbers, underscores or hyphens.'),
795+
'invalid_unicode':_('Enter a valid "slug" consisting of Unicode letters, numbers, underscores, or hyphens.')
795796
}
796797

797-
def__init__(self,**kwargs):
798+
def__init__(self,allow_unicode=False,**kwargs):
798799
super(SlugField,self).__init__(**kwargs)
799-
slug_regex=re.compile(r'^[-a-zA-Z0-9_]+$')
800-
validator=RegexValidator(slug_regex,message=self.error_messages['invalid'])
800+
self.allow_unicode=allow_unicode
801+
ifself.allow_unicode:
802+
validator=RegexValidator(re.compile(r'^[-\w]+\Z',re.UNICODE),message=self.error_messages['invalid_unicode'])
803+
else:
804+
validator=RegexValidator(re.compile(r'^[-a-zA-Z0-9_]+$'),message=self.error_messages['invalid'])
801805
self.validators.append(validator)
802806

803807

‎tests/test_api_client.py‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,13 @@ def test_multipart_encoding(self):
275275
client=CoreAPIClient()
276276
schema=client.get('http://api.example.com/')
277277

278-
temp=tempfile.NamedTemporaryFile()
279-
temp.write(b'example file content')
280-
temp.flush()
278+
withtempfile.NamedTemporaryFile()astemp:
279+
temp.write(b'example file content')
280+
temp.flush()
281+
temp.seek(0)
281282

282-
withopen(temp.name,'rb')asupload:
283-
name=os.path.basename(upload.name)
284-
data=client.action(schema, ['encoding','multipart'],params={'example':upload})
283+
name=os.path.basename(temp.name)
284+
data=client.action(schema, ['encoding','multipart'],params={'example':temp})
285285

286286
expected= {
287287
'method':'POST',
@@ -407,13 +407,13 @@ def test_raw_upload(self):
407407
client=CoreAPIClient()
408408
schema=client.get('http://api.example.com/')
409409

410-
temp=tempfile.NamedTemporaryFile()
411-
temp.write(b'example file content')
412-
temp.flush()
410+
withtempfile.NamedTemporaryFile(delete=False)astemp:
411+
temp.write(b'example file content')
412+
temp.flush()
413+
temp.seek(0)
413414

414-
withopen(temp.name,'rb')asupload:
415-
name=os.path.basename(upload.name)
416-
data=client.action(schema, ['encoding','raw_upload'],params={'example':upload})
415+
name=os.path.basename(temp.name)
416+
data=client.action(schema, ['encoding','raw_upload'],params={'example':temp})
417417

418418
expected= {
419419
'method':'POST',

‎tests/test_fields.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,17 @@ class TestSlugField(FieldValues):
704704
outputs= {}
705705
field=serializers.SlugField()
706706

707+
deftest_allow_unicode_true(self):
708+
field=serializers.SlugField(allow_unicode=True)
709+
710+
validation_error=False
711+
try:
712+
field.run_validation(u'slug-99-\u0420')
713+
exceptserializers.ValidationError:
714+
validation_error=True
715+
716+
assertnotvalidation_error
717+
707718

708719
classTestURLField(FieldValues):
709720
"""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp