From abb55a490964790a65ad5ef32397c6046d03d889 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 30 Dec 2010 13:52:46 +0000 Subject: Add styling and urlizing to html views of resources --- src/rest/resource.py | 10 ++- src/rest/templates/emitter.html | 13 +++- src/rest/templatetags/__init__.py | 0 src/rest/templatetags/__init__.pyc | Bin 0 -> 163 bytes src/rest/templatetags/urlize_quoted_links.py | 96 ++++++++++++++++++++++++++ src/rest/templatetags/urlize_quoted_links.pyc | Bin 0 -> 4515 bytes 6 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 src/rest/templatetags/__init__.py create mode 100644 src/rest/templatetags/__init__.pyc create mode 100644 src/rest/templatetags/urlize_quoted_links.py create mode 100644 src/rest/templatetags/urlize_quoted_links.pyc (limited to 'src/rest') diff --git a/src/rest/resource.py b/src/rest/resource.py index 18421a19..4e9c4e05 100644 --- a/src/rest/resource.py +++ b/src/rest/resource.py @@ -1,6 +1,8 @@ from django.http import HttpResponse -from decimal import Decimal +from django.core.urlresolvers import reverse from rest import emitters, parsers +from decimal import Decimal + class Resource(object): @@ -29,6 +31,7 @@ class Resource(object): def __new__(cls, request, *args, **kwargs): self = object.__new__(cls) self.__init__() + self._request = request return self._handle_request(request, *args, **kwargs) def __init__(self): @@ -145,3 +148,8 @@ class Resource(object): def delete(self, headers={}, *args, **kwargs): return self._not_implemented('delete') + + def reverse(self, view, *args, **kwargs): + """Return a fully qualified URI for a view, using the current request as the base URI. + """ + return self._request.build_absolute_uri(reverse(view, *args, **kwargs)) diff --git a/src/rest/templates/emitter.html b/src/rest/templates/emitter.html index 4c843aa3..b3a2d823 100644 --- a/src/rest/templates/emitter.html +++ b/src/rest/templates/emitter.html @@ -1,11 +1,18 @@ - +{% load urlize_quoted_links %} +
+ +{{ resource_doc }}
-
-{% include 'emitter.txt' %}
+ {% autoescape off %}HTTP Status {{ status }}
+{% for key, val in headers.items %}{{ key }}: {{ val }}
+{% endfor %}
+{{ content|urlize_quoted_links }}{% endautoescape %}
\ No newline at end of file
diff --git a/src/rest/templatetags/__init__.py b/src/rest/templatetags/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/rest/templatetags/__init__.pyc b/src/rest/templatetags/__init__.pyc
new file mode 100644
index 00000000..69527f63
Binary files /dev/null and b/src/rest/templatetags/__init__.pyc differ
diff --git a/src/rest/templatetags/urlize_quoted_links.py b/src/rest/templatetags/urlize_quoted_links.py
new file mode 100644
index 00000000..cef179bf
--- /dev/null
+++ b/src/rest/templatetags/urlize_quoted_links.py
@@ -0,0 +1,96 @@
+"""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
+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 = ['(', '<', '<', '"', "'"]
+TRAILING_PUNCTUATION = ['.', ',', ')', '>', '\n', '>', '"', "'"]
+
+# List of possible strings used for bullets in bulleted lists.
+DOTS = ['·', '*', '\xe2\x80\xa2', '', '•', '•']
+
+unencoded_ampersands_re = re.compile(r'&(?!(\w+|#\d+);)')
+word_split_re = re.compile(r'(\s+)')
+punctuation_re = re.compile('^(?P(?:%s).*?[a-zA-Z].*?
\s*)+)' % '|'.join([re.escape(x) for x in DOTS]), re.DOTALL) +trailing_empty_content_re = re.compile(r'(?:(?: |\s|
)*?