add freeisbn resource

pull/1/head
eric 2015-12-18 20:21:28 -05:00
parent 174f2f076d
commit 7efa73697b
4 changed files with 51 additions and 2 deletions

View File

@ -12,6 +12,7 @@ from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from regluit.core import models
import regluit.core.isbn
logger = logging.getLogger(__name__)
@ -121,8 +122,38 @@ class SubjectResource(ModelResource):
queryset = models.Subject.objects.all()
resource_name = 'subject'
class FreeResource(ModelResource):
def alter_list_data_to_serialize(self, request, data):
del data["meta"]["limit"]
del data["meta"]["offset"]
return data
def dehydrate(self, bundle):
bundle.data["filetype"]=bundle.obj.format
bundle.data["rights"]=bundle.obj.rights
bundle.data["provider"]=bundle.obj.provider
bundle.data["href"]=reverse('download_ebook',kwargs={'ebook_id':bundle.obj.id})
return bundle
def obj_get_list(self, request=None, **kwargs):
isbn =""
if hasattr(request, 'GET'):
isbn = request.GET.get("isbn","")
isbn = isbn.replace('-','')
if len(isbn)==10:
isbn=regluit.core.isbn.convert_10_to_13(isbn)
try:
work=models.Identifier.objects.get(type='isbn',value=isbn,).work
base_object_list = models.Ebook.objects.filter(edition__work=work)
return self.apply_authorization_limits(request, base_object_list)
except ValueError:
raise BadRequest("Invalid resource lookup data provided (mismatched type).")
except models.Identifier.DoesNotExist:
return self.apply_authorization_limits(request, models.Ebook.objects.none())
class Meta:
authentication = ApiKeyAuthentication()
queryset = models.Wishlist.objects.all()
resource_name = 'wishlist'
fields = [ 'provider', 'rights' ]
limit = 0
include_resource_uri = False

View File

@ -35,6 +35,13 @@ class ApiTests(TestCase):
)
self.user = User.objects.create_user('test', 'test@example.org', 'testpass')
self.client = Client()
ebook = models.Ebook.objects.create(
url="http://example.com/ebook",
provider="github",
rights='CC BY',
format='epub',
edition=edition,
)
def test_user(self):
self.assertEqual(User.objects.all().count(), 1)
@ -120,6 +127,15 @@ class ApiTests(TestCase):
self.assertEqual(j['meta']['logged_in_username'], 'test')
self.assertEqual(j['objects'][0]['in_wishlist'], True)
r = self.client.get('/api/v1/free/', data={
'format': 'json',
'isbn': '0441007465',
'username': self.user.username,
'api_key': self.user.api_key.key
})
j = json.loads(r.content)
self.assertEqual(j['objects'][0]['filetype'], 'epub')
def test_widget(self):
r = self.client.get('/api/widget/0441007465/')
self.assertEqual(r.status_code, 200)

View File

@ -16,6 +16,7 @@ v1_api.register(resources.EditionResource())
v1_api.register(resources.CampaignResource())
v1_api.register(resources.AuthorResource())
v1_api.register(resources.SubjectResource())
v1_api.register(resources.FreeResource())
urlpatterns = patterns('',
url(r'^help$', ApiHelpView.as_view(), name="api_help"),

View File

@ -156,3 +156,4 @@ class OnixView(View):
facet_class = opds.get_facet_class(facet)()
return HttpResponse(onix.onix_feed(facet_class, max),
content_type="text/xml")