Add missing constraint.
[wolnelektury.git] / src / catalogue / fields.py
index aaa322d..c592c55 100644 (file)
@@ -69,18 +69,8 @@ class EbookField(models.FileField):
     librarian2_api = False
     ZIP = None
 
-    def __init__(self, verbose_name_=None, with_etag=True, etag_field_name=None, **kwargs):
-        # This is just for compatibility with older migrations,
-        # where first argument was for ebook format.
-        # Can be scrapped if old migrations are updated/removed.
-        verbose_name = verbose_name_ or _("%s file") % self.ext
-        kwargs.setdefault('verbose_name', verbose_name_ )
-
-        # Another compatibility fix:
-        # old migrations use EbookField directly, creating etag fields.
-        if type(self) is EbookField:
-            with_etag = False
-
+    def __init__(self, verbose_name=None, with_etag=True, etag_field_name=None, **kwargs):
+        kwargs.setdefault('verbose_name', verbose_name)
         self.with_etag = with_etag
         self.etag_field_name = etag_field_name
         kwargs.setdefault('max_length', 255)
@@ -108,15 +98,8 @@ class EbookField(models.FileField):
         else:
             kwargs['with_etag'] = self.with_etag
 
-        # Compatibility
-        verbose_name = kwargs.get('verbose_name')
-        if verbose_name:
-            del kwargs['verbose_name']
-            if verbose_name != _("%s file") % self.ext:
-                args = [verbose_name] + args
         return name, path, args, kwargs
 
-
     @classmethod
     def get_upload_to(cls, directory):
         directory = getattr(cls, 'directory', cls.ext)
@@ -190,7 +173,8 @@ class EbookField(models.FileField):
         out = self.transform(
             book.wldocument2() if self.librarian2_api else book.wldocument(),
         )
-        fieldfile.save(None, File(open(out.get_filename(), 'rb')), save=False)
+        with open(out.get_filename(), 'rb') as f:
+            fieldfile.save(None, File(f), save=False)
         self.set_file_permissions(fieldfile)
         if book.pk is not None:
             book.save(update_fields=[self.attname])
@@ -375,6 +359,10 @@ class CoverField(EbookField):
     ext = 'jpg'
     directory = 'cover'
 
+    @staticmethod
+    def transform(wldoc):
+        return wldoc.as_cover()
+
     def set_file_permissions(self, fieldfile):
         pass