Permalink
Browse files

adding a test app

  • Loading branch information...
1 parent 281eaf4 commit 000afe101250240646f13661c4d5184d72ac7ee0 @aehlke committed Mar 2, 2011
View
No changes.
No changes.
@@ -0,0 +1,4 @@
+from django.contrib import admin
+from models import Deck
+
+admin.site.register(Deck)
View
@@ -0,0 +1,15 @@
+from django.db import models
+from django.contrib.auth.models import User
+
+
+class Deck(models.Model):
+ name = models.CharField(max_length=100)
+ description = models.TextField(max_length=2000, blank=True)
+ owner = models.ForeignKey(User)
+
+ created_at = models.DateTimeField(auto_now_add=True, editable=False)
+ modified_at = models.DateTimeField(auto_now=True, editable=False)
+
+
+
+
@@ -0,0 +1,29 @@
+from catnap.restresources import RestResource, RestModelResource
+from django.core.urlresolvers import reverse
+
+
+
+class UserResource(RestModelResource):
+ fields = ('username', 'first_name', 'last_name', 'is_staff',
+ 'is_superuser', 'is_active', 'date_joined',)
+
+ def get_data(self):
+ data = super(UserResource, self).get_data()
+
+
+
+class DeckResource(RestModelResource):
+ fields = ('name', 'description', 'owner', 'created_at', 'modified_at',)
+
+ def get_url(self):
+ return reverse('api-deck', args=[self.obj.id])
+
+ def get_data(self):
+ data = super(DeckResource, self).get_data()
+ data['owner'] = UserResource(self.obj.owner).get_data()
+
+
+
+
+
+
View
@@ -0,0 +1,30 @@
+"""
+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
+from django.core.urlresolvers import reverse
+
+
+USERNAME = 'alex'
+PASSWORD = 'f'
+
+class RestResourcesIntegrationsTest(TestCase):
+ fixtures = ['testdata.json']
+
+ def setUp(self):
+ self.client.login(username=USERNAME, password=PASSWORD)
+
+ def test_entry_point(self):
+ res = self.client.get(reverse('api-entry_point'))
+ self.assertEqual(res.status_code, 200)
+
+ def test_deck_list(self):
+ res = self.client.get(reverse('api-deck_list'))
+ self.assertEqual(res.status_code, 200)
+
+
+
View
@@ -0,0 +1,71 @@
+from django.contrib.auth.decorators import login_required
+from django.db import transaction
+from django.forms import forms
+from django.forms.models import modelformset_factory, formset_factory
+from django.http import Http404, HttpResponseRedirect, HttpResponse
+from django.shortcuts import get_object_or_404, render_to_response
+from django.template import RequestContext, loader
+from django.core.urlresolvers import reverse
+from django.utils.decorators import method_decorator
+from catnap.restviews import (JsonResponseMixin, AutoContentTypeMixin,
+ RestView, ListView, DetailView)
+from restresources import UserResource, DeckResource
+import models
+
+
+class MyRestView(JsonResponseMixin, AutoContentTypeMixin, RestView):
+ '''
+ Our JSON-formatted response base class.
+ '''
+ content_type_template_string = 'application/vnd.catnap-test.{0}+json'
+
+ #@method_decorator(login_required)
+ def dispatch(self, *args, **kwargs):
+ return super(MyRestView, self).dispatch(*args, **kwargs)
+
+
+
+# Resource views
+
+class EntryPoint(MyRestView):
+ '''
+ Entry-point to our REST API.
+
+ This view's URL is the only one that clients should need to know,
+ and the only one that should be documented in the API!
+ '''
+ def get(self, request):
+ '''
+ List the available top-level resource URLs.
+ '''
+ context = {
+ 'decks_url': reverse('api-deck_list'),
+ #'users': reverse('rest-users'),
+ }
+ return self.render_to_response(context)
+
+
+class DeckList(ListView, MyRestView):
+ '''
+ List of decks.
+ '''
+ content_subtype = 'DeckList'
+ resource = DeckResource
+
+ def get_queryset(self):
+ return models.Deck.objects.order_by('name')
+
+
+class Deck(DetailView, MyRestView):
+ '''
+ Detail view of a single deck.
+ '''
+ content_subtype = 'Deck'
+
+ def get_object(self):
+ return get_object_or_404(pk=self.kwargs.get('pk'))
+ #deck.url = reverse('api-deck', args=[deck.id])
+ #return deck
+
+
+
Oops, something went wrong.

0 comments on commit 000afe1

Please sign in to comment.