Children book formats are added to ZIP archive for shelf.
[wolnelektury.git] / apps / catalogue / views.py
index 257d472..b73552e 100644 (file)
@@ -79,7 +79,7 @@ def tagged_object_list(request, tags=''):
     related_tags = models.Tag.objects.related_for_model(tags, model, counts=True, extra={'where': [extra_where]})
     categories = split_tags(related_tags)
 
-    if not theme_is_set:
+    if not (theme_is_set or shelf_is_set):
         model=models.Book.objects.filter(parent=None)
     
     return newtagging_views.tagged_object_list(
@@ -214,7 +214,18 @@ def download_shelf(request, slug):
     # Create a ZIP archive
     temp = temp = tempfile.TemporaryFile()
     archive = zipfile.ZipFile(temp, 'w')
-    for book in models.Book.tagged.with_all(shelf):
+    
+    # Collect all books to include in ZIP archive
+    def collect_books(books):
+        result = []
+        for book in books:
+            if len(book.children.all()) == 0:
+                result.append(book)
+            else:
+                result += collect_books(book.children.all())
+        return result
+    
+    for book in collect_books(models.Book.tagged.with_all(shelf)):
         if book.pdf_file:
             filename = book.pdf_file.path
             archive.write(filename, str('%s.pdf' % book.slug))