mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 09:36:49 +03:00
Start adding example app
This commit is contained in:
parent
f144b769fe
commit
95ac2396d6
|
@ -284,6 +284,7 @@ class Resource(object):
|
|||
# Serialize the response content
|
||||
ret = self.cleanup_response(ret)
|
||||
content = emitter(self, request, status, headers, form).emit(ret)
|
||||
print content
|
||||
|
||||
# Build the HTTP Response
|
||||
resp = HttpResponse(content, mimetype=mimetype, status=status)
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
from django.db import models
|
||||
from django.template.defaultfilters import slugify
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
def uuid_str():
|
||||
|
@ -28,4 +30,34 @@ class ExampleItem(models.Model):
|
|||
container = models.ForeignKey(ExampleContainer, related_name='items')
|
||||
index = models.IntegerField()
|
||||
note = models.CharField(max_length=1024)
|
||||
unique_together = (container, index)
|
||||
unique_together = (container, index)
|
||||
|
||||
|
||||
class BlogPost(models.Model):
|
||||
slug = models.SlugField(editable=False, primary_key=True, default='blah')
|
||||
title = models.CharField(max_length=128)
|
||||
content = models.TextField()
|
||||
when = models.DateTimeField(editable=False)
|
||||
|
||||
@models.permalink
|
||||
def get_absolute_url(self):
|
||||
return ('testapp.views.BlogPostInstance', (self.slug,))
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.slug = slugify(self.title)
|
||||
self.when = datetime.now()
|
||||
super(self.__class__, self).save(*args, **kwargs)
|
||||
|
||||
|
||||
class Comment(models.Model):
|
||||
blogpost = models.ForeignKey(BlogPost, related_name='comments')
|
||||
name = models.CharField(max_length=128)
|
||||
content = models.TextField()
|
||||
when = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
@models.permalink
|
||||
def get_absolute_url(self):
|
||||
return ('testapp.views.CommentInstance', (self.blogpost.slug, self.id))
|
||||
|
||||
def save(self):
|
||||
self.index = self.blogpost.comments.count()
|
|
@ -143,4 +143,4 @@ class CreatedModelTests(TestCase):
|
|||
self.assertEquals(resp.status_code, 200)
|
||||
container = json.loads(resp.content)
|
||||
self.assertEquals(container, self.container)
|
||||
|
||||
|
||||
|
|
|
@ -8,4 +8,7 @@ urlpatterns = patterns('testapp.views',
|
|||
(r'^model$', 'ModelFormResource'),
|
||||
(r'^container$', 'ContainerFactory'),
|
||||
(r'^container/((?P<key>[^/]+))$', 'ContainerInstance'),
|
||||
|
||||
(r'^blogpost/create$', 'BlogPostCreator'),
|
||||
(r'^blogposts/(?P<slug>[^/]+)', 'BlogPostInstance'),
|
||||
)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
from rest.resource import Resource, ModelResource
|
||||
from testapp.forms import ExampleForm
|
||||
from testapp.models import ExampleModel, ExampleContainer
|
||||
from testapp.models import ExampleModel, ExampleContainer, BlogPost, Comment
|
||||
|
||||
class RootResource(Resource):
|
||||
"""This is my docstring
|
||||
|
@ -12,7 +12,8 @@ class RootResource(Resource):
|
|||
'write-only-api': self.reverse(WriteOnlyResource),
|
||||
'read-write-api': self.reverse(ReadWriteResource),
|
||||
'model-api': self.reverse(ModelFormResource),
|
||||
'create-container': self.reverse(ContainerFactory)}, {})
|
||||
'create-container': self.reverse(ContainerFactory),
|
||||
'blog-post-creator': self.reverse(BlogPostCreator)}, {})
|
||||
|
||||
|
||||
class ReadOnlyResource(Resource):
|
||||
|
@ -61,3 +62,17 @@ class ContainerInstance(ModelResource):
|
|||
fields = ('absolute_uri', 'name', 'key')
|
||||
form_fields = ('name',)
|
||||
|
||||
#######################
|
||||
|
||||
|
||||
class BlogPostCreator(ModelResource):
|
||||
"""A Resource with which blog posts may be created.
|
||||
This is distinct from blog post instance so that it is discoverable by the client.
|
||||
(ie the client doens't need to know how to form a blog post url in order to create a blog post)"""
|
||||
allowed_operations = ('create',)
|
||||
model = BlogPost
|
||||
|
||||
class BlogPostInstance(ModelResource):
|
||||
"""Represents a single Blog Post."""
|
||||
allowed_operations = ('read', 'update', 'delete')
|
||||
model = BlogPost
|
Loading…
Reference in New Issue
Block a user