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
a06252f8
Commit
a06252f8
authored
Feb 13, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1397 from amezhenin/issue_1386
update regex for matching URLs, fixes issue #1386
parents
69b8ec4e
dbd993d1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
5 deletions
+47
-5
rest_framework/templatetags/rest_framework.py
+14
-4
rest_framework/tests/test_templatetags.py
+33
-1
No files found.
rest_framework/templatetags/rest_framework.py
View file @
a06252f8
...
@@ -6,7 +6,7 @@ from django.utils.encoding import iri_to_uri
...
@@ -6,7 +6,7 @@ from django.utils.encoding import iri_to_uri
from
django.utils.html
import
escape
from
django.utils.html
import
escape
from
django.utils.safestring
import
SafeData
,
mark_safe
from
django.utils.safestring
import
SafeData
,
mark_safe
from
rest_framework.compat
import
urlparse
,
force_text
,
six
,
smart_urlquote
from
rest_framework.compat
import
urlparse
,
force_text
,
six
,
smart_urlquote
import
re
,
string
import
re
register
=
template
.
Library
()
register
=
template
.
Library
()
...
@@ -189,6 +189,17 @@ simple_url_2_re = re.compile(r'^www\.|^(?!http)\w[^@]+\.(com|edu|gov|int|mil|net
...
@@ -189,6 +189,17 @@ simple_url_2_re = re.compile(r'^www\.|^(?!http)\w[^@]+\.(com|edu|gov|int|mil|net
simple_email_re
=
re
.
compile
(
r'^\S+@\S+\.\S+$'
)
simple_email_re
=
re
.
compile
(
r'^\S+@\S+\.\S+$'
)
def
smart_urlquote_wrapper
(
matched_url
):
"""
Simple wrapper for smart_urlquote. ValueError("Invalid IPv6 URL") can
be raised here, see issue #1386
"""
try
:
return
smart_urlquote
(
matched_url
)
except
ValueError
:
return
None
@register.filter
@register.filter
def
urlize_quoted_links
(
text
,
trim_url_limit
=
None
,
nofollow
=
True
,
autoescape
=
True
):
def
urlize_quoted_links
(
text
,
trim_url_limit
=
None
,
nofollow
=
True
,
autoescape
=
True
):
"""
"""
...
@@ -211,7 +222,6 @@ def urlize_quoted_links(text, trim_url_limit=None, nofollow=True, autoescape=Tru
...
@@ -211,7 +222,6 @@ def urlize_quoted_links(text, trim_url_limit=None, nofollow=True, autoescape=Tru
safe_input
=
isinstance
(
text
,
SafeData
)
safe_input
=
isinstance
(
text
,
SafeData
)
words
=
word_split_re
.
split
(
force_text
(
text
))
words
=
word_split_re
.
split
(
force_text
(
text
))
for
i
,
word
in
enumerate
(
words
):
for
i
,
word
in
enumerate
(
words
):
match
=
None
if
'.'
in
word
or
'@'
in
word
or
':'
in
word
:
if
'.'
in
word
or
'@'
in
word
or
':'
in
word
:
# Deal with punctuation.
# Deal with punctuation.
lead
,
middle
,
trail
=
''
,
word
,
''
lead
,
middle
,
trail
=
''
,
word
,
''
...
@@ -233,9 +243,9 @@ def urlize_quoted_links(text, trim_url_limit=None, nofollow=True, autoescape=Tru
...
@@ -233,9 +243,9 @@ def urlize_quoted_links(text, trim_url_limit=None, nofollow=True, autoescape=Tru
url
=
None
url
=
None
nofollow_attr
=
' rel="nofollow"'
if
nofollow
else
''
nofollow_attr
=
' rel="nofollow"'
if
nofollow
else
''
if
simple_url_re
.
match
(
middle
):
if
simple_url_re
.
match
(
middle
):
url
=
smart_urlquote
(
middle
)
url
=
smart_urlquote
_wrapper
(
middle
)
elif
simple_url_2_re
.
match
(
middle
):
elif
simple_url_2_re
.
match
(
middle
):
url
=
smart_urlquote
(
'http://
%
s'
%
middle
)
url
=
smart_urlquote
_wrapper
(
'http://
%
s'
%
middle
)
elif
not
':'
in
middle
and
simple_email_re
.
match
(
middle
):
elif
not
':'
in
middle
and
simple_email_re
.
match
(
middle
):
local
,
domain
=
middle
.
rsplit
(
'@'
,
1
)
local
,
domain
=
middle
.
rsplit
(
'@'
,
1
)
try
:
try
:
...
...
rest_framework/tests/test_templatetags.py
View file @
a06252f8
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
from
django.test
import
TestCase
from
django.test
import
TestCase
from
rest_framework.test
import
APIRequestFactory
from
rest_framework.test
import
APIRequestFactory
from
rest_framework.templatetags.rest_framework
import
add_query_param
from
rest_framework.templatetags.rest_framework
import
add_query_param
,
urlize_quoted_links
factory
=
APIRequestFactory
()
factory
=
APIRequestFactory
()
...
@@ -17,3 +17,35 @@ class TemplateTagTests(TestCase):
...
@@ -17,3 +17,35 @@ class TemplateTagTests(TestCase):
json_url
=
add_query_param
(
request
,
"format"
,
"json"
)
json_url
=
add_query_param
(
request
,
"format"
,
"json"
)
self
.
assertIn
(
"q=
%
E6
%9
F
%
A5
%
E8
%
AF
%
A2"
,
json_url
)
self
.
assertIn
(
"q=
%
E6
%9
F
%
A5
%
E8
%
AF
%
A2"
,
json_url
)
self
.
assertIn
(
"format=json"
,
json_url
)
self
.
assertIn
(
"format=json"
,
json_url
)
class
Issue1386Tests
(
TestCase
):
"""
Covers #1386
"""
def
test_issue_1386
(
self
):
"""
Test function urlize_quoted_links with different args
"""
correct_urls
=
[
"asdf.com"
,
"asdf.net"
,
"www.as_df.org"
,
"as.d8f.ghj8.gov"
,
]
for
i
in
correct_urls
:
res
=
urlize_quoted_links
(
i
)
self
.
assertNotEqual
(
res
,
i
)
self
.
assertIn
(
i
,
res
)
incorrect_urls
=
[
"mailto://asdf@fdf.com"
,
"asdf.netnet"
,
]
for
i
in
incorrect_urls
:
res
=
urlize_quoted_links
(
i
)
self
.
assertEqual
(
i
,
res
)
# example from issue #1386, this shouldn't raise an exception
_
=
urlize_quoted_links
(
"asdf:[/p]zxcv.com"
)
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