Epub+mo support
[wolnelektury.git] / src / catalogue / models / book.py
index 9e0f7c8..7d923ea 100644 (file)
@@ -139,6 +139,8 @@ class Book(models.Model):
 
     SORT_KEY_SEP = '$'
 
+    is_book = True
+
     class AlreadyExists(Exception):
         pass
 
@@ -226,6 +228,18 @@ class Book(models.Model):
     def isbn_mobi(self):
         return self.get_extra_info_json().get('isbn_mobi')
 
+    def is_accessible_to(self, user):
+        if not self.preview:
+            return True
+        if not user.is_authenticated:
+            return False
+        Membership = apps.get_model('club', 'Membership')
+        if Membership.is_active_for(user):
+            return True
+        Funding = apps.get_model('funding', 'Funding')
+        if Funding.objects.filter(user=user, offer__book=self):
+            return True
+        return False
 
     def save(self, force_insert=False, force_update=False, **kwargs):
         from sortify import sortify
@@ -284,23 +298,24 @@ class Book(models.Model):
         
         return self.parent.get_prev_text()
 
-    def get_next_text(self):
-        child = self.children.order_by('parent_number').first()
-        if child is not None:
-            return child.get_first_text()
+    def get_next_text(self, inside=True):
+        if inside:
+            child = self.children.order_by('parent_number').first()
+            if child is not None:
+                return child.get_first_text()
 
         if not self.parent:
             return None
         sibling = self.parent.children.filter(parent_number__gt=self.parent_number).order_by('parent_number').first()
         if sibling is not None:
             return sibling.get_first_text()
-        return self.parent.get_next_text()
+        return self.parent.get_next_text(inside=False)
 
     def get_child_audiobook(self):
         BookMedia = apps.get_model('catalogue', 'BookMedia')
         if not BookMedia.objects.filter(book__ancestor=self).exists():
             return None
-        for child in self.children.all():
+        for child in self.children.order_by('parent_number').all():
             if child.has_mp3_file():
                 return child
             child_sub = child.get_child_audiobook()
@@ -388,6 +403,9 @@ class Book(models.Model):
     def get_daisy(self):
         return self.get_media("daisy")
 
+    def get_audio_epub(self):
+        return self.get_media("audio.epub")
+
     def media_url(self, format_):
         media = self.get_media(format_)
         if media:
@@ -439,10 +457,17 @@ class Book(models.Model):
     has_daisy_file.short_description = 'DAISY'
     has_daisy_file.boolean = True
 
+    def has_audio_epub_file(self):
+        return self.has_media("audio.epub")
+
     @property
     def media_daisy(self):
         return self.get_media('daisy')
     
+    @property
+    def media_audio_epub(self):
+        return self.get_media('audio.epub')
+
     def get_audiobooks(self):
         ogg_files = {}
         for m in self.media.filter(type='ogg').order_by().iterator():
@@ -548,6 +573,7 @@ class Book(models.Model):
 
     # will make problems in conjunction with paid previews
     def download_pictures(self, remote_gallery_url):
+        # This is only needed for legacy relative image paths.
         gallery_path = self.gallery_path()
         # delete previous files, so we don't include old files in ebooks
         if os.path.isdir(gallery_path):
@@ -559,6 +585,8 @@ class Book(models.Model):
             makedirs(gallery_path)
             for ilustr in ilustr_elements:
                 ilustr_src = ilustr.get('src')
+                if '/' in ilustr_src:
+                    continue
                 ilustr_path = os.path.join(gallery_path, ilustr_src)
                 urlretrieve('%s/%s' % (remote_gallery_url, ilustr_src), ilustr_path)
 
@@ -831,7 +859,7 @@ class Book(models.Model):
     def related_themes(self):
         return Tag.objects.usage_for_queryset(
             Fragment.objects.filter(models.Q(book=self) | models.Q(book__ancestor=self)),
-            counts=True).filter(category='theme')
+            counts=True).filter(category='theme').order_by('-count')
 
     def parent_cover_changed(self):
         """Called when parent book's cover image is changed."""