mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 10:03:57 +03:00
Optional login/logout tags so browseable API will work without requiring auth views
This commit is contained in:
parent
215de9af81
commit
f1dc9be5fc
|
@ -4,6 +4,7 @@
|
||||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
{% load urlize_quoted_links %}
|
{% load urlize_quoted_links %}
|
||||||
{% load add_query_param %}
|
{% load add_query_param %}
|
||||||
|
{% load optional_login %}
|
||||||
{% load static %}
|
{% load static %}
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||||
<head>
|
<head>
|
||||||
|
@ -24,10 +25,9 @@
|
||||||
{% block userlinks %}
|
{% block userlinks %}
|
||||||
{% if user.is_active %}
|
{% if user.is_active %}
|
||||||
Welcome, {{ user }}.
|
Welcome, {{ user }}.
|
||||||
<a href='{% url 'djangorestframework:logout' %}?next={{ request.path }}'>Log out</a>
|
{% optional_login %}
|
||||||
{% else %}
|
{% else %}
|
||||||
Anonymous
|
{% optional_logout %}
|
||||||
<a href='{% url 'djangorestframework:login' %}?next={{ request.path }}'>Log in</a>
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
</div>
|
</div>
|
||||||
|
|
32
djangorestframework/templatetags/optional_login.py
Normal file
32
djangorestframework/templatetags/optional_login.py
Normal file
|
@ -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
|
|
@ -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
|
This is identical to the built-in filter 'urlize' with the exception that
|
||||||
single and double quotes are permitted as leading or trailing punctuation.
|
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 re
|
||||||
import string
|
import string
|
||||||
|
|
||||||
from django.utils.safestring import SafeData, mark_safe
|
from django.utils.safestring import SafeData, mark_safe
|
||||||
from django.utils.encoding import force_unicode
|
from django.utils.encoding import force_unicode
|
||||||
from django.utils.http import urlquote
|
|
||||||
from django.utils.html import escape
|
from django.utils.html import escape
|
||||||
from django import template
|
from django import template
|
||||||
|
|
||||||
# Configuration for urlize() function.
|
# Configuration for urlize() function.
|
||||||
LEADING_PUNCTUATION = ['(', '<', '<', '"', "'"]
|
LEADING_PUNCTUATION = ['(', '<', '<', '"', "'"]
|
||||||
TRAILING_PUNCTUATION = ['.', ',', ')', '>', '\n', '>', '"', "'"]
|
TRAILING_PUNCTUATION = ['.', ',', ')', '>', '\n', '>', '"', "'"]
|
||||||
|
|
||||||
# List of possible strings used for bullets in bulleted lists.
|
# 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)
|
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>(?: |\s|<br \/>)*?</p>\s*)+\Z')
|
trailing_empty_content_re = re.compile(r'(?:<p>(?: |\s|<br \/>)*?</p>\s*)+\Z')
|
||||||
|
|
||||||
|
|
||||||
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.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user