First naive pass at creating a IsbnResource

pull/1/head
Raymond Yee 2011-09-14 16:15:01 -07:00
parent 93eb9a1549
commit 05753ad81b
2 changed files with 103 additions and 2 deletions

View File

@ -1,9 +1,16 @@
from django.contrib.auth.models import User
from django.db.models import Q
from regluit.core import models
from tastypie.resources import ModelResource
from tastypie.resources import ModelResource, Resource, Bundle
from tastypie import fields
import logging
# Get an instance of a specific named logger
logger = logging.getLogger(__name__)
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
@ -51,4 +58,96 @@ class WishlistResource(ModelResource):
works = fields.ToManyField(WorkResource, 'works')
class Meta:
queryset = models.Wishlist.objects.all()
resource_name = 'wishlist'
resource_name = 'wishlist'
# http://django-tastypie.readthedocs.org/en/latest/non_orm_data_sources.html
def get_all_isbn():
# need to deal with isbn-13 too
return [e.isbn_10 for e in models.Edition.objects.all() if e.isbn_10 is not None]
class IsbnObject(object):
def __init__(self, initial=None):
self.__dict__['_data'] = {}
if hasattr(initial, 'items'):
self.__dict__['_data'] = initial
def __getattr__(self, name):
return self._data.get(name, None)
def __setattr__(self, name, value):
self.__dict__['_data'][name] = value
def to_dict(self):
return self._data
class IsbnResource(Resource):
isbn_10 = fields.CharField(attribute='isbn_10')
title = fields.CharField(attribute='title')
edition_id = fields.CharField(attribute='edition_id')
class Meta:
resource_name = 'isbn'
object_class = IsbnObject
# The following methods will need overriding regardless of your
# data source.
def get_resource_uri(self, bundle_or_obj):
"""
Handles generating a resource URI for a single resource.
Uses the model's ``pk`` in order to create the URI.
"""
kwargs = {
'resource_name': self._meta.resource_name,
}
if isinstance(bundle_or_obj, Bundle):
kwargs['pk'] = bundle_or_obj.obj.isbn_10
else:
kwargs['pk'] = bundle_or_obj.isbn_10
if self._meta.api_name is not None:
kwargs['api_name'] = self._meta.api_name
return self._build_reverse_url("api_dispatch_detail", kwargs=kwargs)
def get_object_list(self, request):
"""
A hook to allow making returning the list of available objects.
This needs to be implemented at the user level.
``ModelResource`` includes a full working version specific to Django's
``Models``.
"""
results = []
isbns = get_all_isbn()
logger.info("isbn_10: %s" % (isbns))
for isbn in isbns:
editions = models.Edition.objects.filter(isbn_10=isbn)
new_obj = IsbnObject()
new_obj.isbn_10 = isbn
new_obj.title = editions[0].title
new_obj.edition_id = editions[0].id
results.append(new_obj)
logger.info("results: %s" % ([r.editions for r in results]))
return results
def obj_get_list(self, request=None, **kwargs):
# Filtering disabled for brevity...
return self.get_object_list(request)
def obj_get(self, request=None, **kwargs):
isbn = kwargs['pk']
editions = models.Edition.objects.filter(isbn_10=isbn)
new_obj = IsbnObject()
new_obj.isbn_10 = isbn
new_obj.title = editions[0].title
new_obj.edition_id = editions[0].id
return new_obj

View File

@ -2,6 +2,7 @@ from django.conf.urls.defaults import *
from tastypie.api import Api
from regluit.api.models import UserResource, WorkResource, EditionResource, CampaignResource, AuthorResource, SubjectResource, EditionCoverResource, WishlistResource
from regluit.api.models import IsbnResource
v1_api = Api(api_name='v1') # how set api_name to None so that we don't get URIs like /api/v1 but more like /api/ Don't think there is a way...
v1_api.register(UserResource())
@ -12,6 +13,7 @@ v1_api.register(CampaignResource())
v1_api.register(AuthorResource())
v1_api.register(SubjectResource())
v1_api.register(WishlistResource())
v1_api.register(IsbnResource())
urlpatterns = patterns('',
url(r'^editions/$', 'regluit.api.views.editions', name="editions"),