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
5a82aa97
Commit
5a82aa97
authored
Sep 20, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clean up template tags
parent
7efc6e82
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
98 additions
and
123 deletions
+98
-123
rest_framework/templates/rest_framework/base.html
+1
-4
rest_framework/templatetags/add_class.py
+0
-41
rest_framework/templatetags/add_query_param.py
+0
-20
rest_framework/templatetags/optional_login.py
+0
-32
rest_framework/templatetags/rest_framework.py
+97
-26
No files found.
rest_framework/templates/rest_framework/base.html
View file @
5a82aa97
{% load url from future %}
{% load url from future %}
{% load urlize_quoted_links %}
{% load rest_framework %}
{% load add_query_param %}
{% load add_class %}
{% load optional_login %}
{% load static %}
{% load static %}
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<html>
...
...
rest_framework/templatetags/add_class.py
deleted
100644 → 0
View file @
7efc6e82
"""
From http://stackoverflow.com/questions/4124220/django-adding-css-classes-when-rendering-form-fields-in-a-template
The add_class filter allows for inserting classes into template variables that
contain HTML tags, useful for modifying forms without needing to change the
Form objects.
To use:
{{ field.label_tag|add_class:"control-label" }}
will insert the class `controls-label` into the label tag generated by a form.
In the case of Django REST Framework, the filter is used to add Bootstrap-specific
classes to the forms, while still allowing non-Bootstrap customization of the
browsable API.
"""
import
re
from
django.utils.safestring
import
mark_safe
from
django
import
template
register
=
template
.
Library
()
class_re
=
re
.
compile
(
r'(?<=class=["\'])(.*)(?=["\'])'
)
@register.filter
def
add_class
(
value
,
css_class
):
string
=
unicode
(
value
)
match
=
class_re
.
search
(
string
)
if
match
:
m
=
re
.
search
(
r'^
%
s$|^
%
s\s|\s
%
s\s|\s
%
s$'
%
(
css_class
,
css_class
,
css_class
,
css_class
),
match
.
group
(
1
))
print
match
.
group
(
1
)
if
not
m
:
return
mark_safe
(
class_re
.
sub
(
match
.
group
(
1
)
+
" "
+
css_class
,
string
))
else
:
return
mark_safe
(
string
.
replace
(
'>'
,
' class="
%
s">'
%
css_class
,
1
))
return
value
\ No newline at end of file
rest_framework/templatetags/add_query_param.py
deleted
100644 → 0
View file @
7efc6e82
from
django.http
import
QueryDict
from
django.template
import
Library
from
urlparse
import
urlparse
,
urlunparse
register
=
Library
()
def
replace_query_param
(
url
,
key
,
val
):
(
scheme
,
netloc
,
path
,
params
,
query
,
fragment
)
=
urlparse
(
url
)
query_dict
=
QueryDict
(
query
)
.
copy
()
query_dict
[
key
]
=
val
query
=
query_dict
.
urlencode
()
return
urlunparse
((
scheme
,
netloc
,
path
,
params
,
query
,
fragment
))
def
add_query_param
(
url
,
param
):
key
,
val
=
param
.
split
(
'='
)
return
replace_query_param
(
url
,
key
,
val
)
register
.
filter
(
'add_query_param'
,
add_query_param
)
rest_framework/templatetags/optional_login.py
deleted
100644 → 0
View file @
7efc6e82
"""
Tags to optionally include the login and logout links, depending on if the
login and logout views are in the urlconf.
"""
from
django
import
template
from
django.core.urlresolvers
import
reverse
,
NoReverseMatch
register
=
template
.
Library
()
@register.simple_tag
(
takes_context
=
True
)
def
optional_login
(
context
):
try
:
login_url
=
reverse
(
'rest_framework:login'
)
except
NoReverseMatch
:
return
''
request
=
context
[
'request'
]
snippet
=
"<a href='
%
s?next=
%
s'>Log in</a>"
%
(
login_url
,
request
.
path
)
return
snippet
@register.simple_tag
(
takes_context
=
True
)
def
optional_logout
(
context
):
try
:
logout_url
=
reverse
(
'rest_framework:logout'
)
except
NoReverseMatch
:
return
''
request
=
context
[
'request'
]
snippet
=
"<a href='
%
s?next=
%
s'>Log out</a>"
%
(
logout_url
,
request
.
path
)
return
snippet
rest_framework/templatetags/
urlize_quoted_links
.py
→
rest_framework/templatetags/
rest_framework
.py
View file @
5a82aa97
"""
from
django
import
template
Adds the custom filter 'urlize_quoted_links'
from
django.core.urlresolvers
import
reverse
,
NoReverseMatch
from
django.http
import
QueryDict
from
django.utils.encoding
import
force_unicode
from
django.utils.html
import
escape
from
django.utils.safestring
import
SafeData
,
mark_safe
from
urlparse
import
urlsplit
,
urlunsplit
import
re
import
string
This is identical to the built-in filter 'urlize' with the exception that
register
=
template
.
Library
()
single and double quotes are permitted as leading or trailing punctuation.
Almost all of this code is copied verbatim from django.utils.html
LEADING_PUNCTUATION and TRAILING_PUNCTUATION have been modified
"""
import
re
# Regex for adding classes to html snippets
import
string
class_re
=
re
.
compile
(
r'(?<=class=["\'])(.*)(?=["\'])'
)
from
django.utils.safestring
import
SafeData
,
mark_safe
from
django.utils.encoding
import
force_unicode
from
django.utils.html
import
escape
from
django
import
template
#
Configuration for urlize() function.
#
Bunch of stuff cloned from urlize
LEADING_PUNCTUATION
=
[
'('
,
'<'
,
'<'
,
'"'
,
"'"
]
LEADING_PUNCTUATION
=
[
'('
,
'<'
,
'<'
,
'"'
,
"'"
]
TRAILING_PUNCTUATION
=
[
'.'
,
','
,
')'
,
'>'
,
'
\n
'
,
'>'
,
'"'
,
"'"
]
TRAILING_PUNCTUATION
=
[
'.'
,
','
,
')'
,
'>'
,
'
\n
'
,
'>'
,
'"'
,
"'"
]
# List of possible strings used for bullets in bulleted lists.
DOTS
=
[
'·'
,
'*'
,
'
\xe2\x80\xa2
'
,
'•'
,
'•'
,
'•'
]
DOTS
=
[
'·'
,
'*'
,
'
\xe2\x80\xa2
'
,
'•'
,
'•'
,
'•'
]
unencoded_ampersands_re
=
re
.
compile
(
r'&(?!(\w+|#\d+);)'
)
unencoded_ampersands_re
=
re
.
compile
(
r'&(?!(\w+|#\d+);)'
)
word_split_re
=
re
.
compile
(
r'(\s+)'
)
word_split_re
=
re
.
compile
(
r'(\s+)'
)
punctuation_re
=
re
.
compile
(
'^(?P<lead>(?:
%
s)*)(?P<middle>.*?)(?P<trail>(?:
%
s)*)$'
%
\
punctuation_re
=
re
.
compile
(
'^(?P<lead>(?:
%
s)*)(?P<middle>.*?)(?P<trail>(?:
%
s)*)$'
%
\
...
@@ -35,6 +31,90 @@ hard_coded_bullets_re = re.compile(r'((?:<p>(?:%s).*?[a-zA-Z].*?</p>\s*)+)' % '|
...
@@ -35,6 +31,90 @@ hard_coded_bullets_re = re.compile(r'((?:<p>(?:%s).*?[a-zA-Z].*?</p>\s*)+)' % '|
trailing_empty_content_re
=
re
.
compile
(
r'(?:<p>(?: |\s|<br \/>)*?</p>\s*)+\Z'
)
trailing_empty_content_re
=
re
.
compile
(
r'(?:<p>(?: |\s|<br \/>)*?</p>\s*)+\Z'
)
# Helper function for 'add_query_param'
def
replace_query_param
(
url
,
key
,
val
):
"""
Given a URL and a key/val pair, set or replace an item in the query
parameters of the URL, and return the new URL.
"""
(
scheme
,
netloc
,
path
,
query
,
fragment
)
=
urlsplit
(
url
)
query_dict
=
QueryDict
(
query
)
.
copy
()
query_dict
[
key
]
=
val
query
=
query_dict
.
urlencode
()
return
urlunsplit
((
scheme
,
netloc
,
path
,
query
,
fragment
))
# And the template tags themselves...
@register.simple_tag
(
takes_context
=
True
)
def
optional_login
(
context
):
"""
Include a login snippet if REST framework's login view is in the URLconf.
"""
try
:
login_url
=
reverse
(
'rest_framework:login'
)
except
NoReverseMatch
:
return
''
request
=
context
[
'request'
]
snippet
=
"<a href='
%
s?next=
%
s'>Log in</a>"
%
(
login_url
,
request
.
path
)
return
snippet
@register.simple_tag
(
takes_context
=
True
)
def
optional_logout
(
context
):
"""
Include a logout snippet if REST framework's logout view is in the URLconf.
"""
try
:
logout_url
=
reverse
(
'rest_framework:logout'
)
except
NoReverseMatch
:
return
''
request
=
context
[
'request'
]
snippet
=
"<a href='
%
s?next=
%
s'>Log out</a>"
%
(
logout_url
,
request
.
path
)
return
snippet
@register.filter
def
add_query_param
(
url
,
param
):
"""
"""
key
,
val
=
param
.
split
(
'='
)
return
replace_query_param
(
url
,
key
,
val
)
@register.filter
def
add_class
(
value
,
css_class
):
"""
http://stackoverflow.com/questions/4124220/django-adding-css-classes-when-rendering-form-fields-in-a-template
Inserts classes into template variables that contain HTML tags,
useful for modifying forms without needing to change the Form objects.
Usage:
{{ field.label_tag|add_class:"control-label" }}
In the case of REST Framework, the filter is used to add Bootstrap-specific
classes to the forms.
"""
html
=
unicode
(
value
)
match
=
class_re
.
search
(
html
)
if
match
:
m
=
re
.
search
(
r'^
%
s$|^
%
s\s|\s
%
s\s|\s
%
s$'
%
(
css_class
,
css_class
,
css_class
,
css_class
),
match
.
group
(
1
))
print
match
.
group
(
1
)
if
not
m
:
return
mark_safe
(
class_re
.
sub
(
match
.
group
(
1
)
+
" "
+
css_class
,
html
))
else
:
return
mark_safe
(
html
.
replace
(
'>'
,
' class="
%
s">'
%
css_class
,
1
))
return
value
@register.filter
(
is_safe
=
True
)
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
):
"""
"""
Converts any URLs in text into clickable links.
Converts any URLs in text into clickable links.
...
@@ -91,12 +171,3 @@ def urlize_quoted_links(text, trim_url_limit=None, nofollow=True, autoescape=Tru
...
@@ -91,12 +171,3 @@ def urlize_quoted_links(text, trim_url_limit=None, nofollow=True, autoescape=Tru
elif
autoescape
:
elif
autoescape
:
words
[
i
]
=
escape
(
word
)
words
[
i
]
=
escape
(
word
)
return
u''
.
join
(
words
)
return
u''
.
join
(
words
)
#urlize_quoted_links.needs_autoescape = True
urlize_quoted_links
.
is_safe
=
True
# Register urlize_quoted_links as a custom filter
# http://docs.djangoproject.com/en/dev/howto/custom-template-tags/
register
=
template
.
Library
()
register
.
filter
(
urlize_quoted_links
)
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