banner image for misja ksiazka
[wolnelektury.git] / src / catalogue / fields.py
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.
4 #
5 from django.conf import settings
6 from django.core.files import File
7 from django.db import models
8 from django.db.models.fields.files import FieldFile
9 from catalogue import app_settings
10 from catalogue.constants import LANGUAGES_3TO2
11 from catalogue.utils import remove_zip, truncate_html_words, gallery_path, gallery_url
12 from celery.task import Task, task
13 from celery.utils.log import get_task_logger
14 from waiter.utils import clear_cache
15
16 task_logger = get_task_logger(__name__)
17
18
19 class EbookFieldFile(FieldFile):
20     """Represents contents of an ebook file field."""
21
22     def build(self):
23         """Build the ebook immediately."""
24         return self.field.builder.build(self)
25
26     def build_delay(self):
27         """Builds the ebook in a delayed task."""
28         return self.field.builder.delay(self.instance, self.field.attname)
29
30
31 class EbookField(models.FileField):
32     """Represents an ebook file field, attachable to a model."""
33     attr_class = EbookFieldFile
34
35     def __init__(self, format_name, *args, **kwargs):
36         super(EbookField, self).__init__(*args, **kwargs)
37         self.format_name = format_name
38
39     def deconstruct(self):
40         name, path, args, kwargs = super(EbookField, self).deconstruct()
41         args.insert(0, self.format_name)
42         return name, path, args, kwargs
43
44     @property
45     def builder(self):
46         """Finds a celery task suitable for the format of the field."""
47         return BuildEbook.for_format(self.format_name)
48
49     def contribute_to_class(self, cls, name):
50         super(EbookField, self).contribute_to_class(cls, name)
51
52         def has(model_instance):
53             return bool(getattr(model_instance, self.attname, None))
54         has.__doc__ = None
55         has.__name__ = str("has_%s" % self.attname)
56         has.short_description = self.name
57         has.boolean = True
58         setattr(cls, 'has_%s' % self.attname, has)
59
60
61 class BuildEbook(Task):
62     formats = {}
63
64     @classmethod
65     def register(cls, format_name):
66         """A decorator for registering subclasses for particular formats."""
67         def wrapper(builder):
68             cls.formats[format_name] = builder
69             return builder
70         return wrapper
71
72     @classmethod
73     def for_format(cls, format_name):
74         """Returns a celery task suitable for specified format."""
75         return cls.formats.get(format_name, BuildEbookTask)
76
77     @staticmethod
78     def transform(wldoc, fieldfile):
79         """Transforms an librarian.WLDocument into an librarian.OutputFile.
80
81         By default, it just calls relevant wldoc.as_??? method.
82
83         """
84         return getattr(wldoc, "as_%s" % fieldfile.field.format_name)()
85
86     def run(self, obj, field_name):
87         """Just run `build` on FieldFile, can't pass it directly to Celery."""
88         task_logger.info("%s -> %s" % (obj.slug, field_name))
89         ret = self.build(getattr(obj, field_name))
90         obj.flush_includes()
91         return ret
92
93     def build(self, fieldfile):
94         book = fieldfile.instance
95         out = self.transform(book.wldocument(), fieldfile)
96         fieldfile.save(None, File(open(out.get_filename())), save=False)
97         if book.pk is not None:
98             type(book).objects.filter(pk=book.pk).update(**{
99                 fieldfile.field.attname: fieldfile
100             })
101         if fieldfile.field.format_name in app_settings.FORMAT_ZIPS:
102             remove_zip(app_settings.FORMAT_ZIPS[fieldfile.field.format_name])
103 # Don't decorate BuildEbook, because we want to subclass it.
104 BuildEbookTask = task(BuildEbook, ignore_result=True)
105
106
107 @BuildEbook.register('txt')
108 @task(ignore_result=True)
109 class BuildTxt(BuildEbook):
110     @staticmethod
111     def transform(wldoc, fieldfile):
112         return wldoc.as_text()
113
114
115 @BuildEbook.register('pdf')
116 @task(ignore_result=True)
117 class BuildPdf(BuildEbook):
118     @staticmethod
119     def transform(wldoc, fieldfile):
120         return wldoc.as_pdf(morefloats=settings.LIBRARIAN_PDF_MOREFLOATS, cover=True,
121                             ilustr_path=gallery_path(wldoc.book_info.url.slug))
122
123     def build(self, fieldfile):
124         BuildEbook.build(self, fieldfile)
125         clear_cache(fieldfile.instance.slug)
126
127
128 @BuildEbook.register('epub')
129 @task(ignore_result=True)
130 class BuildEpub(BuildEbook):
131     @staticmethod
132     def transform(wldoc, fieldfile):
133         return wldoc.as_epub(cover=True, ilustr_path=gallery_path(wldoc.book_info.url.slug))
134
135
136 @BuildEbook.register('mobi')
137 @task(ignore_result=True)
138 class BuildMobi(BuildEbook):
139     @staticmethod
140     def transform(wldoc, fieldfile):
141         return wldoc.as_mobi(cover=True, ilustr_path=gallery_path(wldoc.book_info.url.slug))
142
143
144 @BuildEbook.register('html')
145 @task(ignore_result=True)
146 class BuildHtml(BuildEbook):
147     def build(self, fieldfile):
148         from django.core.files.base import ContentFile
149         from fnpdjango.utils.text.slughifi import slughifi
150         from sortify import sortify
151         from librarian import html
152         from catalogue.models import Fragment, Tag
153
154         book = fieldfile.instance
155
156         html_output = self.transform(book.wldocument(parse_dublincore=False), fieldfile)
157
158         # Delete old fragments, create from scratch if necessary.
159         book.fragments.all().delete()
160
161         if html_output:
162             meta_tags = list(book.tags.filter(
163                 category__in=('author', 'epoch', 'genre', 'kind')))
164
165             lang = book.language
166             lang = LANGUAGES_3TO2.get(lang, lang)
167             if lang not in [ln[0] for ln in settings.LANGUAGES]:
168                 lang = None
169
170             fieldfile.save(None, ContentFile(html_output.get_string()), save=False)
171             type(book).objects.filter(pk=book.pk).update(**{
172                 fieldfile.field.attname: fieldfile
173             })
174
175             # Extract fragments
176             closed_fragments, open_fragments = html.extract_fragments(fieldfile.path)
177             for fragment in closed_fragments.values():
178                 try:
179                     theme_names = [s.strip() for s in fragment.themes.split(',')]
180                 except AttributeError:
181                     continue
182                 themes = []
183                 for theme_name in theme_names:
184                     if not theme_name:
185                         continue
186                     if lang == settings.LANGUAGE_CODE:
187                         # Allow creating themes if book in default language.
188                         tag, created = Tag.objects.get_or_create(
189                                             slug=slughifi(theme_name),
190                                             category='theme')
191                         if created:
192                             tag.name = theme_name
193                             setattr(tag, "name_%s" % lang, theme_name)
194                             tag.sort_key = sortify(theme_name.lower())
195                             tag.save()
196                         themes.append(tag)
197                     elif lang is not None:
198                         # Don't create unknown themes in non-default languages.
199                         try:
200                             tag = Tag.objects.get(category='theme', **{"name_%s" % lang: theme_name})
201                         except Tag.DoesNotExist:
202                             pass
203                         else:
204                             themes.append(tag)
205                 if not themes:
206                     continue
207
208                 text = fragment.to_string()
209                 short_text = truncate_html_words(text, 15)
210                 if text == short_text:
211                     short_text = ''
212                 new_fragment = Fragment.objects.create(anchor=fragment.id, book=book, text=text, short_text=short_text)
213
214                 new_fragment.save()
215                 new_fragment.tags = set(meta_tags + themes)
216             book.html_built.send(sender=type(self), instance=book)
217             return True
218         return False
219
220     @staticmethod
221     def transform(wldoc, fieldfile):
222         # ugly, but we can't use wldoc.book_info here
223         from librarian import DCNS
224         url_elem = wldoc.edoc.getroot().find('.//' + DCNS('identifier.url'))
225         if url_elem is None:
226             gallery = ''
227         else:
228             gallery = gallery_url(slug=url_elem.text.rsplit('/', 1)[1])
229         return wldoc.as_html(options={'gallery': "'%s'" % gallery})
230
231
232 @BuildEbook.register('cover_thumb')
233 @task(ignore_result=True)
234 class BuildCoverThumb(BuildEbook):
235     @classmethod
236     def transform(cls, wldoc, fieldfile):
237         from librarian.cover import WLCover
238         return WLCover(wldoc.book_info, height=193).output_file()
239
240
241 class OverwritingFieldFile(FieldFile):
242     """
243         Deletes the old file before saving the new one.
244     """
245
246     def save(self, name, content, *args, **kwargs):
247         leave = kwargs.pop('leave', None)
248         # delete if there's a file already and there's a new one coming
249         if not leave and self and (not hasattr(content, 'path') or content.path != self.path):
250             self.delete(save=False)
251         return super(OverwritingFieldFile, self).save(name, content, *args, **kwargs)
252
253
254 class OverwritingFileField(models.FileField):
255     attr_class = OverwritingFieldFile