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