1 # -*- coding: utf-8 -*-
3 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from django.contrib.sites.models import Site
7 from django.db import models, transaction
8 from django.template.loader import render_to_string
9 from django.utils.translation import ugettext_lazy as _
10 from django.conf import settings
11 from slughifi import slughifi
15 from catalogue.helpers import cached_in_field, GalleryMerger
16 from catalogue.models import BookPublishRecord, ChunkPublishRecord
17 from catalogue.signals import post_publish
18 from catalogue.tasks import refresh_instance, book_content_updated
19 from catalogue.xml_tools import compile_text, split_xml
24 class Book(models.Model):
25 """ A document edited on the wiki """
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)
32 #wl_slug = models.CharField(_('title'), max_length=255, null=True, db_index=True, editable=False)
33 parent = models.ForeignKey('self', 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)
37 _short_html = models.TextField(null=True, blank=True, editable=False)
38 _single = models.NullBooleanField(editable=False, db_index=True)
39 _new_publishable = models.NullBooleanField(editable=False)
40 _published = models.NullBooleanField(editable=False)
41 _on_track = models.IntegerField(null=True, blank=True, db_index=True, editable=False)
42 dc_slug = models.CharField(max_length=128, null=True, blank=True,
43 editable=False, db_index=True)
45 class NoTextError(BaseException):
49 app_label = 'catalogue'
50 ordering = ['title', 'slug']
51 verbose_name = _('book')
52 verbose_name_plural = _('books')
59 return iter(self.chunk_set.all())
61 def __getitem__(self, chunk):
62 return self.chunk_set.all()[chunk]
65 return self.chunk_set.count()
67 def __nonzero__(self):
69 Necessary so that __len__ isn't used for bool evaluation.
73 def __unicode__(self):
77 def get_absolute_url(self):
78 return ("catalogue_book", [self.slug])
80 def correct_about(self):
81 return "http://%s%s" % (
82 Site.objects.get_current().domain,
83 self.get_absolute_url()
86 # Creating & manipulating
87 # =======================
89 def accessible(self, request):
90 return self.public or request.user.is_authenticated()
93 @transaction.commit_on_success
94 def create(cls, creator, text, *args, **kwargs):
95 b = cls.objects.create(*args, **kwargs)
96 b.chunk_set.all().update(creator=creator)
97 b[0].commit(text, author=creator)
100 def add(self, *args, **kwargs):
101 """Add a new chunk at the end."""
102 return self.chunk_set.reverse()[0].split(*args, **kwargs)
105 @transaction.commit_on_success
106 def import_xml_text(cls, text=u'', previous_book=None,
107 commit_args=None, **kwargs):
108 """Imports a book from XML, splitting it into chunks as necessary."""
109 texts = split_xml(text)
111 instance = previous_book
113 instance = cls(**kwargs)
116 # if there are more parts, set the rest to empty strings
117 book_len = len(instance)
118 for i in range(book_len - len(texts)):
119 texts.append((u'pusta część %d' % (i + 1), u''))
122 for i, (title, text) in enumerate(texts):
124 title = u'część %d' % (i + 1)
126 slug = slughifi(title)
130 chunk.slug = slug[:50]
131 chunk.title = title[:255]
134 chunk = instance.add(slug, title)
136 chunk.commit(text, **commit_args)
140 def make_chunk_slug(self, proposed):
142 Finds a chunk slug not yet used in the book.
144 slugs = set(c.slug for c in self)
146 new_slug = proposed[:50]
147 while new_slug in slugs:
148 new_slug = "%s_%d" % (proposed[:45], i)
152 @transaction.commit_on_success
153 def append(self, other, slugs=None, titles=None):
154 """Add all chunks of another book to self."""
157 number = self[len(self) - 1].number + 1
158 len_other = len(other)
159 single = len_other == 1
161 if slugs is not None:
162 assert len(slugs) == len_other
163 if titles is not None:
164 assert len(titles) == len_other
166 slugs = [slughifi(t) for t in titles]
168 for i, chunk in enumerate(other):
169 # move chunk to new book
171 chunk.number = number
174 # try some title guessing
175 if other.title.startswith(self.title):
176 other_title_part = other.title[len(self.title):].lstrip(' /')
178 other_title_part = other.title
181 # special treatment for appending one-parters:
182 # just use the guessed title and original book slug
183 chunk.title = other_title_part
184 if other.slug.startswith(self.slug):
185 chunk.slug = other.slug[len(self.slug):].lstrip('-_')
187 chunk.slug = other.slug
189 chunk.title = ("%s, %s" % (other_title_part, chunk.title))[:255]
191 chunk.slug = slugs[i]
192 chunk.title = titles[i]
194 chunk.slug = self.make_chunk_slug(chunk.slug)
197 assert not other.chunk_set.exists()
199 gm = GalleryMerger(self.gallery, other.gallery)
200 self.gallery = gm.merge()
202 # and move the gallery starts
204 for chunk in self[len(self) - len_other:]:
205 chunk.gallery_start += gm.dest_size - gm.num_deleted
211 @transaction.commit_on_success
212 def prepend_history(self, other):
213 """Prepend history from all the other book's chunks to own."""
216 for i in range(len(self), len(other)):
217 title = u"pusta część %d" % i
218 chunk = self.add(slughifi(title), title)
221 for i in range(len(other)):
222 self[i].prepend_history(other[0])
224 assert not other.chunk_set.exists()
228 """Splits all the chunks into separate books."""
231 book = Book.objects.create(title=chunk.title, slug=chunk.slug,
232 public=self.public, gallery=self.gallery)
237 assert not self.chunk_set.exists()
243 def last_published(self):
245 return self.publish_log.all()[0].timestamp
249 def assert_publishable(self):
250 assert self.chunk_set.exists(), _('No chunks in the book.')
252 changes = self.get_current_changes(publishable=True)
253 except self.NoTextError:
254 raise AssertionError(_('Not all chunks have publishable revisions.'))
255 book_xml = self.materialize(changes=changes)
257 from librarian.dcparser import BookInfo
258 from librarian import NoDublinCore, ParseError, ValidationError
261 bi = BookInfo.from_string(book_xml.encode('utf-8'), strict=True)
262 except ParseError, e:
263 raise AssertionError(_('Invalid XML') + ': ' + unicode(e))
265 raise AssertionError(_('No Dublin Core found.'))
266 except ValidationError, e:
267 raise AssertionError(_('Invalid Dublin Core') + ': ' + unicode(e))
269 valid_about = self.correct_about()
270 assert bi.about == valid_about, _("rdf:about is not") + " " + valid_about
273 return self.slug.startswith('.')
275 def is_new_publishable(self):
276 """Checks if book is ready for publishing.
278 Returns True if there is a publishable version newer than the one
282 new_publishable = False
283 if not self.chunk_set.exists():
286 change = chunk.publishable()
289 if not new_publishable and not change.publish_log.exists():
290 new_publishable = True
291 return new_publishable
292 new_publishable = cached_in_field('_new_publishable')(is_new_publishable)
294 def is_published(self):
295 return self.publish_log.exists()
296 published = cached_in_field('_published')(is_published)
298 def get_on_track(self):
301 stages = [ch.stage.ordering if ch.stage is not None else 0
306 on_track = cached_in_field('_on_track')(get_on_track)
309 return len(self) == 1
310 single = cached_in_field('_single')(is_single)
312 @cached_in_field('_short_html')
313 def short_html(self):
314 return render_to_string('catalogue/book_list/book.html', {'book': self})
316 def book_info(self, publishable=True):
318 book_xml = self.materialize(publishable=publishable)
319 except self.NoTextError:
322 from librarian.dcparser import BookInfo
323 from librarian import NoDublinCore, ParseError, ValidationError
325 return BookInfo.from_string(book_xml.encode('utf-8'))
326 except (self.NoTextError, ParseError, NoDublinCore, ValidationError):
329 def refresh_dc_cache(self):
334 info = self.book_info()
336 update['dc_slug'] = info.url.slug
337 Book.objects.filter(pk=self.pk).update(**update)
340 # this should only really be done when text or publishable status changes
341 book_content_updated.delay(self)
344 "_new_publishable": self.is_new_publishable(),
345 "_published": self.is_published(),
346 "_single": self.is_single(),
347 "_on_track": self.get_on_track(),
350 Book.objects.filter(pk=self.pk).update(**update)
351 refresh_instance(self)
354 """This should be done offline."""
360 # Materializing & publishing
361 # ==========================
363 def get_current_changes(self, publishable=True):
365 Returns a list containing one Change for every Chunk in the Book.
366 Takes the most recent revision (publishable, if set).
367 Throws an error, if a proper revision is unavailable for a Chunk.
370 changes = [chunk.publishable() for chunk in self]
372 changes = [chunk.head for chunk in self if chunk.head is not None]
374 raise self.NoTextError('Some chunks have no available text.')
377 def materialize(self, publishable=False, changes=None):
379 Get full text of the document compiled from chunks.
380 Takes the current versions of all texts
381 or versions most recently tagged for publishing,
382 or a specified iterable changes.
385 changes = self.get_current_changes(publishable)
386 return compile_text(change.materialize() for change in changes)
388 def wldocument(self, publishable=True, changes=None, parse_dublincore=True):
389 from catalogue.ebook_utils import RedakcjaDocProvider
390 from librarian.parser import WLDocument
392 return WLDocument.from_string(
393 self.materialize(publishable=publishable, changes=changes),
394 provider=RedakcjaDocProvider(publishable=publishable),
395 parse_dublincore=parse_dublincore)
397 def publish(self, user):
399 Publishes a book on behalf of a (local) user.
401 self.assert_publishable()
402 changes = self.get_current_changes(publishable=True)
403 book_xml = self.materialize(changes=changes)
404 apiclient.api_call(user, "books/", {"book_xml": book_xml})
406 br = BookPublishRecord.objects.create(book=self, user=user)
408 ChunkPublishRecord.objects.create(book_record=br, change=c)
409 post_publish.send(sender=br)