Commits

Jesper Nøhr  committed 3e61606

adding initial data fixture and simple api

  • Participants
  • Parent commits a90c5bf

Comments (0)

Files changed (10)

File examples/blogserver/api/__init__.py

Empty file added.

File examples/blogserver/api/handlers.py

+from piston.handler import BaseHandler, AnonymousBaseHandler
+
+from blogserver.blog.models import Blogpost
+
+class AnonymousBlogpostHandler(AnonymousBaseHandler):
+    model = Blogpost
+    fields = ('title', 'content', 'created_on')
+
+class BlogpostHandler(BaseHandler):
+    model = Blogpost
+    anonymous = AnonymousBlogpostHandler
+    fields = ('title', 'content', ('author', ('username',)), 
+              'created_on', 'content_length')
+    
+    def content_length(self, blogpost):
+        return len(blogpost.content)

File examples/blogserver/api/urls.py

+from django.conf.urls.defaults import *
+from piston.resource import Resource
+from piston.authentication import HttpBasicAuthentication
+
+from blogserver.api.handlers import BlogpostHandler
+
+auth = HttpBasicAuthentication(realm='My sample API')
+
+blogposts = Resource(handler=BlogpostHandler, authentication=auth)
+
+urlpatterns = patterns('',
+    url(r'^posts/$', blogposts),
+)

File examples/blogserver/blog/models.py

 from django.db import models
 from django.contrib.auth.models import User
+from django.contrib import admin
 
 class Blogpost(models.Model):
     title = models.CharField(max_length=255)
     def __unicode__(self):
         return self.title
 
-    
+admin.site.register(Blogpost)

File examples/blogserver/blog/urls.py

+from django.conf.urls.defaults import *
+
+urlpatterns = patterns('blogserver.blog.views',
+    url(r'^$', 'posts', name='posts'),
+)

File examples/blogserver/blog/views.py

-# Create your views here.
+from django.http import HttpResponse, HttpResponseRedirect
+from django.contrib.auth.decorators import login_required
+from django.shortcuts import render_to_response, get_object_or_404
+from django.template import RequestContext
+
+from blogserver.blog.models import Blogpost
+
+def posts(request):
+    posts = Blogpost.objects.all()
+
+    return render_to_response("posts.html", {
+        'posts': posts },
+        RequestContext(request))

File examples/blogserver/fixtures/initial_data.xml

+<?xml version="1.0" encoding="utf-8"?>
+<django-objects version="1.0">
+	<object pk="1" model="auth.user">
+		<field type="CharField" name="username">testuser</field>
+		<field type="CharField" name="first_name"/>
+		<field type="CharField" name="last_name"/>
+		<field type="CharField" name="email"/>
+		<field type="CharField" name="password">sha1$97a04$0537e86889530ffd5c46a756fffbe0f745e4938e</field> <!-- foobar -->
+		<field type="BooleanField" name="is_staff">False</field>
+		<field type="BooleanField" name="is_active">True</field>
+		<field type="BooleanField" name="is_superuser">False</field>
+		<field type="DateTimeField" name="last_login">2009-04-27 04:55:09</field>
+		<field type="DateTimeField" name="date_joined">2009-04-27 04:55:09</field>
+		<field to="auth.group" name="groups" rel="ManyToManyRel"/>
+		<field to="auth.permission" name="user_permissions" rel="ManyToManyRel"/>
+	</object>
+	<object pk="1" model="sites.site">
+		<field type="CharField" name="domain">example.com</field>
+		<field type="CharField" name="name">example.com</field>
+	</object>
+	<object pk="1" model="blog.blogpost">
+		<field type="CharField" name="title">Sample blogpost 1</field>
+		<field type="TextField" name="content">This is just a sample post.</field>
+		<field to="auth.user" name="author" rel="ManyToOneRel">1</field>
+		<field type="DateTimeField" name="created_on">2009-04-27 04:55:23</field>
+	</object>
+	<object pk="2" model="blog.blogpost">
+		<field type="CharField" name="title">Another sample post</field>
+		<field type="TextField" name="content">This is yet another sample post.</field>
+		<field to="auth.user" name="author" rel="ManyToOneRel">1</field>
+		<field type="DateTimeField" name="created_on">2009-04-27 04:55:33</field>
+	</object>
+</django-objects>

File examples/blogserver/settings.py

-import os
+import os, sys
 
 DEBUG = True
 TEMPLATE_DEBUG = DEBUG
 
 BASE_DIR = os.path.dirname(os.path.abspath(__file__))
 
+# Fix up piston imports here. We would normally place piston in 
+# a directory accessible via the Django app, but this is an
+# example and we ship it a couple of directories up.
+sys.path.insert(0, os.path.join(BASE_DIR, '../../'))
+
 DATABASE_ENGINE = 'sqlite3'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
 DATABASE_NAME = os.path.join(BASE_DIR, 'db')             # Or path to database file if using sqlite3.
 #DATABASE_USER = ''             # Not used with sqlite3.
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.sites',
+    'django.contrib.admin',
     'blogserver.blog',
+    'blogserver.api',
 )
+
+FIXTURE_DIRS = (
+    os.path.join(BASE_DIR, 'fixtures'),
+)

File examples/blogserver/templates/posts.html

+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+
+<head>
+
+<title>Blogposts</title>
+
+</head>
+
+<body>
+
+	<h1>Posts</h1>
+	
+	{% for post in posts %}
+	
+		<h3>{{ post.title }}</h3>
+	
+		<div>
+			
+			{{ post.content}}
+			
+		</div>
+		
+		<div>
+			Written on {{ post.created_on }} by {{ post.author.username }}.
+		</div>
+	
+	{% endfor %}
+
+</body>
+
+</html>

File examples/blogserver/urls.py

 from django.conf.urls.defaults import *
+from django.contrib import admin
 
-# Uncomment the next two lines to enable the admin:
-# from django.contrib import admin
-# admin.autodiscover()
+admin.autodiscover()
 
 urlpatterns = patterns('',
-    # Example:
-    # (r'^blogserver/', include('blogserver.foo.urls')),
-
-    # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
-    # to INSTALLED_APPS to enable admin documentation:
-    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
-
-    # Uncomment the next line to enable the admin:
-    # (r'^admin/(.*)', admin.site.root),
+    (r'^', include('blogserver.blog.urls')),
+    (r'^api/', include('blogserver.api.urls')),
+    (r'^admin/(.*)', admin.site.root),
 )