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