aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Christie2012-09-07 10:20:25 +0100
committerTom Christie2012-09-07 10:20:25 +0100
commitf1dc9be5fcbb8f89c4e2929ddee835ef251c8e71 (patch)
tree313992349f834f3d279b7ada9884b35562cf0e7d
parent215de9af81007e81def7ee9f9590e0c12dfec272 (diff)
downloaddjango-rest-framework-f1dc9be5fcbb8f89c4e2929ddee835ef251c8e71.tar.bz2
Optional login/logout tags so browseable API will work without requiring auth views
-rw-r--r--djangorestframework/templates/djangorestframework/base.html6
-rw-r--r--djangorestframework/templatetags/optional_login.py32
-rw-r--r--djangorestframework/templatetags/urlize_quoted_links.py12
3 files changed, 42 insertions, 8 deletions
diff --git a/djangorestframework/templates/djangorestframework/base.html b/djangorestframework/templates/djangorestframework/base.html
index a4988d1d..7a3e71eb 100644
--- a/djangorestframework/templates/djangorestframework/base.html
+++ b/djangorestframework/templates/djangorestframework/base.html
@@ -4,6 +4,7 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
{% load urlize_quoted_links %}
{% load add_query_param %}
+{% load optional_login %}
{% load static %}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
@@ -24,10 +25,9 @@
{% block userlinks %}
{% if user.is_active %}
Welcome, {{ user }}.
- <a href='{% url 'djangorestframework:logout' %}?next={{ request.path }}'>Log out</a>
+ {% optional_login %}
{% else %}
- Anonymous
- <a href='{% url 'djangorestframework:login' %}?next={{ request.path }}'>Log in</a>
+ {% optional_logout %}
{% endif %}
{% endblock %}
</div>
diff --git a/djangorestframework/templatetags/optional_login.py b/djangorestframework/templatetags/optional_login.py
new file mode 100644
index 00000000..c448c142
--- /dev/null
+++ b/djangorestframework/templatetags/optional_login.py
@@ -0,0 +1,32 @@
+"""
+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('djangorestframework: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('djangorestframework:logout')
+ except NoReverseMatch:
+ return ''
+
+ request = context['request']
+ snippet = "<a href='%s?next=%s'>Log out</a>" % (logout_url, request.path)
+ return snippet
diff --git a/djangorestframework/templatetags/urlize_quoted_links.py b/djangorestframework/templatetags/urlize_quoted_links.py
index ffe859c9..e8852fad 100644
--- a/djangorestframework/templatetags/urlize_quoted_links.py
+++ b/djangorestframework/templatetags/urlize_quoted_links.py
@@ -1,22 +1,23 @@
-"""Adds the custom filter 'urlize_quoted_links'
+"""
+Adds the custom filter 'urlize_quoted_links'
This is identical to the built-in filter 'urlize' with the exception that
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
"""
-# Almost all of this code is copied verbatim from django.utils.html
-# LEADING_PUNCTUATION and TRAILING_PUNCTUATION have been modified
import re
import string
from django.utils.safestring import SafeData, mark_safe
from django.utils.encoding import force_unicode
-from django.utils.http import urlquote
from django.utils.html import escape
from django import template
# Configuration for urlize() function.
-LEADING_PUNCTUATION = ['(', '<', '&lt;', '"', "'"]
+LEADING_PUNCTUATION = ['(', '<', '&lt;', '"', "'"]
TRAILING_PUNCTUATION = ['.', ',', ')', '>', '\n', '&gt;', '"', "'"]
# List of possible strings used for bullets in bulleted lists.
@@ -33,6 +34,7 @@ html_gunk_re = re.compile(r'(?:<br clear="all">|<i><\/i>|<b><\/b>|<em><\/em>|<st
hard_coded_bullets_re = re.compile(r'((?:<p>(?:%s).*?[a-zA-Z].*?</p>\s*)+)' % '|'.join([re.escape(x) for x in DOTS]), re.DOTALL)
trailing_empty_content_re = re.compile(r'(?:<p>(?:&nbsp;|\s|<br \/>)*?</p>\s*)+\Z')
+
def urlize_quoted_links(text, trim_url_limit=None, nofollow=True, autoescape=True):
"""
Converts any URLs in text into clickable links.