Audiobook fixes.
authorRadek Czajka <rczajka@rczajka.pl>
Thu, 23 Dec 2021 11:39:27 +0000 (12:39 +0100)
committerRadek Czajka <rczajka@rczajka.pl>
Thu, 23 Dec 2021 11:39:27 +0000 (12:39 +0100)
src/catalogue/models/book.py
src/catalogue/models/bookmedia.py

index 9f0576c..b1684f9 100644 (file)
@@ -479,7 +479,7 @@ class Book(models.Model):
 
     def zip_audiobooks(self, format_):
         bm = BookMedia.objects.filter(book=self, type=format_)
 
     def zip_audiobooks(self, format_):
         bm = BookMedia.objects.filter(book=self, type=format_)
-        paths = map(lambda bm: (None, bm.file.path), bm)
+        paths = map(lambda bm: (bm.get_nice_filename(), bm.file.path), bm)
         licenses = set()
         for m in bm:
             license = constants.LICENSES.get(
         licenses = set()
         for m in bm:
             license = constants.LICENSES.get(
index 14483eb..e2fc343 100644 (file)
@@ -56,6 +56,17 @@ class BookMedia(models.Model):
     def get_extra_info_json(self):
         return json.loads(self.extra_info or '{}')
 
     def get_extra_info_json(self):
         return json.loads(self.extra_info or '{}')
 
+    def get_nice_filename(self):
+        parts_count = 1 + type(self).objects.filter(book=self.book, type=self.type).exclude(pk=self.pk).count()
+
+        name = self.book.slug
+        if parts_count > 0:
+            name += f'_{self.index:03d}'
+        if self.part_name:
+            name += f'_' + slugify(self.part_name)
+        ext = self.ext()
+        return f'{name}.{ext}'
+
     def save(self, parts_count=None, *args, **kwargs):
         from catalogue.utils import ExistingFile, remove_zip
 
     def save(self, parts_count=None, *args, **kwargs):
         from catalogue.utils import ExistingFile, remove_zip
 
@@ -73,13 +84,9 @@ class BookMedia(models.Model):
             old = BookMedia.objects.get(pk=self.pk)
         except BookMedia.DoesNotExist:
             old = None
             old = BookMedia.objects.get(pk=self.pk)
         except BookMedia.DoesNotExist:
             old = None
-        else:
-            # if name changed, change the file name, too
-            if slugify(self.name) != slugify(old.name):
-                self.file.save(None, ExistingFile(self.file.path), save=False)
 
         super(BookMedia, self).save(*args, **kwargs)
 
         super(BookMedia, self).save(*args, **kwargs)
-
+        
         # remove the zip package for book with modified media
         if old:
             remove_zip("%s_%s" % (old.book.slug, old.type))
         # remove the zip package for book with modified media
         if old:
             remove_zip("%s_%s" % (old.book.slug, old.type))