Merge Marko's doc improvements.

This commit is contained in:
Tom Christie 2011-05-17 09:15:35 +01:00
commit bfbb8ceccf
16 changed files with 97 additions and 79 deletions

View File

@ -1,10 +1,10 @@
""" """
The ``authentication`` module provides a set of pluggable authentication classes. The :mod:`authentication` module provides a set of pluggable authentication classes.
Authentication behavior is provided by adding the ``AuthMixin`` class to a ``View`` . Authentication behavior is provided by mixing the :class:`mixins.AuthMixin` class into a :class:`View` class.
The set of authentication methods which are used is then specified by setting the The set of authentication methods which are used is then specified by setting the
``authentication`` attribute on the ``View`` class, and listing a set of authentication classes. :attr:`authentication` attribute on the :class:`View` class, and listing a set of authentication classes.
""" """
from django.contrib.auth import authenticate from django.contrib.auth import authenticate
@ -26,24 +26,23 @@ class BaseAuthenticaton(object):
def __init__(self, view): def __init__(self, view):
""" """
Authentication classes are always passed the current view on creation. :param view: :class:`Authentication` classes are always passed the current view on creation.
""" """
self.view = view self.view = view
def authenticate(self, request): def authenticate(self, request):
""" """
Authenticate the request and return a ``User`` instance or None. (*) :param request: Request to be authenticated
:rtype: :obj:`User` or None [*]_
This function must be overridden to be implemented.
.. [*] The authentication context *will* typically be a :obj:`User`,
(*) The authentication context _will_ typically be a ``User`` object, but it need not be. It can be any user-like object so long as the
but it need not be. It can be any user-like object so long as the permissions classes on the view can handle the object and use
permissions classes on the view can handle the object and use it to determine if the request has the required permissions or not.
it to determine if the request has the required permissions or not.
This can be an important distinction if you're implementing some token
This can be an important distinction if you're implementing some token based authentication mechanism, where the authentication context
based authentication mechanism, where the authentication context may be more involved than simply mapping to a :obj:`User`.
may be more involved than simply mapping to a ``User``.
""" """
return None return None
@ -54,6 +53,10 @@ class BasicAuthenticaton(BaseAuthenticaton):
""" """
def authenticate(self, request): def authenticate(self, request):
"""
Returns a :obj:`User` if a correct username and password have been supplied
using HTTP Basic authentication. Otherwise returns `None`.
"""
from django.utils.encoding import smart_unicode, DjangoUnicodeDecodeError from django.utils.encoding import smart_unicode, DjangoUnicodeDecodeError
if 'HTTP_AUTHORIZATION' in request.META: if 'HTTP_AUTHORIZATION' in request.META:
@ -81,6 +84,9 @@ class UserLoggedInAuthenticaton(BaseAuthenticaton):
""" """
def authenticate(self, request): def authenticate(self, request):
"""
Returns a :obj:`User` if the request session currently has a logged in user, otherwise `None`.
"""
# TODO: Switch this back to request.POST, and let FormParser/MultiPartParser deal with the consequences. # TODO: Switch this back to request.POST, and let FormParser/MultiPartParser deal with the consequences.
if getattr(request, 'user', None) and request.user.is_active: if getattr(request, 'user', None) and request.user.is_active:
# If this is a POST request we enforce CSRF validation. # If this is a POST request we enforce CSRF validation.

View File

@ -408,28 +408,6 @@ class AuthMixin(object):
permission.check_permission(user) permission.check_permission(user)
##########
class InstanceMixin(object):
"""
Mixin class that is used to identify a view class as being the canonical identifier
for the resources it is mapped too.
"""
@classmethod
def as_view(cls, **initkwargs):
"""
Store the callable object on the resource class that has been associated with this view.
"""
view = super(InstanceMixin, cls).as_view(**initkwargs)
if 'resource' in initkwargs:
# We do a little dance when we store the view callable...
# we need to store it wrapped in a 1-tuple, so that inspect will treat it
# as a function when we later look it up (rather than turning it into a method).
# This makes sure our URL reversing works ok.
initkwargs['resource'].view_callable = (view,)
return view
########## Resource Mixin ########## ########## Resource Mixin ##########
class ResourceMixin(object): class ResourceMixin(object):
@ -449,6 +427,9 @@ class ResourceMixin(object):
@property @property
def CONTENT(self): def CONTENT(self):
"""
Returns the cleaned, validated request content.
"""
if not hasattr(self, '_content'): if not hasattr(self, '_content'):
self._content = self.validate_request(self.DATA, self.FILES) self._content = self.validate_request(self.DATA, self.FILES)
return self._content return self._content
@ -474,6 +455,28 @@ class ResourceMixin(object):
##########
class InstanceMixin(object):
"""
Mixin class that is used to identify a view class as being the canonical identifier
for the resources it is mapped too.
"""
@classmethod
def as_view(cls, **initkwargs):
"""
Store the callable object on the resource class that has been associated with this view.
"""
view = super(InstanceMixin, cls).as_view(**initkwargs)
if 'resource' in initkwargs:
# We do a little dance when we store the view callable...
# we need to store it wrapped in a 1-tuple, so that inspect will treat it
# as a function when we later look it up (rather than turning it into a method).
# This makes sure our URL reversing works ok.
initkwargs['resource'].view_callable = (view,)
return view
########## Model Mixins ########## ########## Model Mixins ##########

View File

@ -11,7 +11,7 @@ __all__ = (
'BaseView', 'BaseView',
'ModelView', 'ModelView',
'InstanceModelView', 'InstanceModelView',
'ListOrModelView', 'ListModelView',
'ListOrCreateModelView' 'ListOrCreateModelView'
) )
@ -131,7 +131,3 @@ class ListModelView(ListModelMixin, ModelView):
class ListOrCreateModelView(ListModelMixin, CreateModelMixin, ModelView): class ListOrCreateModelView(ListModelMixin, CreateModelMixin, ModelView):
"""A view which provides default operations for list and create, against a model in the database.""" """A view which provides default operations for list and create, against a model in the database."""
pass pass

View File

@ -60,6 +60,7 @@ version = '0.1'
release = '0.1' release = '0.1'
autodoc_member_order='bysource' autodoc_member_order='bysource'
# The language for content autogenerated by Sphinx. Refer to documentation # The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages. # for a list of supported languages.
#language = None #language = None

View File

@ -27,7 +27,7 @@ Creating the resources
Once we have some existing models there's very little we need to do to create the corresponding resources. We simply create a base resource and an instance resource for each model we're working with. Once we have some existing models there's very little we need to do to create the corresponding resources. We simply create a base resource and an instance resource for each model we're working with.
django-rest-framework will provide the default operations on the resources all the usual input validation that Django's models can give us for free. django-rest-framework will provide the default operations on the resources all the usual input validation that Django's models can give us for free.
``views.py`` #``views.py``
.. include:: ../../examples/blogpost/views.py #.. include:: ../../examples/blogpost/views.py
:literal: # :literal:

View File

@ -18,7 +18,7 @@ Features:
* Automatically provides an awesome Django admin style `browse-able self-documenting API <http://api.django-rest-framework.org>`_. * Automatically provides an awesome Django admin style `browse-able self-documenting API <http://api.django-rest-framework.org>`_.
* Clean, simple, views for Resources, using Django's new `class based views <http://docs.djangoproject.com/en/dev/topics/class-based-views/>`_. * Clean, simple, views for Resources, using Django's new `class based views <http://docs.djangoproject.com/en/dev/topics/class-based-views/>`_.
* Support for ModelResources with out-of-the-box default implementations and input validation. * Support for ModelResources with out-of-the-box default implementations and input validation.
* Pluggable :mod:`.emitters`, :mod:`parsers`, :mod:`validators` and :mod:`authenticators` - Easy to customise. * Pluggable :mod:`.parsers`, :mod:`renderers`, :mod:`authentication` and :mod:`permissions` - Easy to customise.
* Content type negotiation using HTTP Accept headers. * Content type negotiation using HTTP Accept headers.
* Optional support for forms as input validation. * Optional support for forms as input validation.
* Modular architecture - MixIn classes can be used without requiring the :class:`.Resource` or :class:`.ModelResource` classes. * Modular architecture - MixIn classes can be used without requiring the :class:`.Resource` or :class:`.ModelResource` classes.
@ -36,7 +36,8 @@ Resources
Any and all questions, thoughts, bug reports and contributions are *hugely appreciated*. Any and all questions, thoughts, bug reports and contributions are *hugely appreciated*.
We'd like for this to be a real community driven effort, so come say hi, get involved, and get forking! (See: `Bitbucket <http://confluence.atlassian.com/display/BITBUCKET/Forking+a+Bitbucket+Repository>`_, `GitHub <http://help.github.com/fork-a-repo/>`_) We'd like for this to be a real community driven effort, so come say hi, get involved, and get forking! (See: `Forking a Bitbucket Repository
<http://confluence.atlassian.com/display/BITBUCKET/Forking+a+Bitbucket+Repository>`_, or `Fork A GitHub Repo <http://help.github.com/fork-a-repo/>`_)
Requirements Requirements
------------ ------------
@ -139,14 +140,16 @@ Library Reference
.. toctree:: .. toctree::
:maxdepth: 1 :maxdepth: 1
library/resource library/authentication
library/modelresource library/compat
library/emitters library/mixins
library/parsers library/parsers
library/authenticators library/permissions
library/validators library/renderers
library/resource
library/response library/response
library/status library/status
library/views
Examples Reference Examples Reference
------------------ ------------------

View File

@ -0,0 +1,5 @@
:mod:`authentication`
=====================
.. automodule:: authentication
:members:

View File

@ -1,5 +0,0 @@
:mod:`authenticators`
=====================
.. automodule:: authenticators
:members:

5
docs/library/compat.rst Normal file
View File

@ -0,0 +1,5 @@
:mod:`compat`
=====================
.. automodule:: compat
:members:

View File

@ -1,7 +0,0 @@
:mod:`emitters`
===============
The emitters module provides a set of emitters that can be plugged in to a :class:`.Resource`. An emitter is responsible for taking the output of a and serializing it to a given media type. A :class:`.Resource` can have a number of emitters, allow the same content to be serialized in a number of different formats depending on the requesting client's preferences, as specified in the HTTP Request's Accept header.
.. automodule:: emitters
:members:

5
docs/library/mixins.rst Normal file
View File

@ -0,0 +1,5 @@
:mod:`mixins`
=====================
.. automodule:: mixins
:members:

View File

@ -1,9 +0,0 @@
:mod:`modelresource`
====================
.. note::
TODO - document this module properly
.. automodule:: modelresource
:members:

View File

@ -0,0 +1,5 @@
:mod:`permissions`
=====================
.. automodule:: permissions
:members:

View File

@ -0,0 +1,10 @@
:mod:`renderers`
================
The renderers module provides a set of renderers that can be plugged in to a :class:`.Resource`.
A renderer is responsible for taking the output of a View and serializing it to a given media type.
A :class:`.Resource` can have a number of renderers, allow the same content to be serialized in a number
of different formats depending on the requesting client's preferences, as specified in the HTTP Request's Accept header.
.. automodule:: renderers
:members:

View File

@ -1,5 +0,0 @@
:mod:`validators`
=================
.. automodule:: validators
:members:

5
docs/library/views.rst Normal file
View File

@ -0,0 +1,5 @@
:mod:`views`
=====================
.. automodule:: views
:members: