Anonymous avatar Anonymous committed 2fe1384

Added resourceexample, moved simpleexample to modelresourceexample

Comments (0)

Files changed (14)

docs/templates/layout.html

 
 {% block htmltitle %}<title>{% if pagename == 'index' %}Django REST framework{% else %}{{ titleprefix }}{{ title|striptags|e }}{% endif %}</title>{% endblock %}
 
+{% block extrahead %}
+{{ super() }}
+<script type="text/javascript">
 
+  var _gaq = _gaq || [];
+  _gaq.push(['_setAccount', 'UA-18852272-2']);
+  _gaq.push(['_trackPageview']);
+
+  (function() {
+    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+  })();
+
+</script>
+{% endblock %}
Add a comment to this file

examples/modelresourceexample/__init__.py

Empty file added.

examples/modelresourceexample/models.py

+from django.db import models
+
+MAX_INSTANCES = 10
+
+class MyModel(models.Model):
+    foo = models.BooleanField()
+    bar = models.IntegerField(help_text='Must be an integer.')
+    baz = models.CharField(max_length=32, help_text='Free text.  Max length 32 chars.')
+    created = models.DateTimeField(auto_now_add=True)
+    
+    class Meta:
+        ordering = ('created',)
+
+    def save(self, *args, **kwargs):
+        """For the purposes of the sandbox, limit the maximum number of stored models."""
+        super(MyModel, self).save(*args, **kwargs)
+        while MyModel.objects.all().count() > MAX_INSTANCES:
+            MyModel.objects.all()[0].delete()
+    
+    @models.permalink
+    def get_absolute_url(self):
+        return ('modelresourceexample.views.MyModelResource', (self.pk,))
+

examples/modelresourceexample/urls.py

+from django.conf.urls.defaults import patterns, url
+
+urlpatterns = patterns('modelresourceexample.views',
+    url(r'^$',          'MyModelRootResource'),
+    url(r'^([0-9]+)/$', 'MyModelResource'),
+)

examples/modelresourceexample/views.py

+from djangorestframework.modelresource import ModelResource, RootModelResource
+from modelresourceexample.models import MyModel
+
+FIELDS = ('foo', 'bar', 'baz', 'absolute_url')
+
+class MyModelRootResource(RootModelResource):
+    """A create/list resource for MyModel.
+    Available for both authenticated and anonymous access for the purposes of the sandbox."""
+    model = MyModel
+    allowed_methods = anon_allowed_methods = ('GET', 'POST')
+    fields = FIELDS
+
+class MyModelResource(ModelResource):
+    """A read/update/delete resource for MyModel.
+    Available for both authenticated and anonymous access for the purposes of the sandbox."""
+    model = MyModel
+    allowed_methods = anon_allowed_methods = ('GET', 'PUT', 'DELETE')
+    fields = FIELDS
Add a comment to this file

examples/resourceexample/__init__.py

Empty file added.

examples/resourceexample/urls.py

+from django.conf.urls.defaults import patterns, url
+
+urlpatterns = patterns('resourceexample.views',
+    url(r'^$',          'ExampleResource'),
+    url(r'^(?P<num>[0-9]+)/$', 'AnotherExampleResource'),
+)

examples/resourceexample/views.py

+from django import forms
+from djangorestframework.resource import Resource
+from djangorestframework.response import Response, status
+
+
+class MyForm(forms.Form):
+    foo = forms.BooleanField()
+    bar = forms.IntegerField(help_text='Must be an integer.')
+    baz = forms.CharField(max_length=32, help_text='Free text.  Max length 32 chars.')
+
+
+class ExampleResource(Resource):
+    """A basic read only resource that points to 3 other resources."""
+    allowed_methods = anon_allowed_methods = ('GET',)
+
+    def get(self, request, auth):
+        return {"Some other resources": [self.reverse(AnotherExampleResource, num=num) for num in range(3)]}
+
+
+class AnotherExampleResource(Resource):
+    """A basic GET-able/POST-able resource."""
+    allowed_methods = anon_allowed_methods = ('GET', 'POST')
+    form = MyForm   # Optional form validation on input
+
+    def get(self, request, auth, num):
+        """Handle GET requests"""
+        if int(num) > 2:
+            return Response(status.HTTP_404_NOT_FOUND)
+        return "GET request to AnotherExampleResource %s" % num
+    
+    def post(self, request, auth, content, num):
+        """Handle POST requests"""
+        if int(num) > 2:
+            return Response(status.HTTP_404_NOT_FOUND)
+        return "POST request to AnotherExampleResource %s, with content: %s" % (num, repr(content))

examples/settings.py

     'django.contrib.messages',
     #'django.contrib.admin',
     'djangorestframework',
-    'simpleexample',
+
+    'resourceexample',
+    'modelresourceexample',
     'objectstore',
     'pygments_api',
     'blogpost',
Add a comment to this file

examples/simpleexample/__init__.py

Empty file removed.

examples/simpleexample/models.py

-from django.db import models
-
-MAX_INSTANCES = 10
-
-class MyModel(models.Model):
-    foo = models.BooleanField()
-    bar = models.IntegerField(help_text='Must be an integer.')
-    baz = models.CharField(max_length=32, help_text='Free text.  Max length 32 chars.')
-    created = models.DateTimeField(auto_now_add=True)
-    
-    class Meta:
-        ordering = ('created',)
-
-    def save(self, *args, **kwargs):
-        """For the purposes of the sandbox, limit the maximum number of stored models."""
-        super(MyModel, self).save(*args, **kwargs)
-        while MyModel.objects.all().count() > MAX_INSTANCES:
-            MyModel.objects.all()[0].delete()
-    
-    @models.permalink
-    def get_absolute_url(self):
-        return ('simpleexample.views.MyModelResource', (self.pk,))
-

examples/simpleexample/urls.py

-from django.conf.urls.defaults import patterns, url
-
-urlpatterns = patterns('simpleexample.views',
-    url(r'^$',          'MyModelRootResource'),
-    url(r'^([0-9]+)/$', 'MyModelResource'),
-)

examples/simpleexample/views.py

-from djangorestframework.modelresource import ModelResource, RootModelResource
-from simpleexample.models import MyModel
-
-FIELDS = ('foo', 'bar', 'baz', 'absolute_url')
-
-class MyModelRootResource(RootModelResource):
-    """A create/list resource for MyModel.
-    Available for both authenticated and anonymous access for the purposes of the sandbox."""
-    model = MyModel
-    allowed_methods = anon_allowed_methods = ('GET', 'POST')
-    fields = FIELDS
-
-class MyModelResource(ModelResource):
-    """A read/update/delete resource for MyModel.
-    Available for both authenticated and anonymous access for the purposes of the sandbox."""
-    model = MyModel
-    allowed_methods = anon_allowed_methods = ('GET', 'PUT', 'DELETE')
-    fields = FIELDS
 #admin.autodiscover()
 
 class RootResource(Resource):
+    """This is the sandbox for the examples provided with django-rest-framework.
+
+    These examples are here to help you get a better idea of the some of the
+    features of django-rest-framework API, such as automatic form and model validation,
+    support for multiple input and output media types, etc...
+
+    Please feel free to browse, create, edit and delete the resources here, either
+    in the browser, from the command line, or programmatically."""
     allowed_methods = anon_allowed_methods = ('GET',)
 
     def get(self, request, auth):
-        return {'simple example': self.reverse('simpleexample.views.MyModelRootResource'),
-                'pygments example': self.reverse('pygments_api.views.PygmentsRoot'),
-                'object store example': self.reverse('objectstore.views.ObjectStoreRoot'),
-                'blog post example': self.reverse('blogpost.views.BlogPostRoot'),}
+        return {'Simple Resource example': self.reverse('resourceexample.views.ExampleResource'),
+                'Simple ModelResource example': self.reverse('modelresourceexample.views.MyModelRootResource'),
+                'Object store API (Resource)': self.reverse('objectstore.views.ObjectStoreRoot'),
+                'A pygments pastebin API (Resource + forms)': self.reverse('pygments_api.views.PygmentsRoot'),
+                'Blog posts API (ModelResource)': self.reverse('blogpost.views.BlogPostRoot'),}
 
 
 urlpatterns = patterns('',
     (r'^$', RootResource),
-    (r'^simple-example/', include('simpleexample.urls')),
+    (r'^model-resource-example/', include('modelresourceexample.urls')),
+    (r'^resource-example/', include('resourceexample.urls')),
     (r'^object-store/', include('objectstore.urls')),
     (r'^pygments/', include('pygments_api.urls')),
     (r'^blog-post/', include('blogpost.urls')),
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.