From 7efa73697be39bb15934952a20923709ffd23253 Mon Sep 17 00:00:00 2001 From: eric Date: Fri, 18 Dec 2015 20:21:28 -0500 Subject: [PATCH] add freeisbn resource --- api/resources.py | 35 +++++++++++++++++++++++++++++++++-- api/tests.py | 16 ++++++++++++++++ api/urls.py | 1 + api/views.py | 1 + 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/api/resources.py b/api/resources.py index bd47aa8e..5430644f 100755 --- a/api/resources.py +++ b/api/resources.py @@ -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 diff --git a/api/tests.py b/api/tests.py index ebfa473b..cb56ba96 100755 --- a/api/tests.py +++ b/api/tests.py @@ -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) diff --git a/api/urls.py b/api/urls.py index e6a79420..830b8b32 100644 --- a/api/urls.py +++ b/api/urls.py @@ -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"), diff --git a/api/views.py b/api/views.py index f11732e5..cc37618f 100755 --- a/api/views.py +++ b/api/views.py @@ -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") +