From 5bb6301b7f53e3815ab1a81a5fa38721dc95b113 Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Thu, 2 Feb 2012 18:19:44 +0200 Subject: Response as a subclass of HttpResponse - first draft, not quite there yet. --- djangorestframework/utils/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'djangorestframework/utils') diff --git a/djangorestframework/utils/__init__.py b/djangorestframework/utils/__init__.py index 634d0d68..fbe55474 100644 --- a/djangorestframework/utils/__init__.py +++ b/djangorestframework/utils/__init__.py @@ -48,6 +48,13 @@ def url_resolves(url): return True +def allowed_methods(view): + """ + Return the list of uppercased allowed HTTP methods on `view`. + """ + return [method.upper() for method in view.http_method_names if hasattr(view, method)] + + # From http://www.koders.com/python/fidB6E125C586A6F49EAC38992CF3AFDAAE35651975.aspx?s=mdef:xml #class object_dict(dict): # """object view of dict, you can -- cgit v1.2.3 From af9e4f69d732cc643d6ec7ae13d4a19ac0332d44 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 21 Feb 2012 20:12:14 +0000 Subject: Merging master into develop --- djangorestframework/utils/__init__.py | 21 ++++++++++++++++++++- djangorestframework/utils/staticviews.py | 4 ++-- 2 files changed, 22 insertions(+), 3 deletions(-) (limited to 'djangorestframework/utils') diff --git a/djangorestframework/utils/__init__.py b/djangorestframework/utils/__init__.py index fbe55474..afef4f19 100644 --- a/djangorestframework/utils/__init__.py +++ b/djangorestframework/utils/__init__.py @@ -1,6 +1,7 @@ +import django from django.utils.encoding import smart_unicode from django.utils.xmlutils import SimplerXMLGenerator -from django.core.urlresolvers import resolve +from django.core.urlresolvers import resolve, reverse as django_reverse from django.conf import settings from djangorestframework.compat import StringIO @@ -180,3 +181,21 @@ class XMLRenderer(): def dict2xml(input): return XMLRenderer().dict2xml(input) + + +def reverse(viewname, request, *args, **kwargs): + """ + Do the same as :py:func:`django.core.urlresolvers.reverse` but using + *request* to build a fully qualified URL. + """ + return request.build_absolute_uri(django_reverse(viewname, *args, **kwargs)) + +if django.VERSION >= (1, 4): + from django.core.urlresolvers import reverse_lazy as django_reverse_lazy + + def reverse_lazy(viewname, request, *args, **kwargs): + """ + Do the same as :py:func:`django.core.urlresolvers.reverse_lazy` but using + *request* to build a fully qualified URL. + """ + return request.build_absolute_uri(django_reverse_lazy(viewname, *args, **kwargs)) diff --git a/djangorestframework/utils/staticviews.py b/djangorestframework/utils/staticviews.py index 9bae0ee7..7cbc0b9b 100644 --- a/djangorestframework/utils/staticviews.py +++ b/djangorestframework/utils/staticviews.py @@ -12,7 +12,7 @@ import base64 # be making settings changes in order to accomodate django-rest-framework @csrf_protect @never_cache -def api_login(request, template_name='api_login.html', +def api_login(request, template_name='djangorestframework/login.html', redirect_field_name=REDIRECT_FIELD_NAME, authentication_form=AuthenticationForm): """Displays the login form and handles the login action.""" @@ -57,5 +57,5 @@ def api_login(request, template_name='api_login.html', }, context_instance=RequestContext(request)) -def api_logout(request, next_page=None, template_name='api_login.html', redirect_field_name=REDIRECT_FIELD_NAME): +def api_logout(request, next_page=None, template_name='djangorestframework/login.html', redirect_field_name=REDIRECT_FIELD_NAME): return logout(request, next_page, template_name, redirect_field_name) -- cgit v1.2.3 From 1cde31c86d9423e9b7a7409c2ef2ba7c0500e47f Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Sat, 25 Feb 2012 18:45:17 +0000 Subject: Massive merge --- djangorestframework/utils/__init__.py | 110 ++++++++++++------------------- djangorestframework/utils/staticviews.py | 61 ----------------- 2 files changed, 41 insertions(+), 130 deletions(-) delete mode 100644 djangorestframework/utils/staticviews.py (limited to 'djangorestframework/utils') diff --git a/djangorestframework/utils/__init__.py b/djangorestframework/utils/__init__.py index afef4f19..9d250be2 100644 --- a/djangorestframework/utils/__init__.py +++ b/djangorestframework/utils/__init__.py @@ -1,24 +1,18 @@ -import django from django.utils.encoding import smart_unicode from django.utils.xmlutils import SimplerXMLGenerator -from django.core.urlresolvers import resolve, reverse as django_reverse -from django.conf import settings +from django.core.urlresolvers import resolve from djangorestframework.compat import StringIO +from djangorestframework.compat import RequestFactory as DjangoRequestFactory +from djangorestframework.request import Request import re import xml.etree.ElementTree as ET -#def admin_media_prefix(request): -# """Adds the ADMIN_MEDIA_PREFIX to the request context.""" -# return {'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX} - -from mediatypes import media_type_matches, is_form_media_type -from mediatypes import add_media_type_param, get_media_type_params, order_by_precedence - MSIE_USER_AGENT_REGEX = re.compile(r'^Mozilla/[0-9]+\.[0-9]+ \([^)]*; MSIE [0-9]+\.[0-9]+[a-z]?;[^)]*\)(?!.* Opera )') + def as_tuple(obj): """ Given an object which may be a list/tuple, another object, or None, @@ -49,45 +43,6 @@ def url_resolves(url): return True -def allowed_methods(view): - """ - Return the list of uppercased allowed HTTP methods on `view`. - """ - return [method.upper() for method in view.http_method_names if hasattr(view, method)] - - -# From http://www.koders.com/python/fidB6E125C586A6F49EAC38992CF3AFDAAE35651975.aspx?s=mdef:xml -#class object_dict(dict): -# """object view of dict, you can -# >>> a = object_dict() -# >>> a.fish = 'fish' -# >>> a['fish'] -# 'fish' -# >>> a['water'] = 'water' -# >>> a.water -# 'water' -# >>> a.test = {'value': 1} -# >>> a.test2 = object_dict({'name': 'test2', 'value': 2}) -# >>> a.test, a.test2.name, a.test2.value -# (1, 'test2', 2) -# """ -# def __init__(self, initd=None): -# if initd is None: -# initd = {} -# dict.__init__(self, initd) -# -# def __getattr__(self, item): -# d = self.__getitem__(item) -# # if value is the only key in object, you can omit it -# if isinstance(d, dict) and 'value' in d and len(d) == 1: -# return d['value'] -# else: -# return d -# -# def __setattr__(self, item, value): -# self.__setitem__(item, value) - - # From xml2dict class XML2Dict(object): @@ -99,24 +54,23 @@ class XML2Dict(object): # Save attrs and text, hope there will not be a child with same name if node.text: node_tree = node.text - for (k,v) in node.attrib.items(): - k,v = self._namespace_split(k, v) + for (k, v) in node.attrib.items(): + k, v = self._namespace_split(k, v) node_tree[k] = v #Save childrens for child in node.getchildren(): tag, tree = self._namespace_split(child.tag, self._parse_node(child)) - if tag not in node_tree: # the first time, so store it in dict + if tag not in node_tree: # the first time, so store it in dict node_tree[tag] = tree continue old = node_tree[tag] if not isinstance(old, list): node_tree.pop(tag) - node_tree[tag] = [old] # multi times, so change old dict to a list - node_tree[tag].append(tree) # add the new one + node_tree[tag] = [old] # multi times, so change old dict to a list + node_tree[tag].append(tree) # add the new one return node_tree - def _namespace_split(self, tag, value): """ Split the tag '{http://cs.sfsu.edu/csc867/myscheduler}patients' @@ -179,23 +133,41 @@ class XMLRenderer(): xml.endDocument() return stream.getvalue() + def dict2xml(input): return XMLRenderer().dict2xml(input) -def reverse(viewname, request, *args, **kwargs): +class RequestFactory(DjangoRequestFactory): """ - Do the same as :py:func:`django.core.urlresolvers.reverse` but using - *request* to build a fully qualified URL. + Replicate RequestFactory, but return Request, not HttpRequest. """ - return request.build_absolute_uri(django_reverse(viewname, *args, **kwargs)) - -if django.VERSION >= (1, 4): - from django.core.urlresolvers import reverse_lazy as django_reverse_lazy - - def reverse_lazy(viewname, request, *args, **kwargs): - """ - Do the same as :py:func:`django.core.urlresolvers.reverse_lazy` but using - *request* to build a fully qualified URL. - """ - return request.build_absolute_uri(django_reverse_lazy(viewname, *args, **kwargs)) + def get(self, *args, **kwargs): + parsers = kwargs.pop('parsers', None) + request = super(RequestFactory, self).get(*args, **kwargs) + return Request(request, parsers) + + def post(self, *args, **kwargs): + parsers = kwargs.pop('parsers', None) + request = super(RequestFactory, self).post(*args, **kwargs) + return Request(request, parsers) + + def put(self, *args, **kwargs): + parsers = kwargs.pop('parsers', None) + request = super(RequestFactory, self).put(*args, **kwargs) + return Request(request, parsers) + + def delete(self, *args, **kwargs): + parsers = kwargs.pop('parsers', None) + request = super(RequestFactory, self).delete(*args, **kwargs) + return Request(request, parsers) + + def head(self, *args, **kwargs): + parsers = kwargs.pop('parsers', None) + request = super(RequestFactory, self).head(*args, **kwargs) + return Request(request, parsers) + + def options(self, *args, **kwargs): + parsers = kwargs.pop('parsers', None) + request = super(RequestFactory, self).options(*args, **kwargs) + return Request(request, parsers) diff --git a/djangorestframework/utils/staticviews.py b/djangorestframework/utils/staticviews.py deleted file mode 100644 index 7cbc0b9b..00000000 --- a/djangorestframework/utils/staticviews.py +++ /dev/null @@ -1,61 +0,0 @@ -from django.contrib.auth.views import * -from django.conf import settings -from django.http import HttpResponse -from django.shortcuts import render_to_response -from django.template import RequestContext -import base64 - - -# BLERGH -# Replicate django.contrib.auth.views.login simply so we don't have get users to update TEMPLATE_CONTEXT_PROCESSORS -# to add ADMIN_MEDIA_PREFIX to the RequestContext. I don't like this but really really want users to not have to -# be making settings changes in order to accomodate django-rest-framework -@csrf_protect -@never_cache -def api_login(request, template_name='djangorestframework/login.html', - redirect_field_name=REDIRECT_FIELD_NAME, - authentication_form=AuthenticationForm): - """Displays the login form and handles the login action.""" - - redirect_to = request.REQUEST.get(redirect_field_name, '') - - if request.method == "POST": - form = authentication_form(data=request.POST) - if form.is_valid(): - # Light security check -- make sure redirect_to isn't garbage. - if not redirect_to or ' ' in redirect_to: - redirect_to = settings.LOGIN_REDIRECT_URL - - # Heavier security check -- redirects to http://example.com should - # not be allowed, but things like /view/?param=http://example.com - # should be allowed. This regex checks if there is a '//' *before* a - # question mark. - elif '//' in redirect_to and re.match(r'[^\?]*//', redirect_to): - redirect_to = settings.LOGIN_REDIRECT_URL - - # Okay, security checks complete. Log the user in. - auth_login(request, form.get_user()) - - if request.session.test_cookie_worked(): - request.session.delete_test_cookie() - - return HttpResponseRedirect(redirect_to) - - else: - form = authentication_form(request) - - request.session.set_test_cookie() - - #current_site = get_current_site(request) - - return render_to_response(template_name, { - 'form': form, - redirect_field_name: redirect_to, - #'site': current_site, - #'site_name': current_site.name, - 'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX, - }, context_instance=RequestContext(request)) - - -def api_logout(request, next_page=None, template_name='djangorestframework/login.html', redirect_field_name=REDIRECT_FIELD_NAME): - return logout(request, next_page, template_name, redirect_field_name) -- cgit v1.2.3 From a25b4be4418a2a94e38a77b13cc234ca68e8322c Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 3 Sep 2012 13:30:20 +0100 Subject: Support generators --- djangorestframework/utils/encoders.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 djangorestframework/utils/encoders.py (limited to 'djangorestframework/utils') diff --git a/djangorestframework/utils/encoders.py b/djangorestframework/utils/encoders.py new file mode 100644 index 00000000..3cd2e8e1 --- /dev/null +++ b/djangorestframework/utils/encoders.py @@ -0,0 +1,33 @@ +import datetime +import decimal +from django.utils import timezone +from django.utils import simplejson as json + + +class JSONEncoder(json.JSONEncoder): + """ + JSONEncoder subclass that knows how to encode date/time and decimal types. + """ + def default(self, o): + # See "Date Time String Format" in the ECMA-262 specification. + if isinstance(o, datetime.datetime): + r = o.isoformat() + if o.microsecond: + r = r[:23] + r[26:] + if r.endswith('+00:00'): + r = r[:-6] + 'Z' + return r + elif isinstance(o, datetime.date): + return o.isoformat() + elif isinstance(o, datetime.time): + if timezone.is_aware(o): + raise ValueError("JSON can't represent timezone-aware times.") + r = o.isoformat() + if o.microsecond: + r = r[:12] + return r + elif isinstance(o, decimal.Decimal): + return str(o) + elif hasattr(o, '__iter__'): + return [i for i in o] + return super(JSONEncoder, self).default(o) -- cgit v1.2.3 From 7abef9ac3b3fb20a6cdef5d52c640e5725c93437 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 3 Sep 2012 14:28:40 +0100 Subject: Parsers may return raw data, or a DataAndFiles object --- djangorestframework/utils/encoders.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'djangorestframework/utils') diff --git a/djangorestframework/utils/encoders.py b/djangorestframework/utils/encoders.py index 3cd2e8e1..ba7c8553 100644 --- a/djangorestframework/utils/encoders.py +++ b/djangorestframework/utils/encoders.py @@ -1,3 +1,6 @@ +""" +Helper classes for parsers. +""" import datetime import decimal from django.utils import timezone @@ -6,10 +9,12 @@ from django.utils import simplejson as json class JSONEncoder(json.JSONEncoder): """ - JSONEncoder subclass that knows how to encode date/time and decimal types. + JSONEncoder subclass that knows how to encode date/time, + decimal types, and generators. """ def default(self, o): - # See "Date Time String Format" in the ECMA-262 specification. + # For Date Time string spec, see ECMA 262 + # http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15 if isinstance(o, datetime.datetime): r = o.isoformat() if o.microsecond: -- cgit v1.2.3 From a092a72844705e3129b8996b81d8424997b5d37f Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 3 Sep 2012 16:54:17 +0100 Subject: View -> APIView --- djangorestframework/utils/breadcrumbs.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'djangorestframework/utils') diff --git a/djangorestframework/utils/breadcrumbs.py b/djangorestframework/utils/breadcrumbs.py index 85e13a5a..319ce5f9 100644 --- a/djangorestframework/utils/breadcrumbs.py +++ b/djangorestframework/utils/breadcrumbs.py @@ -1,9 +1,10 @@ from django.core.urlresolvers import resolve + def get_breadcrumbs(url): """Given a url returns a list of breadcrumbs, which are each a tuple of (name, url).""" - from djangorestframework.views import View + from djangorestframework.views import APIView def breadcrumbs_recursive(url, breadcrumbs_list): """Add tuples of (name, url) to the breadcrumbs list, progressively chomping off parts of the url.""" @@ -14,7 +15,7 @@ def get_breadcrumbs(url): pass else: # Check if this is a REST framework view, and if so add it to the breadcrumbs - if isinstance(getattr(view, 'cls_instance', None), View): + if isinstance(getattr(view, 'cls_instance', None), APIView): breadcrumbs_list.insert(0, (view.cls_instance.get_name(), url)) if url == '': -- cgit v1.2.3 From 6af75d3a69c486b19adc6e2da00719094778eb31 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 4 Sep 2012 09:29:59 +0100 Subject: Remove some bits from utils --- djangorestframework/utils/__init__.py | 33 -------------------------------- djangorestframework/utils/breadcrumbs.py | 1 - 2 files changed, 34 deletions(-) (limited to 'djangorestframework/utils') diff --git a/djangorestframework/utils/__init__.py b/djangorestframework/utils/__init__.py index ef611a16..bb5bb6d7 100644 --- a/djangorestframework/utils/__init__.py +++ b/djangorestframework/utils/__init__.py @@ -1,6 +1,5 @@ from django.utils.encoding import smart_unicode from django.utils.xmlutils import SimplerXMLGenerator -from django.core.urlresolvers import resolve from djangorestframework.compat import StringIO from djangorestframework.compat import RequestFactory as DjangoRequestFactory @@ -9,38 +8,6 @@ from djangorestframework.request import Request import re import xml.etree.ElementTree as ET -MSIE_USER_AGENT_REGEX = re.compile(r'^Mozilla/[0-9]+\.[0-9]+ \([^)]*; MSIE [0-9]+\.[0-9]+[a-z]?;[^)]*\)(?!.* Opera )') - - -def as_tuple(obj): - """ - Given an object which may be a list/tuple, another object, or None, - return that object in list form. - - IE: - If the object is already a list/tuple just return it. - If the object is not None, return it in a list with a single element. - If the object is None return an empty list. - """ - if obj is None: - return () - elif isinstance(obj, list): - return tuple(obj) - elif isinstance(obj, tuple): - return obj - return (obj,) - - -def url_resolves(url): - """ - Return True if the given URL is mapped to a view in the urlconf, False otherwise. - """ - try: - resolve(url) - except Exception: - return False - return True - # From xml2dict class XML2Dict(object): diff --git a/djangorestframework/utils/breadcrumbs.py b/djangorestframework/utils/breadcrumbs.py index 319ce5f9..84dd3dfa 100644 --- a/djangorestframework/utils/breadcrumbs.py +++ b/djangorestframework/utils/breadcrumbs.py @@ -30,4 +30,3 @@ def get_breadcrumbs(url): return breadcrumbs_recursive(url[:url.rfind('/') + 1], breadcrumbs_list) return breadcrumbs_recursive(url, []) - -- cgit v1.2.3 From 8457c871963264c9f62552f30307e98221a1c25d Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 4 Sep 2012 12:02:05 +0100 Subject: Bits of cleanup --- djangorestframework/utils/mediatypes.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'djangorestframework/utils') diff --git a/djangorestframework/utils/mediatypes.py b/djangorestframework/utils/mediatypes.py index 48dca0f0..5eba7fb2 100644 --- a/djangorestframework/utils/mediatypes.py +++ b/djangorestframework/utils/mediatypes.py @@ -53,7 +53,7 @@ def get_media_type_params(media_type): def order_by_precedence(media_type_lst): """ - Returns a list of lists of media type strings, ordered by precedence. + Returns a list of sets of media type strings, ordered by precedence. Precedence is determined by how specific a media type is: 3. 'type/subtype; param=val' @@ -61,11 +61,11 @@ def order_by_precedence(media_type_lst): 1. 'type/*' 0. '*/*' """ - ret = [[], [], [], []] + ret = [set(), set(), set(), set()] for media_type in media_type_lst: precedence = _MediaType(media_type).precedence - ret[3 - precedence].append(media_type) - return ret + ret[3 - precedence].add(media_type) + return [media_types for media_types in ret if media_types] class _MediaType(object): -- cgit v1.2.3 From 7d9125bcb69950e54bb9c2ca61f59403c1018178 Mon Sep 17 00:00:00 2001 From: Mjumbe Wawatu Poe Date: Fri, 7 Sep 2012 17:05:21 -0400 Subject: Fix Django 1.3 compatibility --- djangorestframework/utils/encoders.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'djangorestframework/utils') diff --git a/djangorestframework/utils/encoders.py b/djangorestframework/utils/encoders.py index ba7c8553..74876017 100644 --- a/djangorestframework/utils/encoders.py +++ b/djangorestframework/utils/encoders.py @@ -3,8 +3,8 @@ Helper classes for parsers. """ import datetime import decimal -from django.utils import timezone from django.utils import simplejson as json +from djangorestframework.compat import timezone class JSONEncoder(json.JSONEncoder): @@ -25,7 +25,7 @@ class JSONEncoder(json.JSONEncoder): elif isinstance(o, datetime.date): return o.isoformat() elif isinstance(o, datetime.time): - if timezone.is_aware(o): + if timezone and timezone.is_aware(o): raise ValueError("JSON can't represent timezone-aware times.") r = o.isoformat() if o.microsecond: -- cgit v1.2.3 From b3e29d9576f1b1b6d12f9abfeb4a06f669b45202 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 14 Sep 2012 22:42:29 +0100 Subject: Moved content negotiation out of response. Nicer exception handling now. --- djangorestframework/utils/__init__.py | 38 ----------------------------------- 1 file changed, 38 deletions(-) (limited to 'djangorestframework/utils') diff --git a/djangorestframework/utils/__init__.py b/djangorestframework/utils/__init__.py index bb5bb6d7..f53ac0b8 100644 --- a/djangorestframework/utils/__init__.py +++ b/djangorestframework/utils/__init__.py @@ -1,9 +1,6 @@ from django.utils.encoding import smart_unicode from django.utils.xmlutils import SimplerXMLGenerator - from djangorestframework.compat import StringIO -from djangorestframework.compat import RequestFactory as DjangoRequestFactory -from djangorestframework.request import Request import re import xml.etree.ElementTree as ET @@ -102,38 +99,3 @@ class XMLRenderer(): def dict2xml(input): return XMLRenderer().dict2xml(input) - - -class RequestFactory(DjangoRequestFactory): - """ - Replicate RequestFactory, but return Request, not HttpRequest. - """ - def get(self, *args, **kwargs): - parsers = kwargs.pop('parsers', None) - request = super(RequestFactory, self).get(*args, **kwargs) - return Request(request, parsers) - - def post(self, *args, **kwargs): - parsers = kwargs.pop('parsers', None) - request = super(RequestFactory, self).post(*args, **kwargs) - return Request(request, parsers) - - def put(self, *args, **kwargs): - parsers = kwargs.pop('parsers', None) - request = super(RequestFactory, self).put(*args, **kwargs) - return Request(request, parsers) - - def delete(self, *args, **kwargs): - parsers = kwargs.pop('parsers', None) - request = super(RequestFactory, self).delete(*args, **kwargs) - return Request(request, parsers) - - def head(self, *args, **kwargs): - parsers = kwargs.pop('parsers', None) - request = super(RequestFactory, self).head(*args, **kwargs) - return Request(request, parsers) - - def options(self, *args, **kwargs): - parsers = kwargs.pop('parsers', None) - request = super(RequestFactory, self).options(*args, **kwargs) - return Request(request, parsers) -- cgit v1.2.3 From 9921b6bd73c5256a3b65c2a5106717ce0fc8f0cf Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 19 Sep 2012 17:14:40 +0100 Subject: Use get_script_prefix to play nicely if not installed at the root. --- djangorestframework/utils/breadcrumbs.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'djangorestframework/utils') diff --git a/djangorestframework/utils/breadcrumbs.py b/djangorestframework/utils/breadcrumbs.py index 84dd3dfa..71284380 100644 --- a/djangorestframework/utils/breadcrumbs.py +++ b/djangorestframework/utils/breadcrumbs.py @@ -1,4 +1,4 @@ -from django.core.urlresolvers import resolve +from django.core.urlresolvers import resolve, get_script_prefix def get_breadcrumbs(url): @@ -6,7 +6,7 @@ def get_breadcrumbs(url): from djangorestframework.views import APIView - def breadcrumbs_recursive(url, breadcrumbs_list): + def breadcrumbs_recursive(url, breadcrumbs_list, prefix): """Add tuples of (name, url) to the breadcrumbs list, progressively chomping off parts of the url.""" try: @@ -16,7 +16,7 @@ def get_breadcrumbs(url): else: # Check if this is a REST framework view, and if so add it to the breadcrumbs if isinstance(getattr(view, 'cls_instance', None), APIView): - breadcrumbs_list.insert(0, (view.cls_instance.get_name(), url)) + breadcrumbs_list.insert(0, (view.cls_instance.get_name(), prefix + url)) if url == '': # All done @@ -24,9 +24,11 @@ def get_breadcrumbs(url): elif url.endswith('/'): # Drop trailing slash off the end and continue to try to resolve more breadcrumbs - return breadcrumbs_recursive(url.rstrip('/'), breadcrumbs_list) + return breadcrumbs_recursive(url.rstrip('/'), breadcrumbs_list, prefix) # Drop trailing non-slash off the end and continue to try to resolve more breadcrumbs - return breadcrumbs_recursive(url[:url.rfind('/') + 1], breadcrumbs_list) + return breadcrumbs_recursive(url[:url.rfind('/') + 1], breadcrumbs_list, prefix) - return breadcrumbs_recursive(url, []) + prefix = get_script_prefix() + url = url[len(prefix):] + return breadcrumbs_recursive(url, [], prefix) -- cgit v1.2.3 From cb8a8e98ed42f5053b9752064aa8c4094ee5f754 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 19 Sep 2012 18:48:59 +0100 Subject: Dont strip final '/' --- djangorestframework/utils/breadcrumbs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'djangorestframework/utils') diff --git a/djangorestframework/utils/breadcrumbs.py b/djangorestframework/utils/breadcrumbs.py index 71284380..ccee0081 100644 --- a/djangorestframework/utils/breadcrumbs.py +++ b/djangorestframework/utils/breadcrumbs.py @@ -29,6 +29,6 @@ def get_breadcrumbs(url): # Drop trailing non-slash off the end and continue to try to resolve more breadcrumbs return breadcrumbs_recursive(url[:url.rfind('/') + 1], breadcrumbs_list, prefix) - prefix = get_script_prefix() + prefix = get_script_prefix().rstrip('/') url = url[len(prefix):] return breadcrumbs_recursive(url, [], prefix) -- cgit v1.2.3 From 87dae4d8549c02fa9a57adb3bb876d249dae1f79 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 20 Sep 2012 13:19:43 +0100 Subject: Remove old 'djangorestframework directories --- djangorestframework/utils/__init__.py | 101 --------------------------- djangorestframework/utils/breadcrumbs.py | 34 ---------- djangorestframework/utils/encoders.py | 38 ----------- djangorestframework/utils/mediatypes.py | 113 ------------------------------- 4 files changed, 286 deletions(-) delete mode 100644 djangorestframework/utils/__init__.py delete mode 100644 djangorestframework/utils/breadcrumbs.py delete mode 100644 djangorestframework/utils/encoders.py delete mode 100644 djangorestframework/utils/mediatypes.py (limited to 'djangorestframework/utils') diff --git a/djangorestframework/utils/__init__.py b/djangorestframework/utils/__init__.py deleted file mode 100644 index f53ac0b8..00000000 --- a/djangorestframework/utils/__init__.py +++ /dev/null @@ -1,101 +0,0 @@ -from django.utils.encoding import smart_unicode -from django.utils.xmlutils import SimplerXMLGenerator -from djangorestframework.compat import StringIO - -import re -import xml.etree.ElementTree as ET - - -# From xml2dict -class XML2Dict(object): - - def __init__(self): - pass - - def _parse_node(self, node): - node_tree = {} - # Save attrs and text, hope there will not be a child with same name - if node.text: - node_tree = node.text - for (k, v) in node.attrib.items(): - k, v = self._namespace_split(k, v) - node_tree[k] = v - #Save childrens - for child in node.getchildren(): - tag, tree = self._namespace_split(child.tag, self._parse_node(child)) - if tag not in node_tree: # the first time, so store it in dict - node_tree[tag] = tree - continue - old = node_tree[tag] - if not isinstance(old, list): - node_tree.pop(tag) - node_tree[tag] = [old] # multi times, so change old dict to a list - node_tree[tag].append(tree) # add the new one - - return node_tree - - def _namespace_split(self, tag, value): - """ - Split the tag '{http://cs.sfsu.edu/csc867/myscheduler}patients' - ns = http://cs.sfsu.edu/csc867/myscheduler - name = patients - """ - result = re.compile("\{(.*)\}(.*)").search(tag) - if result: - value.namespace, tag = result.groups() - return (tag, value) - - def parse(self, file): - """parse a xml file to a dict""" - f = open(file, 'r') - return self.fromstring(f.read()) - - def fromstring(self, s): - """parse a string""" - t = ET.fromstring(s) - unused_root_tag, root_tree = self._namespace_split(t.tag, self._parse_node(t)) - return root_tree - - -def xml2dict(input): - return XML2Dict().fromstring(input) - - -# Piston: -class XMLRenderer(): - def _to_xml(self, xml, data): - if isinstance(data, (list, tuple)): - for item in data: - xml.startElement("list-item", {}) - self._to_xml(xml, item) - xml.endElement("list-item") - - elif isinstance(data, dict): - for key, value in data.iteritems(): - xml.startElement(key, {}) - self._to_xml(xml, value) - xml.endElement(key) - - elif data is None: - # Don't output any value - pass - - else: - xml.characters(smart_unicode(data)) - - def dict2xml(self, data): - stream = StringIO.StringIO() - - xml = SimplerXMLGenerator(stream, "utf-8") - xml.startDocument() - xml.startElement("root", {}) - - self._to_xml(xml, data) - - xml.endElement("root") - xml.endDocument() - return stream.getvalue() - - -def dict2xml(input): - return XMLRenderer().dict2xml(input) diff --git a/djangorestframework/utils/breadcrumbs.py b/djangorestframework/utils/breadcrumbs.py deleted file mode 100644 index ccee0081..00000000 --- a/djangorestframework/utils/breadcrumbs.py +++ /dev/null @@ -1,34 +0,0 @@ -from django.core.urlresolvers import resolve, get_script_prefix - - -def get_breadcrumbs(url): - """Given a url returns a list of breadcrumbs, which are each a tuple of (name, url).""" - - from djangorestframework.views import APIView - - def breadcrumbs_recursive(url, breadcrumbs_list, prefix): - """Add tuples of (name, url) to the breadcrumbs list, progressively chomping off parts of the url.""" - - try: - (view, unused_args, unused_kwargs) = resolve(url) - except Exception: - pass - else: - # Check if this is a REST framework view, and if so add it to the breadcrumbs - if isinstance(getattr(view, 'cls_instance', None), APIView): - breadcrumbs_list.insert(0, (view.cls_instance.get_name(), prefix + url)) - - if url == '': - # All done - return breadcrumbs_list - - elif url.endswith('/'): - # Drop trailing slash off the end and continue to try to resolve more breadcrumbs - return breadcrumbs_recursive(url.rstrip('/'), breadcrumbs_list, prefix) - - # Drop trailing non-slash off the end and continue to try to resolve more breadcrumbs - return breadcrumbs_recursive(url[:url.rfind('/') + 1], breadcrumbs_list, prefix) - - prefix = get_script_prefix().rstrip('/') - url = url[len(prefix):] - return breadcrumbs_recursive(url, [], prefix) diff --git a/djangorestframework/utils/encoders.py b/djangorestframework/utils/encoders.py deleted file mode 100644 index 74876017..00000000 --- a/djangorestframework/utils/encoders.py +++ /dev/null @@ -1,38 +0,0 @@ -""" -Helper classes for parsers. -""" -import datetime -import decimal -from django.utils import simplejson as json -from djangorestframework.compat import timezone - - -class JSONEncoder(json.JSONEncoder): - """ - JSONEncoder subclass that knows how to encode date/time, - decimal types, and generators. - """ - def default(self, o): - # For Date Time string spec, see ECMA 262 - # http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15 - if isinstance(o, datetime.datetime): - r = o.isoformat() - if o.microsecond: - r = r[:23] + r[26:] - if r.endswith('+00:00'): - r = r[:-6] + 'Z' - return r - elif isinstance(o, datetime.date): - return o.isoformat() - elif isinstance(o, datetime.time): - if timezone and timezone.is_aware(o): - raise ValueError("JSON can't represent timezone-aware times.") - r = o.isoformat() - if o.microsecond: - r = r[:12] - return r - elif isinstance(o, decimal.Decimal): - return str(o) - elif hasattr(o, '__iter__'): - return [i for i in o] - return super(JSONEncoder, self).default(o) diff --git a/djangorestframework/utils/mediatypes.py b/djangorestframework/utils/mediatypes.py deleted file mode 100644 index 5eba7fb2..00000000 --- a/djangorestframework/utils/mediatypes.py +++ /dev/null @@ -1,113 +0,0 @@ -""" -Handling of media types, as found in HTTP Content-Type and Accept headers. - -See http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7 -""" - -from django.http.multipartparser import parse_header - - -def media_type_matches(lhs, rhs): - """ - Returns ``True`` if the media type in the first argument <= the - media type in the second argument. The media types are strings - as described by the HTTP spec. - - Valid media type strings include: - - 'application/json; indent=4' - 'application/json' - 'text/*' - '*/*' - """ - lhs = _MediaType(lhs) - rhs = _MediaType(rhs) - return lhs.match(rhs) - - -def is_form_media_type(media_type): - """ - Return True if the media type is a valid form media type as defined by the HTML4 spec. - (NB. HTML5 also adds text/plain to the list of valid form media types, but we don't support this here) - """ - media_type = _MediaType(media_type) - return media_type.full_type == 'application/x-www-form-urlencoded' or \ - media_type.full_type == 'multipart/form-data' - - -def add_media_type_param(media_type, key, val): - """ - Add a key, value parameter to a media type string, and return the new media type string. - """ - media_type = _MediaType(media_type) - media_type.params[key] = val - return str(media_type) - - -def get_media_type_params(media_type): - """ - Return a dictionary of the parameters on the given media type. - """ - return _MediaType(media_type).params - - -def order_by_precedence(media_type_lst): - """ - Returns a list of sets of media type strings, ordered by precedence. - Precedence is determined by how specific a media type is: - - 3. 'type/subtype; param=val' - 2. 'type/subtype' - 1. 'type/*' - 0. '*/*' - """ - ret = [set(), set(), set(), set()] - for media_type in media_type_lst: - precedence = _MediaType(media_type).precedence - ret[3 - precedence].add(media_type) - return [media_types for media_types in ret if media_types] - - -class _MediaType(object): - def __init__(self, media_type_str): - if media_type_str is None: - media_type_str = '' - self.orig = media_type_str - self.full_type, self.params = parse_header(media_type_str) - self.main_type, sep, self.sub_type = self.full_type.partition('/') - - def match(self, other): - """Return true if this MediaType satisfies the given MediaType.""" - for key in self.params.keys(): - if key != 'q' and other.params.get(key, None) != self.params.get(key, None): - return False - - if self.sub_type != '*' and other.sub_type != '*' and other.sub_type != self.sub_type: - return False - - if self.main_type != '*' and other.main_type != '*' and other.main_type != self.main_type: - return False - - return True - - @property - def precedence(self): - """ - Return a precedence level from 0-3 for the media type given how specific it is. - """ - if self.main_type == '*': - return 0 - elif self.sub_type == '*': - return 1 - elif not self.params or self.params.keys() == ['q']: - return 2 - return 3 - - def __str__(self): - return unicode(self).encode('utf-8') - - def __unicode__(self): - ret = "%s/%s" % (self.main_type, self.sub_type) - for key, val in self.params.items(): - ret += "; %s=%s" % (key, val) - return ret -- cgit v1.2.3