add method for splitting a book
[redakcja.git] / apps / catalogue / ebook_utils.py
1 # -*- coding: utf-8 -*-
2 from StringIO import StringIO
3 from catalogue.models import Book
4 from librarian import DocProvider
5 from django.http import HttpResponse
6
7
8 class RedakcjaDocProvider(DocProvider):
9     """Used for getting books' children."""
10
11     def __init__(self, publishable):
12         self.publishable = publishable
13
14     def by_slug(self, slug):
15         return StringIO(Book.objects.get(dc_slug=slug
16                     ).materialize(publishable=self.publishable))
17
18
19 def serve_file(file_path, name, mime_type):
20     def read_chunks(f, size=8192):
21         chunk = f.read(size)
22         while chunk:
23             yield chunk
24             chunk = f.read(size)
25
26     response = HttpResponse(mimetype=mime_type)
27     response['Content-Disposition'] = 'attachment; filename=%s' % name
28     with open(file_path) as f:
29         for chunk in read_chunks(f):
30             response.write(chunk)
31     return response