Commits

Łukasz Langa committed 368b1e4

books support

  • Participants
  • Parent commits 3a67da0

Comments (0)

Files changed (8)

src/soa_library/books/__init__.py

Empty file added.

src/soa_library/books/admin.py

+from django.contrib import admin
+
+from soa_library.books.models import Book, Author
+
+class BookAdmin(admin.ModelAdmin):
+    list_display = ['name']
+
+admin.site.register(Book, BookAdmin)
+
+class AuthorAdmin(admin.ModelAdmin):
+    list_display = ['last_name', 'first_name']
+    list_filter = ['last_name']
+    search_fields = ['last_name', 'first_name']
+
+admin.site.register(Author, AuthorAdmin)

src/soa_library/books/api.py

+from tastypie import fields
+from tastypie.authentication import ApiKeyAuthentication
+from tastypie.constants import ALL, ALL_WITH_RELATIONS
+from tastypie.resources import ModelResource
+
+from soa_library.books.models import Author, Book
+
+class AuthorResource(ModelResource):
+    class Meta:
+        queryset = Author.objects.all()
+        authentication = ApiKeyAuthentication()
+        filtering = {
+            'id': ALL,
+            'first_name': ALL,
+            'last_name': ALL,
+        }
+
+class BookResource(ModelResource):
+    authors = fields.ToManyField(AuthorResource, 'authors', null=True, full=True)
+
+    class Meta:
+        queryset = Book.objects.all()
+        authentication = ApiKeyAuthentication()
+        filtering = {
+            'id': ALL,
+            'name': ALL,
+            'authors': ALL_WITH_RELATIONS,
+        }

src/soa_library/books/models.py

+from django.db import models as db
+
+
+class Author(db.Model):
+    first_name = db.CharField(max_length=100)
+    last_name = db.CharField(max_length=100)
+
+    class Meta:
+        ordering = ['last_name', 'first_name']
+
+    def __unicode__(self):
+        return "{} {}".format(self.first_name, self.last_name)
+
+class Book(db.Model):
+    name = db.CharField(max_length=100)
+    authors = db.ManyToManyField(Author)
+
+    class Meta:
+        ordering = ['name']
+
+    def __unicode__(self):
+        return self.name

src/soa_library/books/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)

src/soa_library/books/views.py

+# Create your views here.
+
+from django.http import HttpResponse
+
+def dummy_view(request):
+    return HttpResponse("Hello.",
+                        mimetype="text/plain")

src/soa_library/settings.py

     'lck.django.common',
     'gunicorn',
     'tastypie',
+    'soa_library.books',
 )
 TEMPLATE_CONTEXT_PROCESSORS = (
     'django.contrib.auth.context_processors.auth',

src/soa_library/urls.py

 from django.conf.urls.defaults import patterns, include, url
+from django.contrib import admin
+from tastypie.api import Api
 
-# Uncomment the next two lines to enable the admin:
-# from django.contrib import admin
-# admin.autodiscover()
+from soa_library.books.api import AuthorResource, BookResource
+
+admin.autodiscover()
+
+v01_api = Api(api_name='v0.1')
+v01_api.register(AuthorResource())
+v01_api.register(BookResource())
 
 urlpatterns = patterns('',
     # Examples:
     # Uncomment the admin/doc line below to enable admin documentation:
     # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
 
-    # Uncomment the next line to enable the admin:
-    # url(r'^admin/', include(admin.site.urls)),
+    url(r'^admin/', include(admin.site.urls)),
+    url(r'^api/v0.1/book/schema', 'soa_library.books.views.dummy_view'),
+    url(r'^api/', include(v01_api.urls)),
 )