From 7736201ce803ecefcffcf42ce7034c54c1588f01 Mon Sep 17 00:00:00 2001 From: eric Date: Thu, 29 Jun 2017 13:22:58 -0400 Subject: [PATCH] add publishers, ebooks to api --- api/resources.py | 16 ++++++++++++++++ api/urls.py | 2 ++ 2 files changed, 18 insertions(+) diff --git a/api/resources.py b/api/resources.py index adcc69e8..9b8a59fb 100755 --- a/api/resources.py +++ b/api/resources.py @@ -21,6 +21,7 @@ logger = logging.getLogger(__name__) class EditionResource(ModelResource): work = fields.ForeignKey('regluit.api.resources.WorkResource', 'work') identifiers = fields.ToManyField('regluit.api.resources.IdentifierResource', 'identifiers') + ebooks = fields.ToManyField('regluit.api.resources.EbookResource', 'ebooks') class Meta: authentication = ApiKeyAuthentication() queryset = models.Edition.objects.all() @@ -123,6 +124,21 @@ class SubjectResource(ModelResource): queryset = models.Subject.objects.all() resource_name = 'subject' +class EbookResource(ModelResource): + edition = fields.ToOneField(EditionResource, 'edition') + class Meta: + authentication = ApiKeyAuthentication() + queryset = models.Ebook.objects.all() + resource_name = 'ebook' + excludes = ['url'] + +class PublisherResource(ModelResource): + ebooks = fields.ToManyField(EbookResource, attribute=lambda bundle: models.Ebook.objects.filter(edition__publisher_name=bundle.obj.name)) + class Meta: + authentication = ApiKeyAuthentication() + queryset = models.Publisher.objects.all() + resource_name = 'publisher' + class FreeResource(ModelResource): def alter_list_data_to_serialize(self, request, data): del data["meta"]["limit"] diff --git a/api/urls.py b/api/urls.py index bd534ca6..4322651e 100644 --- a/api/urls.py +++ b/api/urls.py @@ -25,6 +25,8 @@ v1_api.register(resources.CampaignResource()) v1_api.register(resources.AuthorResource()) v1_api.register(resources.SubjectResource()) v1_api.register(resources.FreeResource()) +v1_api.register(resources.PublisherResource()) +v1_api.register(resources.EbookResource()) urlpatterns = [ url(r'^help$', ApiHelpView.as_view(), name="api_help"),