Skip to content

Commit

Permalink
Sphinx docs, examples, lots of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tom christie tom@tomchristie.com committed Jan 23, 2011
1 parent 9979903 commit 4100242
Show file tree
Hide file tree
Showing 36 changed files with 2,240 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .hgignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ syntax: glob
*.pyc
*.db
env
cache
docs-build
html
.project
.pydevproject
Expand Down
5 changes: 4 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

import sys, os

sys.path.append(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'src'))
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), 'flywheel'))
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), 'examples'))
import settings
from django.core.management import setup_environ
setup_environ(settings)
Expand Down Expand Up @@ -57,6 +59,7 @@
# The full version, including alpha/beta/rc tags.
release = '0.1'

autodoc_member_order='bysource'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None
Expand Down
5 changes: 5 additions & 0 deletions docs/emitters.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Emitters
========

.. automodule:: emitters
:members:
14 changes: 14 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ FlyWheel Documentation

This is the online documentation for FlyWheel - A REST framework for Django.

* Clean, simple, class-based views for Resources.
* Easy input validation using Forms and ModelForms.
* Self describing APIs, with HTML and Plain Text outputs.

.. toctree::
:maxdepth: 1

resource
modelresource
parsers
emitters
response


Indices and tables
------------------

Expand Down
5 changes: 5 additions & 0 deletions docs/modelresource.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ModelResource
=============

.. automodule:: modelresource
:members:
5 changes: 5 additions & 0 deletions docs/parsers.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Parsers
=======

.. automodule:: parsers
:members:
125 changes: 125 additions & 0 deletions docs/resource.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
:mod:`resource`
===============

The :mod:`resource` module is the core of FlyWheel. It provides the :class:`Resource` base class which handles incoming HTTP requests and maps them to method calls, performing authentication, input deserialization, input validation, output serialization.

Resources are created by sublassing :class:`Resource`, setting a number of class attributes, and overriding one or more methods.

:class:`Resource` class attributes
----------------------------------

The following class attributes determine the behavior of the Resource and are intended to be overridden.

.. attribute:: Resource.allowed_methods

A list of the HTTP methods that the Resource supports.
HTTP requests to the resource that do not map to an allowed operation will result in a 405 Method Not Allowed response.

Default: ``('GET',)``

.. attribute:: Resource.anon_allowed_methods

A list of the HTTP methods that the Resource supports for unauthenticated users.
Unauthenticated HTTP requests to the resource that do not map to an allowed operation will result in a 405 Method Not Allowed response.

Default: ``()``

.. attribute:: Resource.emitters

Lists the set of emitters that the Resource supports. This determines which media types the resource can serialize it's output to. Clients can specify which media types they accept using standard HTTP content negotiation via the Accept header. (See `RFC 2616 - Sec 14.1 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>`_) Clients can also override this standard content negotiation by specifying a `_format` ...

The :mod:`emitters` module provides the :class:`BaseEmitter` class and a set of default emitters, including emitters for JSON and XML, as well as emitters for HTML and Plain Text which provide for a self documenting API.

The ordering of the Emitters is important as it determines an order of preference.

Default: ``(emitters.JSONEmitter, emitters.DocumentingHTMLEmitter, emitters.DocumentingXHTMLEmitter, emitters.DocumentingPlainTextEmitter, emitters.XMLEmitter)``

.. attribute:: Resource.parsers

Lists the set of parsers that the Resource supports. This determines which media types the resource can accept as input for incoming HTTP requests. (Typically PUT and POST requests).

The ordering of the Parsers may be considered informative of preference but is not used ...

Default: ``(parsers.JSONParser, parsers.XMLParser, parsers.FormParser)``

.. attribute:: Resource.form

If not None, this attribute should be a Django form which will be used to validate any request data.
This attribute is typically only used for POST or PUT requests to the resource.

Deafult: ``None``

.. attribute:: Resource.callmap

Maps HTTP methods to function calls on the :class:`Resource`. It may be overridden in order to add support for other HTTP methods such as HEAD, OPTIONS and PATCH, or in order to map methods to different function names, for example to use a more `CRUD <http://en.wikipedia.org/wiki/Create,_read,_update_and_delete>`_ like style.

Default: ``{ 'GET': 'get', 'POST': 'post', 'PUT': 'put', 'DELETE': 'delete' }``


:class:`Resource` methods
-------------------------

.. method:: Resource.get
.. method:: Resource.post
.. method:: Resource.put
.. method:: Resource.delete
.. method:: Resource.authenticate
.. method:: Resource.reverse

:class:`Resource` properties
----------------------------

.. method:: Resource.name
.. method:: Resource.description
.. method:: Resource.default_emitter
.. method:: Resource.default_parser
.. method:: Resource.emitted_media_types
.. method:: Resource.parsed_media_types

:class:`Resource` reserved parameters
-------------------------------------

.. attribute:: Resource.ACCEPT_QUERY_PARAM

If set, allows the default `Accept:` header content negotiation to be bypassed by setting the requested media type in a query parameter on the URL. This can be useful if it is necessary to be able to hyperlink to a given format on the Resource using standard HTML.

Set to None to disable, or to another string value to use another name for the reserved URL query parameter.

Default: ``_accept``

.. attribute:: Resource.METHOD_PARAM

If set, allows for PUT and DELETE requests to be tunneled on form POST operations, by setting a (typically hidden) form field with the method name. This allows standard HTML forms to perform method requests which would otherwise `not be supported <http://dev.w3.org/html5/spec/Overview.html#attr-fs-method>`_

Set to None to disable, or to another string value to use another name for the reserved form field.

Default: ``_method``

.. attribute:: Resource.CONTENTTYPE_PARAM

Used together with :attr:`CONTENT_PARAM`.

If set, allows for arbitrary content types to be tunneled on form POST operations, by setting a form field with the content type. This allows standard HTML forms to perform requests with content types other those `supported by default <http://dev.w3.org/html5/spec/Overview.html#attr-fs-enctype>`_ (ie. `application/x-www-form-urlencoded`, `multipart/form-data`, and `text-plain`)

Set to None to disable, or to another string value to use another name for the reserved form field.

Default: ``_contenttype``

.. attribute:: Resource.CONTENT_PARAM

Used together with :attr:`CONTENTTYPE_PARAM`.

Set to None to disable, or to another string value to use another name for the reserved form field.

Default: ``_content``

.. attribute:: Resource.CSRF_PARAM

The name used in Django's (typically hidden) form field for `CSRF Protection <http://docs.djangoproject.com/en/dev/ref/contrib/csrf/>`_.

Setting to None does not disable Django's CSRF middleware, but it does mean that the field name will not be treated as reserved by FlyWheel, so for example the default :class:`FormParser` will return fields with this as part of the request content, rather than ignoring them.

Default:: ``csrfmiddlewaretoken``

reserved params
internal methods
5 changes: 5 additions & 0 deletions docs/response.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Response
========

.. automodule:: response
:members:
Empty file added examples/__init__.py
Empty file.
Empty file added examples/blogpost/__init__.py
Empty file.
68 changes: 68 additions & 0 deletions examples/blogpost/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from django.db import models
from django.template.defaultfilters import slugify
import uuid

def uuid_str():
return str(uuid.uuid1())


RATING_CHOICES = ((0, 'Awful'),
(1, 'Poor'),
(2, 'OK'),
(3, 'Good'),
(4, 'Excellent'))

class BlogPost(models.Model):
key = models.CharField(primary_key=True, max_length=64, default=uuid_str, editable=False)
title = models.CharField(max_length=128)
content = models.TextField()
created = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(editable=False, default='')

class Meta:
ordering = ('created',)

@models.permalink
def get_absolute_url(self):
return ('blogpost.views.BlogPostInstance', (), {'key': self.key})

@property
@models.permalink
def comments_url(self):
"""Link to a resource which lists all comments for this blog post."""
return ('blogpost.views.CommentList', (), {'blogpost_id': self.key})

@property
@models.permalink
def comment_url(self):
"""Link to a resource which can create a comment for this blog post."""
return ('blogpost.views.CommentCreator', (), {'blogpost_id': self.key})

def __unicode__(self):
return self.title

def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(self.__class__, self).save(*args, **kwargs)


class Comment(models.Model):
blogpost = models.ForeignKey(BlogPost, editable=False, related_name='comments')
username = models.CharField(max_length=128)
comment = models.TextField()
rating = models.IntegerField(blank=True, null=True, choices=RATING_CHOICES, help_text='How did you rate this post?')
created = models.DateTimeField(auto_now_add=True)

class Meta:
ordering = ('created',)

@models.permalink
def get_absolute_url(self):
return ('blogpost.views.CommentInstance', (), {'blogpost': self.blogpost.key, 'id': self.id})

@property
@models.permalink
def blogpost_url(self):
"""Link to the blog post resource which this comment corresponds to."""
return ('blogpost.views.BlogPostInstance', (), {'key': self.blogpost.key})

Loading

0 comments on commit 4100242

Please sign in to comment.