Commits

Jay Yu  committed f6ccb74

piston API for JLOG

  • Participants
  • Parent commits 68fc462

Comments (0)

Files changed (6)

File testproject/api/__init__.py

Empty file added.

File testproject/api/handlers.py

+from piston.handler import BaseHandler
+from jlog.models import Entry
+
+class EntryHandler(BaseHandler):
+    allowed_methods = ('GET',)
+    model = Entry
+    fields = ('title', 'slug', 'content', 'published_date', 'published_date',
+        ('author', ('username', 'first_name', 'last_name', ))
+    )
+
+    def read(self, request, entry_id=None):
+        """
+        Returns a single post if `entry_id` is given,
+        otherwise a subset.
+
+        """
+        base = Entry.objects
+
+        if entry_id:
+            return base.get(pk=entry_id)
+        else:
+            return base.all() # Or base.filter(...)

File testproject/api/models.py

+from django.db import models
+
+# Create your models here.

File testproject/api/tests.py

+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        self.assertEqual(1 + 1, 2)

File testproject/api/urls.py

+from django.conf.urls.defaults import *
+
+from piston.resource import Resource
+
+from handlers import EntryHandler
+
+entry_h = Resource(EntryHandler)
+
+urlpatterns = patterns('',
+   url(r'^entry/(?P<entry_id>[^/]+)/', entry_h, { 'emitter_format': 'json' }),
+   url(r'^entry/', entry_h, { 'emitter_format': 'json' }),
+)

File testproject/api/views.py

+# Create your views here.