1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.conf import settings
6 from django.core.files import File
7 from django.core.files.storage import FileSystemStorage
8 from django.db import models
9 from django.db.models.fields.files import FieldFile
10 from catalogue import app_settings
11 from catalogue.constants import LANGUAGES_3TO2
12 from catalogue.utils import remove_zip, truncate_html_words, gallery_path, gallery_url
13 from celery.task import Task, task
14 from celery.utils.log import get_task_logger
15 from waiter.utils import clear_cache
17 task_logger = get_task_logger(__name__)
20 class EbookFieldFile(FieldFile):
21 """Represents contents of an ebook file field."""
24 """Build the ebook immediately."""
25 return self.field.builder.build(self)
27 def build_delay(self):
28 """Builds the ebook in a delayed task."""
29 return self.field.builder.delay(self.instance, self.field.attname)
32 return self.instance.media_url(self.field.attname.split('_')[0])
34 def set_readable(self, readable):
36 permissions = 0o644 if readable else 0o600
37 os.chmod(self.path, permissions)
40 class EbookField(models.FileField):
41 """Represents an ebook file field, attachable to a model."""
42 attr_class = EbookFieldFile
44 def __init__(self, format_name, *args, **kwargs):
45 super(EbookField, self).__init__(*args, **kwargs)
46 self.format_name = format_name
48 def deconstruct(self):
49 name, path, args, kwargs = super(EbookField, self).deconstruct()
50 args.insert(0, self.format_name)
51 return name, path, args, kwargs
55 """Finds a celery task suitable for the format of the field."""
56 return BuildEbook.for_format(self.format_name)
58 def contribute_to_class(self, cls, name):
59 super(EbookField, self).contribute_to_class(cls, name)
61 def has(model_instance):
62 return bool(getattr(model_instance, self.attname, None))
64 has.__name__ = str("has_%s" % self.attname)
65 has.short_description = self.name
67 setattr(cls, 'has_%s' % self.attname, has)
70 class BuildEbook(Task):
74 def register(cls, format_name):
75 """A decorator for registering subclasses for particular formats."""
77 cls.formats[format_name] = builder
82 def for_format(cls, format_name):
83 """Returns a celery task suitable for specified format."""
84 return cls.formats.get(format_name, BuildEbookTask)
87 def transform(wldoc, fieldfile):
88 """Transforms an librarian.WLDocument into an librarian.OutputFile.
90 By default, it just calls relevant wldoc.as_??? method.
93 return getattr(wldoc, "as_%s" % fieldfile.field.format_name)()
95 def run(self, obj, field_name):
96 """Just run `build` on FieldFile, can't pass it directly to Celery."""
97 task_logger.info("%s -> %s" % (obj.slug, field_name))
98 ret = self.build(getattr(obj, field_name))
102 def set_file_permissions(self, fieldfile):
103 if fieldfile.instance.preview:
104 fieldfile.set_readable(False)
106 def build(self, fieldfile):
107 book = fieldfile.instance
108 out = self.transform(book.wldocument(), fieldfile)
109 fieldfile.save(None, File(open(out.get_filename())), save=False)
110 self.set_file_permissions(fieldfile)
111 if book.pk is not None:
112 type(book).objects.filter(pk=book.pk).update(**{
113 fieldfile.field.attname: fieldfile
115 if fieldfile.field.format_name in app_settings.FORMAT_ZIPS:
116 remove_zip(app_settings.FORMAT_ZIPS[fieldfile.field.format_name])
117 # Don't decorate BuildEbook, because we want to subclass it.
118 BuildEbookTask = task(BuildEbook, ignore_result=True)
121 @BuildEbook.register('txt')
122 @task(ignore_result=True)
123 class BuildTxt(BuildEbook):
125 def transform(wldoc, fieldfile):
126 return wldoc.as_text()
129 @BuildEbook.register('pdf')
130 @task(ignore_result=True)
131 class BuildPdf(BuildEbook):
133 def transform(wldoc, fieldfile):
134 return wldoc.as_pdf(morefloats=settings.LIBRARIAN_PDF_MOREFLOATS, cover=True,
135 ilustr_path=gallery_path(wldoc.book_info.url.slug))
137 def build(self, fieldfile):
138 BuildEbook.build(self, fieldfile)
139 clear_cache(fieldfile.instance.slug)
142 @BuildEbook.register('epub')
143 @task(ignore_result=True)
144 class BuildEpub(BuildEbook):
146 def transform(wldoc, fieldfile):
147 return wldoc.as_epub(cover=True, ilustr_path=gallery_path(wldoc.book_info.url.slug))
150 @BuildEbook.register('mobi')
151 @task(ignore_result=True)
152 class BuildMobi(BuildEbook):
154 def transform(wldoc, fieldfile):
155 return wldoc.as_mobi(cover=True, ilustr_path=gallery_path(wldoc.book_info.url.slug))
158 @BuildEbook.register('html')
159 @task(ignore_result=True)
160 class BuildHtml(BuildEbook):
161 def build(self, fieldfile):
162 from django.core.files.base import ContentFile
163 from fnpdjango.utils.text.slughifi import slughifi
164 from sortify import sortify
165 from librarian import html
166 from catalogue.models import Fragment, Tag
168 book = fieldfile.instance
170 html_output = self.transform(book.wldocument(parse_dublincore=False), fieldfile)
172 # Delete old fragments, create from scratch if necessary.
173 book.fragments.all().delete()
176 meta_tags = list(book.tags.filter(
177 category__in=('author', 'epoch', 'genre', 'kind')))
180 lang = LANGUAGES_3TO2.get(lang, lang)
181 if lang not in [ln[0] for ln in settings.LANGUAGES]:
184 fieldfile.save(None, ContentFile(html_output.get_string()), save=False)
185 self.set_file_permissions(fieldfile)
186 type(book).objects.filter(pk=book.pk).update(**{
187 fieldfile.field.attname: fieldfile
191 closed_fragments, open_fragments = html.extract_fragments(fieldfile.path)
192 for fragment in closed_fragments.values():
194 theme_names = [s.strip() for s in fragment.themes.split(',')]
195 except AttributeError:
198 for theme_name in theme_names:
201 if lang == settings.LANGUAGE_CODE:
202 # Allow creating themes if book in default language.
203 tag, created = Tag.objects.get_or_create(
204 slug=slughifi(theme_name),
207 tag.name = theme_name
208 setattr(tag, "name_%s" % lang, theme_name)
209 tag.sort_key = sortify(theme_name.lower())
213 elif lang is not None:
214 # Don't create unknown themes in non-default languages.
216 tag = Tag.objects.get(category='theme', **{"name_%s" % lang: theme_name})
217 except Tag.DoesNotExist:
224 text = fragment.to_string()
225 short_text = truncate_html_words(text, 15)
226 if text == short_text:
228 new_fragment = Fragment.objects.create(anchor=fragment.id, book=book, text=text, short_text=short_text)
231 new_fragment.tags = set(meta_tags + themes)
233 if not theme.for_books:
234 theme.for_books = True
236 book.html_built.send(sender=type(self), instance=book)
241 def transform(wldoc, fieldfile):
242 # ugly, but we can't use wldoc.book_info here
243 from librarian import DCNS
244 url_elem = wldoc.edoc.getroot().find('.//' + DCNS('identifier.url'))
248 gallery = gallery_url(slug=url_elem.text.rsplit('/', 1)[1])
249 return wldoc.as_html(options={'gallery': "'%s'" % gallery})
252 class BuildCover(BuildEbook):
253 def set_file_permissions(self, fieldfile):
257 @BuildEbook.register('cover_thumb')
258 @task(ignore_result=True)
259 class BuildCoverThumb(BuildCover):
261 def transform(cls, wldoc, fieldfile):
262 from librarian.cover import WLCover
263 return WLCover(wldoc.book_info, height=193).output_file()
266 @BuildEbook.register('cover_api_thumb')
267 @task(ignore_result=True)
268 class BuildCoverApiThumb(BuildCover):
270 def transform(cls, wldoc, fieldfile):
271 from librarian.cover import WLNoBoxCover
272 return WLNoBoxCover(wldoc.book_info, height=500).output_file()
275 @BuildEbook.register('simple_cover')
276 @task(ignore_result=True)
277 class BuildSimpleCover(BuildCover):
279 def transform(cls, wldoc, fieldfile):
280 from librarian.cover import WLNoBoxCover
281 return WLNoBoxCover(wldoc.book_info, height=1000).output_file()
284 # not used, but needed for migrations
285 class OverwritingFieldFile(FieldFile):
287 Deletes the old file before saving the new one.
290 def save(self, name, content, *args, **kwargs):
291 leave = kwargs.pop('leave', None)
292 # delete if there's a file already and there's a new one coming
293 if not leave and self and (not hasattr(content, 'path') or content.path != self.path):
294 self.delete(save=False)
295 return super(OverwritingFieldFile, self).save(name, content, *args, **kwargs)
298 class OverwritingFileField(models.FileField):
299 attr_class = OverwritingFieldFile
302 class OverwriteStorage(FileSystemStorage):
304 def get_available_name(self, name, max_length=None):