From f1dc9be5fcbb8f89c4e2929ddee835ef251c8e71 Mon Sep 17 00:00:00 2001
From: Tom Christie
Date: Fri, 7 Sep 2012 10:20:25 +0100
Subject: [PATCH] Optional login/logout tags so browseable API will work
without requiring auth views
---
.../templates/djangorestframework/base.html | 6 ++--
.../templatetags/optional_login.py | 32 +++++++++++++++++++
.../templatetags/urlize_quoted_links.py | 12 ++++---
3 files changed, 42 insertions(+), 8 deletions(-)
create mode 100644 djangorestframework/templatetags/optional_login.py
diff --git a/djangorestframework/templates/djangorestframework/base.html b/djangorestframework/templates/djangorestframework/base.html
index a4988d1d9..7a3e71eb7 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 %}
@@ -24,10 +25,9 @@
{% block userlinks %}
{% if user.is_active %}
Welcome, {{ user }}.
- Log out
+ {% optional_login %}
{% else %}
- Anonymous
- Log in
+ {% optional_logout %}
{% endif %}
{% endblock %}
diff --git a/djangorestframework/templatetags/optional_login.py b/djangorestframework/templatetags/optional_login.py
new file mode 100644
index 000000000..c448c142b
--- /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 = "Log in" % (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 = "Log out" % (logout_url, request.path)
+ return snippet
diff --git a/djangorestframework/templatetags/urlize_quoted_links.py b/djangorestframework/templatetags/urlize_quoted_links.py
index ffe859c98..e8852fad0 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 = ['(', '<', '<', '"', "'"]
+LEADING_PUNCTUATION = ['(', '<', '<', '"', "'"]
TRAILING_PUNCTUATION = ['.', ',', ')', '>', '\n', '>', '"', "'"]
# List of possible strings used for bullets in bulleted lists.
@@ -33,6 +34,7 @@ html_gunk_re = re.compile(r'(?:
|<\/i>|<\/b>|<\/em>|(?:%s).*?[a-zA-Z].*?
\s*)+)' % '|'.join([re.escape(x) for x in DOTS]), re.DOTALL)
trailing_empty_content_re = re.compile(r'(?:(?: |\s|
)*?
\s*)+\Z')
+
def urlize_quoted_links(text, trim_url_limit=None, nofollow=True, autoescape=True):
"""
Converts any URLs in text into clickable links.