From 81f4a0e5709e2bf818244a07cbb715a54c96f3eb Mon Sep 17 00:00:00 2001 From: Marcin Koziej Date: Tue, 20 Dec 2011 15:13:53 +0100 Subject: [PATCH] Picture create api --- apps/api/handlers.py | 18 ++++++++++++++++++ apps/api/urls.py | 6 +++++- apps/picture/models.py | 13 +++++++------ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/apps/api/handlers.py b/apps/api/handlers.py index 62708c953..270a4fdf6 100644 --- a/apps/api/handlers.py +++ b/apps/api/handlers.py @@ -580,3 +580,21 @@ class ChangesHandler(CatalogueHandler): @piwik_track def read(self, request, since): return self.changes(request, since) + + +class PictureHandler(BaseHandler): + model = Picture + fields = ('slug', 'title') + allowed_methods = ('POST',) + + def create(self, request): + if not request.user.has_perm('catalogue.add_book'): + return rc.FORBIDDEN + + data = json.loads(request.POST.get('data')) + form = BookImportForm(data) + if form.is_valid(): + form.save() + return rc.CREATED + else: + return rc.NOT_FOUND diff --git a/apps/api/urls.py b/apps/api/urls.py index b6fc7604e..9bad68a12 100644 --- a/apps/api/urls.py +++ b/apps/api/urls.py @@ -5,7 +5,7 @@ from piston.resource import Resource from api import handlers from catalogue.models import Book - +from picture.models import Picture auth = OAuthAuthentication(realm="Wolne Lektury") @@ -23,6 +23,7 @@ tag_resource = Resource(handler=handlers.TagDetailHandler) fragment_resource = Resource(handler=handlers.FragmentDetailHandler) fragment_list_resource = Resource(handler=handlers.FragmentsHandler) +picture_resource = Resource(handler=handlers.PictureHandler) urlpatterns = patterns( 'piston.authentication', @@ -63,4 +64,7 @@ urlpatterns = patterns( # tags by category url(r'^(?P[a-z0-9-]+)/$', tag_list_resource), + + # picture by slug + url(r'^pictures/$', picture_resource) ) diff --git a/apps/picture/models.py b/apps/picture/models.py index 01f9b8d48..6ac54fe82 100644 --- a/apps/picture/models.py +++ b/apps/picture/models.py @@ -63,7 +63,7 @@ class Picture(models.Model): return self.slug @classmethod - def from_xml_file(cls, xml_file, images_path=None, overwrite=False): + def from_xml_file(cls, xml_file, image_file=None, overwrite=False): """ Import xml and it's accompanying image file. """ @@ -86,11 +86,12 @@ class Picture(models.Model): picture.tags = catalogue.models.Tag.tags_from_info(picture_xml.picture_info) - img = picture_xml.image_file() - try: - picture.image_file.save(path.basename(picture_xml.image_path), File(img)) - finally: - img.close() + if image_file is not None: + img = image_file + else: + img = picture_xml.image_file() + + picture.image_file.save(path.basename(picture_xml.image_path), File(img)) picture.xml_file.save("%s.xml" % picture.slug, File(xml_file)) picture.save() -- 2.20.1