From 01d6a0899e4435417f774196b08e0613bd0a51f7 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 7 Sep 2012 09:36:52 +0100 Subject: [PATCH] Bits of cleanup --- djangorestframework/generics.py | 5 +++-- djangorestframework/mixins.py | 11 +++++++++++ djangorestframework/settings.py | 12 +++++++----- djangorestframework/urlpatterns.py | 8 ++++++-- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/djangorestframework/generics.py b/djangorestframework/generics.py index 87a2a4b89..5815b73a4 100644 --- a/djangorestframework/generics.py +++ b/djangorestframework/generics.py @@ -41,8 +41,9 @@ class SingleObjectBaseView(SingleObjectMixin, BaseView): """ Override default to add support for object-level permissions. """ - super(self, SingleObjectBaseView).get_object() - self.check_permissions(self.request, self.object) + obj = super(SingleObjectBaseView, self).get_object() + self.check_permissions(self.request, obj) + return obj ### Concrete view classes that provide method handlers ### diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 2f78876f0..4b833aac0 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -1,3 +1,12 @@ +""" +Basic building blocks for generic class based views. + +We don't bind behaviour to http method handlers yet, +which allows mixin classes to be composed in interesting ways. + +Eg. Use mixins to build a Resource class, and have a Router class + perform the binding of http methods to actions for us. +""" from djangorestframework import status from djangorestframework.response import Response @@ -68,6 +77,8 @@ class MetadataMixin(object): """ Return a dicitonary of view metadata. Should be mixed in with any `BaseView`. + + This mixin is typically used for the HTTP 'OPTIONS' method. """ def metadata(self, request, *args, **kwargs): content = { diff --git a/djangorestframework/settings.py b/djangorestframework/settings.py index 6e6ab3c26..e0b6a5d5e 100644 --- a/djangorestframework/settings.py +++ b/djangorestframework/settings.py @@ -13,10 +13,12 @@ API_SETTINGS = { ) } +This module provides the `api_setting` object, that is used to access +REST framework settings, checking for user settings first, then falling +back to the defaults. """ from django.conf import settings from django.utils import importlib -from djangorestframework.compat import yaml DEFAULTS = { @@ -35,17 +37,16 @@ DEFAULTS = { ), 'DEFAULT_PERMISSIONS': (), 'DEFAULT_THROTTLES': (), + 'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser', 'UNAUTHENTICATED_TOKEN': None, + 'FORM_METHOD_OVERRIDE': '_method', 'FORM_CONTENT_OVERRIDE': '_content', 'FORM_CONTENTTYPE_OVERRIDE': '_content_type', 'URL_ACCEPT_OVERRIDE': '_accept' } -if yaml: - DEFAULTS['DEFAULT_RENDERERS'] += ('djangorestframework.renderers.YAMLRenderer', ) - # List of settings that may be in string import notation. IMPORT_STRINGS = ( @@ -97,7 +98,8 @@ class APISettings(object): from djangorestframework.settings import api_settings print api_settings.DEFAULT_RENDERERS - Any setting with string import paths will be resolved. + Any setting with string import paths will be automatically resolved + and return the class, rather than the string literal. """ def __getattr__(self, attr): if attr not in DEFAULTS.keys(): diff --git a/djangorestframework/urlpatterns.py b/djangorestframework/urlpatterns.py index d34acf9b2..6cef721eb 100644 --- a/djangorestframework/urlpatterns.py +++ b/djangorestframework/urlpatterns.py @@ -1,15 +1,19 @@ from django.conf.urls.defaults import url +from djangorestframework.settings import api_settings -def format_suffix_patterns(urlpatterns, suffix_required=False): +def format_suffix_patterns(urlpatterns, suffix_required=False, suffix_kwarg=None): """ Supplement existing urlpatterns with corrosponding patterns that also include a '.format' suffix. Retains urlpattern ordering. """ + suffix_kwarg = suffix_kwarg or api_settings.FORMAT_SUFFIX_KWARG + suffix_pattern = '.(?P<%s>[a-z]+)$' % suffix_kwarg + ret = [] for urlpattern in urlpatterns: # Form our complementing '.format' urlpattern - regex = urlpattern.regex.pattern.rstrip('$') + '.(?P[a-z]+)$' + regex = urlpattern.regex.pattern.rstrip('$') + suffix_pattern view = urlpattern._callback or urlpattern._callback_str kwargs = urlpattern.default_args name = urlpattern.name