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.