Use unmanaged keys instead of some magic.
[redakcja.git] / src / documents / models / book.py
1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from django.apps import apps
5 from django.contrib.sites.models import Site
6 from django.db import models, transaction
7 from django.template.loader import render_to_string
8 from django.urls import reverse
9 from django.utils.translation import ugettext_lazy as _
10 from django.conf import settings
11 from slugify import slugify
12
13
14 import apiclient
15 from documents.helpers import cached_in_field, GalleryMerger
16 from documents.models import BookPublishRecord, ChunkPublishRecord, Project
17 from documents.signals import post_publish
18 from documents.xml_tools import compile_text, split_xml
19 from cover.models import Image
20 import os
21 import shutil
22 import re
23
24 class Book(models.Model):
25     """ A document edited on the wiki """
26
27     title = models.CharField(_('title'), max_length=255, db_index=True)
28     slug = models.SlugField(_('slug'), max_length=128, unique=True, db_index=True)
29     public = models.BooleanField(_('public'), default=True, db_index=True)
30     gallery = models.CharField(_('scan gallery name'), max_length=255, blank=True)
31     project = models.ForeignKey(Project, models.SET_NULL, null=True, blank=True)
32
33     parent = models.ForeignKey('self', models.SET_NULL, null=True, blank=True, verbose_name=_('parent'), related_name="children", editable=False)
34     parent_number = models.IntegerField(_('parent number'), null=True, blank=True, db_index=True, editable=False)
35
36     # Cache
37     _single = models.NullBooleanField(editable=False, db_index=True)
38     _new_publishable = models.NullBooleanField(editable=False)
39     _published = models.NullBooleanField(editable=False)
40     _on_track = models.IntegerField(null=True, blank=True, db_index=True, editable=False)
41     dc_cover_image = models.ForeignKey(Image, blank=True, null=True,
42         db_index=True, on_delete=models.SET_NULL, editable=False)
43     catalogue_book = models.ForeignKey(
44         'catalogue.Book',
45         models.DO_NOTHING,
46         to_field='slug',
47         null=True, blank=True,
48         editable=False, db_index=True,
49         related_name='document_books',
50         related_query_name='document_book',
51     )
52
53     class NoTextError(BaseException):
54         pass
55
56     class Meta:
57         app_label = 'documents'
58         ordering = ['title', 'slug']
59         verbose_name = _('book')
60         verbose_name_plural = _('books')
61
62
63     # Representing
64     # ============
65
66     def __iter__(self):
67         return iter(self.chunk_set.all())
68
69     def __getitem__(self, chunk):
70         return self.chunk_set.all()[chunk]
71
72     def __len__(self):
73         return self.chunk_set.count()
74
75     def __bool__(self):
76         """
77             Necessary so that __len__ isn't used for bool evaluation.
78         """
79         return True
80
81     def __str__(self):
82         return self.title
83
84     def get_absolute_url(self):
85         return reverse("documents_book", args=[self.slug])
86
87     def correct_about(self):
88         return "http://%s%s" % (
89             Site.objects.get_current().domain,
90             self.get_absolute_url()
91         )
92
93     def gallery_path(self):
94         return os.path.join(settings.MEDIA_ROOT, settings.IMAGE_DIR, self.gallery)
95
96     def gallery_url(self):
97         return '%s%s%s/' % (settings.MEDIA_URL, settings.IMAGE_DIR, self.gallery)
98
99     # Creating & manipulating
100     # =======================
101
102     def accessible(self, request):
103         return self.public or request.user.is_authenticated
104
105     @classmethod
106     @transaction.atomic
107     def create(cls, creator, text, *args, **kwargs):
108         b = cls.objects.create(*args, **kwargs)
109         b.chunk_set.all().update(creator=creator)
110         b[0].commit(text, author=creator)
111         return b
112
113     def add(self, *args, **kwargs):
114         """Add a new chunk at the end."""
115         return self.chunk_set.reverse()[0].split(*args, **kwargs)
116
117     @classmethod
118     @transaction.atomic
119     def import_xml_text(cls, text=u'', previous_book=None,
120                 commit_args=None, **kwargs):
121         """Imports a book from XML, splitting it into chunks as necessary."""
122         texts = split_xml(text)
123         if previous_book:
124             instance = previous_book
125         else:
126             instance = cls(**kwargs)
127             instance.save()
128
129         # if there are more parts, set the rest to empty strings
130         book_len = len(instance)
131         for i in range(book_len - len(texts)):
132             texts.append((u'pusta część %d' % (i + 1), u''))
133
134         i = 0
135         for i, (title, text) in enumerate(texts):
136             if not title:
137                 title = u'część %d' % (i + 1)
138
139             slug = slugify(title)
140
141             if i < book_len:
142                 chunk = instance[i]
143                 chunk.slug = slug[:50]
144                 chunk.title = title[:255]
145                 chunk.save()
146             else:
147                 chunk = instance.add(slug, title)
148
149             chunk.commit(text, **commit_args)
150
151         return instance
152
153     def make_chunk_slug(self, proposed):
154         """ 
155             Finds a chunk slug not yet used in the book.
156         """
157         slugs = set(c.slug for c in self)
158         i = 1
159         new_slug = proposed[:50]
160         while new_slug in slugs:
161             new_slug = "%s_%d" % (proposed[:45], i)
162             i += 1
163         return new_slug
164
165     @transaction.atomic
166     def append(self, other, slugs=None, titles=None):
167         """Add all chunks of another book to self."""
168         assert self != other
169
170         number = self[len(self) - 1].number + 1
171         len_other = len(other)
172         single = len_other == 1
173
174         if slugs is not None:
175             assert len(slugs) == len_other
176         if titles is not None:
177             assert len(titles) == len_other
178             if slugs is None:
179                 slugs = [slugify(t) for t in titles]
180
181         for i, chunk in enumerate(other):
182             # move chunk to new book
183             chunk.book = self
184             chunk.number = number
185
186             if titles is None:
187                 # try some title guessing
188                 if other.title.startswith(self.title):
189                     other_title_part = other.title[len(self.title):].lstrip(' /')
190                 else:
191                     other_title_part = other.title
192
193                 if single:
194                     # special treatment for appending one-parters:
195                     # just use the guessed title and original book slug
196                     chunk.title = other_title_part
197                     if other.slug.startswith(self.slug):
198                         chunk.slug = other.slug[len(self.slug):].lstrip('-_')
199                     else:
200                         chunk.slug = other.slug
201                 else:
202                     chunk.title = ("%s, %s" % (other_title_part, chunk.title))[:255]
203             else:
204                 chunk.slug = slugs[i]
205                 chunk.title = titles[i]
206
207             chunk.slug = self.make_chunk_slug(chunk.slug)
208             chunk.save()
209             number += 1
210         assert not other.chunk_set.exists()
211
212         gm = GalleryMerger(self.gallery, other.gallery)
213         self.gallery = gm.merge()
214
215         # and move the gallery starts
216         if gm.was_merged:
217                 for chunk in self[len(self) - len_other:]:
218                         old_start = chunk.gallery_start or 1
219                         chunk.gallery_start = old_start + gm.dest_size - gm.num_deleted
220                         chunk.save()
221
222         other.delete()
223
224
225     @transaction.atomic
226     def prepend_history(self, other):
227         """Prepend history from all the other book's chunks to own."""
228         assert self != other
229
230         for i in range(len(self), len(other)):
231             title = u"pusta część %d" % i
232             chunk = self.add(slugify(title), title)
233             chunk.commit('')
234
235         for i in range(len(other)):
236             self[i].prepend_history(other[0])
237
238         assert not other.chunk_set.exists()
239         other.delete()
240
241     def split(self):
242         """Splits all the chunks into separate books."""
243         self.title
244         for chunk in self:
245             book = Book.objects.create(title=chunk.title, slug=chunk.slug,
246                     public=self.public, gallery=self.gallery)
247             book[0].delete()
248             chunk.book = book
249             chunk.number = 1
250             chunk.save()
251         assert not self.chunk_set.exists()
252         self.delete()
253
254     # State & cache
255     # =============
256
257     def last_published(self):
258         try:
259             return self.publish_log.all()[0].timestamp
260         except IndexError:
261             return None
262
263     def assert_publishable(self):
264         assert self.chunk_set.exists(), _('No chunks in the book.')
265         try:
266             changes = self.get_current_changes(publishable=True)
267         except self.NoTextError:
268             raise AssertionError(_('Not all chunks have publishable revisions.'))
269
270         from librarian import NoDublinCore, ParseError, ValidationError
271
272         try:
273             bi = self.wldocument(changes=changes, strict=True).book_info
274         except ParseError as e:
275             raise AssertionError(_('Invalid XML') + ': ' + str(e))
276         except NoDublinCore:
277             raise AssertionError(_('No Dublin Core found.'))
278         except ValidationError as e:
279             raise AssertionError(_('Invalid Dublin Core') + ': ' + str(e))
280
281         valid_about = self.correct_about()
282         assert bi.about == valid_about, _("rdf:about is not") + " " + valid_about
283
284     def publishable_error(self):
285         try:
286             return self.assert_publishable()
287         except AssertionError as e:
288             return e
289         else:
290             return None
291
292     def hidden(self):
293         return self.slug.startswith('.')
294
295     def is_new_publishable(self):
296         """Checks if book is ready for publishing.
297
298         Returns True if there is a publishable version newer than the one
299         already published.
300
301         """
302         new_publishable = False
303         if not self.chunk_set.exists():
304             return False
305         for chunk in self:
306             change = chunk.publishable()
307             if not change:
308                 return False
309             if not new_publishable and not change.publish_log.exists():
310                 new_publishable = True
311         return new_publishable
312     new_publishable = cached_in_field('_new_publishable')(is_new_publishable)
313
314     def is_published(self):
315         return self.publish_log.exists()
316     published = cached_in_field('_published')(is_published)
317
318     def get_on_track(self):
319         if self.published:
320             return -1
321         stages = [ch.stage.ordering if ch.stage is not None else 0
322                     for ch in self]
323         if not len(stages):
324             return 0
325         return min(stages)
326     on_track = cached_in_field('_on_track')(get_on_track)
327
328     def is_single(self):
329         return len(self) == 1
330     single = cached_in_field('_single')(is_single)
331
332     def book_info(self, publishable=True):
333         try:
334             book_xml = self.materialize(publishable=publishable)
335         except self.NoTextError:
336             pass
337         else:
338             from librarian.dcparser import BookInfo
339             from librarian import NoDublinCore, ParseError, ValidationError
340             try:
341                 return BookInfo.from_bytes(book_xml.encode('utf-8'))
342             except (self.NoTextError, ParseError, NoDublinCore, ValidationError):
343                 return None
344
345     def refresh_dc_cache(self):
346         update = {
347             'catalogue_book_id': None,
348             'dc_cover_image': None,
349         }
350
351         info = self.book_info()
352         print(info)
353         if info is not None:
354             update['catalogue_book_id'] = info.url.slug
355             print(info.url.slug)
356             if info.cover_source:
357                 try:
358                     image = Image.objects.get(pk=int(info.cover_source.rstrip('/').rsplit('/', 1)[-1]))
359                 except:
360                     pass
361                 else:
362                     if info.cover_source == image.get_full_url():
363                         update['dc_cover_image'] = image
364         print(update)
365         Book.objects.filter(pk=self.pk).update(**update)
366
367     def touch(self):
368         update = {
369             "_new_publishable": self.is_new_publishable(),
370             "_published": self.is_published(),
371             "_single": self.is_single(),
372             "_on_track": self.get_on_track(),
373         }
374         Book.objects.filter(pk=self.pk).update(**update)
375         self.refresh_dc_cache()
376
377     # Materializing & publishing
378     # ==========================
379
380     def get_current_changes(self, publishable=True):
381         """
382             Returns a list containing one Change for every Chunk in the Book.
383             Takes the most recent revision (publishable, if set).
384             Throws an error, if a proper revision is unavailable for a Chunk.
385         """
386         if publishable:
387             changes = [chunk.publishable() for chunk in self]
388         else:
389             changes = [chunk.head for chunk in self if chunk.head is not None]
390         if None in changes:
391             raise self.NoTextError('Some chunks have no available text.')
392         return changes
393
394     def materialize(self, publishable=False, changes=None):
395         """ 
396             Get full text of the document compiled from chunks.
397             Takes the current versions of all texts
398             or versions most recently tagged for publishing,
399             or a specified iterable changes.
400         """
401         if changes is None:
402             changes = self.get_current_changes(publishable)
403         return compile_text(change.materialize() for change in changes)
404
405     def wldocument(self, publishable=True, changes=None, 
406             parse_dublincore=True, strict=False):
407         from documents.ebook_utils import RedakcjaDocProvider
408         from librarian.parser import WLDocument
409
410         return WLDocument.from_bytes(
411                 self.materialize(publishable=publishable, changes=changes).encode('utf-8'),
412                 provider=RedakcjaDocProvider(publishable=publishable),
413                 parse_dublincore=parse_dublincore,
414                 strict=strict)
415
416     def publish(self, user, fake=False, host=None, days=0, beta=False):
417         """
418             Publishes a book on behalf of a (local) user.
419         """
420         self.assert_publishable()
421         changes = self.get_current_changes(publishable=True)
422         if not fake:
423             book_xml = self.materialize(changes=changes)
424             data = {"book_xml": book_xml, "days": days}
425             if host:
426                 data['gallery_url'] = host + self.gallery_url()
427             apiclient.api_call(user, "books/", data, beta=beta)
428         if not beta:
429             # record the publish
430             br = BookPublishRecord.objects.create(book=self, user=user)
431             for c in changes:
432                 ChunkPublishRecord.objects.create(book_record=br, change=c)
433             if not self.public and days == 0:
434                 self.public = True
435                 self.save()
436             if self.public and days > 0:
437                 self.public = False
438                 self.save()
439             post_publish.send(sender=br)
440
441     def latex_dir(self):
442         doc = self.wldocument()
443         return doc.latex_dir(cover=True, ilustr_path=self.gallery_path())