X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/4157358510703a54cde8f3b0f9814f2cd1c9f40a..8f031335a8a0dabceec381b89eb0cbbcb2f4b637:/src/catalogue/fields.py diff --git a/src/catalogue/fields.py b/src/catalogue/fields.py index 38cac775b..c592c5504 100644 --- a/src/catalogue/fields.py +++ b/src/catalogue/fields.py @@ -69,14 +69,10 @@ class EbookField(models.FileField): librarian2_api = False ZIP = None - def __init__(self, verbose_name_=None, with_etag=True, **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_ ) - + 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) kwargs.setdefault('blank', True) kwargs.setdefault('default', '') @@ -94,16 +90,15 @@ class EbookField(models.FileField): del kwargs['default'] if self.get_upload_to(self.ext) == kwargs.get('upload_to'): del kwargs['upload_to'] - if not self.with_etag: + # with_etag creates a second field, which then deconstructs to manage + # its own migrations. So for migrations, etag_field_name is explicitly + # set to avoid double creation of the etag field. + if self.with_etag: + kwargs['etag_field_name'] = self.etag_field_name + 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 + return name, path, args, kwargs @classmethod def get_upload_to(cls, directory): @@ -114,8 +109,8 @@ class EbookField(models.FileField): def contribute_to_class(self, cls, name): super(EbookField, self).contribute_to_class(cls, name) - self.etag_field_name = f'{name}_etag' - if self.with_etag: + if self.with_etag and not self.etag_field_name: + self.etag_field_name = f'{name}_etag' self.etag_field = models.CharField(max_length=255, editable=False, default='', db_index=True) self.etag_field.contribute_to_class(cls, f'{name}_etag') @@ -178,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]) @@ -221,7 +217,7 @@ class PdfField(EbookField): base_url=absolute_url(gallery_url(wldoc.book_info.url.slug)), customizations=['notoc']) def build(self, fieldfile): - BuildEbook.build(self, fieldfile) + super().build(fieldfile) clear_cache(fieldfile.instance.slug) @@ -363,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