Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
95ac2396
Commit
95ac2396
authored
Jan 07, 2011
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Start adding example app
parent
f144b769
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
57 additions
and
6 deletions
+57
-6
src/rest/resource.py
+1
-0
src/testapp/models.py
+34
-2
src/testapp/tests.py
+1
-2
src/testapp/urls.py
+3
-0
src/testapp/views.py
+18
-2
No files found.
src/rest/resource.py
View file @
95ac2396
...
...
@@ -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
)
...
...
src/testapp/models.py
View file @
95ac2396
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
)
\ No newline at end of file
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
()
\ No newline at end of file
src/testapp/tests.py
View file @
95ac2396
...
...
@@ -143,4 +143,4 @@ class CreatedModelTests(TestCase):
self
.
assertEquals
(
resp
.
status_code
,
200
)
container
=
json
.
loads
(
resp
.
content
)
self
.
assertEquals
(
container
,
self
.
container
)
\ No newline at end of file
src/testapp/urls.py
View file @
95ac2396
...
...
@@ -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'
),
)
src/testapp/views.py
View file @
95ac2396
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
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment