Tidying up all the covers craziness and storages.
[wolnelektury.git] / src / catalogue / models / bookmedia.py
index acb1881..c1b0c5f 100644 (file)
@@ -4,11 +4,11 @@
 from collections import OrderedDict
 import json
 from collections import namedtuple
 from collections import OrderedDict
 import json
 from collections import namedtuple
+from django.core.files.storage import storages
 from django.db import models
 from slugify import slugify
 import mutagen
 from mutagen import id3
 from django.db import models
 from slugify import slugify
 import mutagen
 from mutagen import id3
-from fnpdjango.storage import BofhFileSystemStorage
 
 
 def _file_upload_to(i, _n):
 
 
 def _file_upload_to(i, _n):
@@ -21,6 +21,10 @@ def _file_upload_to(i, _n):
     return f'book/{ext}/{name}.{ext}'
 
 
     return f'book/{ext}/{name}.{ext}'
 
 
+def get_storage():
+    return storages['bookmedia']
+
+
 class BookMedia(models.Model):
     """Represents media attached to a book."""
     FileFormat = namedtuple("FileFormat", "name ext")
 class BookMedia(models.Model):
     """Represents media attached to a book."""
     FileFormat = namedtuple("FileFormat", "name ext")
@@ -37,7 +41,7 @@ class BookMedia(models.Model):
     name = models.CharField('nazwa', max_length=512)
     part_name = models.CharField('nazwa części', default='', blank=True, max_length=512)
     index = models.IntegerField('indeks', default=0)
     name = models.CharField('nazwa', max_length=512)
     part_name = models.CharField('nazwa części', default='', blank=True, max_length=512)
     index = models.IntegerField('indeks', default=0)
-    file = models.FileField('plik', max_length=600, upload_to=_file_upload_to, storage=BofhFileSystemStorage())
+    file = models.FileField('plik', max_length=600, upload_to=_file_upload_to, storage=get_storage)
     duration = models.FloatField(null=True, blank=True)
     uploaded_at = models.DateTimeField('data utworzenia', auto_now_add=True, editable=False, db_index=True)
     project_description = models.CharField(max_length=2048, blank=True)
     duration = models.FloatField(null=True, blank=True)
     uploaded_at = models.DateTimeField('data utworzenia', auto_now_add=True, editable=False, db_index=True)
     project_description = models.CharField(max_length=2048, blank=True)
@@ -70,6 +74,8 @@ class BookMedia(models.Model):
         return f'{name}.{ext}'
 
     def save(self, parts_count=None, *args, **kwargs):
         return f'{name}.{ext}'
 
     def save(self, parts_count=None, *args, **kwargs):
+        if self.type in ('daisy', 'audio.epub'):
+            return super().save(*args, **kwargs)
         from catalogue.utils import ExistingFile, remove_zip
 
         if not parts_count:
         from catalogue.utils import ExistingFile, remove_zip
 
         if not parts_count:
@@ -87,7 +93,7 @@ class BookMedia(models.Model):
         except BookMedia.DoesNotExist:
             old = None
 
         except BookMedia.DoesNotExist:
             old = None
 
-        super(BookMedia, self).save(*args, **kwargs)
+        #super(BookMedia, self).save(*args, **kwargs)
         
         # remove the zip package for book with modified media
         if old:
         
         # remove the zip package for book with modified media
         if old:
@@ -99,7 +105,9 @@ class BookMedia(models.Model):
         self.extra_info = json.dumps(extra_info)
         self.source_sha1 = self.read_source_sha1(self.file.path, self.type)
         self.duration = self.read_duration()
         self.extra_info = json.dumps(extra_info)
         self.source_sha1 = self.read_source_sha1(self.file.path, self.type)
         self.duration = self.read_duration()
-        return super(BookMedia, self).save(*args, **kwargs)
+        super(BookMedia, self).save(*args, **kwargs)
+        self.book.update_narrators()
+        self.book.update_has_audio()
 
     def read_duration(self):
         try:
 
     def read_duration(self):
         try: