69d0a0d3d57d24f861b929597d0c7f750c22b79c
[redakcja.git] / apps / catalogue / models.py
1 # -*- coding: utf-8 -*-
2 #
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.
5 #
6 from django.contrib.auth.models import User
7 from django.core.urlresolvers import reverse
8 from django.db import models
9 from django.utils.translation import ugettext_lazy as _
10 from django.db.utils import IntegrityError
11
12 from slughifi import slughifi
13
14 from dvcs import models as dvcs_models
15 from catalogue.xml_tools import compile_text, split_xml
16
17 import logging
18 logger = logging.getLogger("fnp.catalogue")
19
20
21 class Book(models.Model):
22     """ A document edited on the wiki """
23
24     title = models.CharField(_('title'), max_length=255, db_index=True)
25     slug = models.SlugField(_('slug'), max_length=128, unique=True, db_index=True)
26     gallery = models.CharField(_('scan gallery name'), max_length=255, blank=True)
27
28     parent = models.ForeignKey('self', null=True, blank=True, verbose_name=_('parent'), related_name="children")
29     parent_number = models.IntegerField(_('parent number'), null=True, blank=True, db_index=True)
30
31     class NoTextError(BaseException):
32         pass
33
34     class Meta:
35         ordering = ['parent_number', 'title']
36         verbose_name = _('book')
37         verbose_name_plural = _('books')
38         permissions = [('can_pubmark', 'Can mark for publishing')]
39
40     def __unicode__(self):
41         return self.title
42
43     def get_absolute_url(self):
44         return reverse("catalogue_book", args=[self.slug])
45
46     @classmethod
47     def import_xml_text(cls, text=u'', creator=None, previous_book=None,
48                 *args, **kwargs):
49
50         texts = split_xml(text)
51         if previous_book:
52             instance = previous_book
53         else:
54             instance = cls(*args, **kwargs)
55             instance.save()
56
57         # if there are more parts, set the rest to empty strings
58         book_len = len(instance)
59         for i in range(book_len - len(texts)):
60             texts.append(u'pusta część %d' % (i + 1), u'')
61
62         i = 0
63         for i, (title, text) in enumerate(texts):
64             if not title:
65                 title = u'część %d' % (i + 1)
66
67             slug = slughifi(title)
68
69             if i < book_len:
70                 chunk = instance[i]
71                 chunk.slug = slug
72                 chunk.comment = title
73                 chunk.save()
74             else:
75                 chunk = instance.add(slug, title, creator, adjust_slug=True)
76
77             chunk.commit(text, author=creator)
78
79         return instance
80
81     @classmethod
82     def create(cls, creator=None, text=u'', *args, **kwargs):
83         """
84             >>> Book.create(slug='x', text='abc').materialize()
85             'abc'
86         """
87         instance = cls(*args, **kwargs)
88         instance.save()
89         instance[0].commit(text, author=creator)
90         return instance
91
92     def __iter__(self):
93         return iter(self.chunk_set.all())
94
95     def __getitem__(self, chunk):
96         return self.chunk_set.all()[chunk]
97
98     def __len__(self):
99         return self.chunk_set.count()
100
101     def __nonzero__(self):
102         """
103             Necessary so that __len__ isn't used for bool evaluation.
104         """
105         return True
106
107     def get_current_changes(self, publishable=True):
108         """
109             Returns a list containing one Change for every Chunk in the Book.
110             Takes the most recent revision (publishable, if set).
111             Throws an error, if a proper revision is unavailable for a Chunk.
112         """
113         if publishable:
114             changes = [chunk.publishable() for chunk in self]
115         else:
116             changes = [chunk.head for chunk in self if chunk.head is not None]
117         if None in changes:
118             raise self.NoTextError('Some chunks have no available text.')
119         return changes
120
121     def materialize(self, publishable=False, changes=None):
122         """ 
123             Get full text of the document compiled from chunks.
124             Takes the current versions of all texts
125             or versions most recently tagged for publishing,
126             or a specified iterable changes.
127         """
128         if changes is None:
129             changes = self.get_current_changes(publishable)
130         return compile_text(change.materialize() for change in changes)
131
132     def publishable(self):
133         if not self.chunk_set.exists():
134             return False
135         for chunk in self:
136             if not chunk.publishable():
137                 return False
138         return True
139
140     def publish(self, user):
141         """
142             Publishes a book on behalf of a (local) user.
143         """
144         from apiclient import api_call
145
146         changes = self.get_current_changes(publishable=True)
147         book_xml = book.materialize(changes=changes)
148         #api_call(user, "books", {"book_xml": book_xml})
149         # record the publish
150         br = BookPublishRecord.objects.create(book=self, user=user)
151         for c in changes:
152             ChunkPublishRecord.objects.create(book_record=br, change=c)
153
154     def make_chunk_slug(self, proposed):
155         """ 
156             Finds a chunk slug not yet used in the book.
157         """
158         slugs = set(c.slug for c in self)
159         i = 1
160         new_slug = proposed
161         while new_slug in slugs:
162             new_slug = "%s_%d" % (proposed, i)
163             i += 1
164         return new_slug
165
166     def append(self, other, slugs=None, titles=None):
167         """Add all chunks of another book to self."""
168         number = self[len(self) - 1].number + 1
169         len_other = len(other)
170         single = len_other == 1
171
172         if slugs is not None:
173             assert len(slugs) == len_other
174         if titles is not None:
175             assert len(titles) == len_other
176             if slugs is None:
177                 slugs = [slughifi(t) for t in titles]
178
179         for i, chunk in enumerate(other):
180             # move chunk to new book
181             chunk.book = self
182             chunk.number = number
183
184             if titles is None:
185                 # try some title guessing
186                 if other.title.startswith(self.title):
187                     other_title_part = other.title[len(self.title):].lstrip(' /')
188                 else:
189                     other_title_part = other.title
190
191                 if single:
192                     # special treatment for appending one-parters:
193                     # just use the guessed title and original book slug
194                     chunk.comment = other_title_part
195                     if other.slug.startswith(self.slug):
196                         chunk_slug = other.slug[len(self.slug):].lstrip('-_')
197                     else:
198                         chunk_slug = other.slug
199                     chunk.slug = self.make_chunk_slug(chunk_slug)
200                 else:
201                     chunk.comment = "%s, %s" % (other_title_part, chunk.comment)
202             else:
203                 chunk.slug = slugs[i]
204                 chunk.comment = titles[i]
205
206             chunk.slug = self.make_chunk_slug(chunk.slug)
207             chunk.save()
208             number += 1
209         other.delete()
210
211     def add(self, *args, **kwargs):
212         """Add a new chunk at the end."""
213         return self.chunk_set.reverse()[0].split(*args, **kwargs)
214
215     @staticmethod
216     def listener_create(sender, instance, created, **kwargs):
217         if created:
218             instance.chunk_set.create(number=1, slug='1')
219
220 models.signals.post_save.connect(Book.listener_create, sender=Book)
221
222
223 class Chunk(dvcs_models.Document):
224     """ An editable chunk of text. Every Book text is divided into chunks. """
225
226     book = models.ForeignKey(Book, editable=False)
227     number = models.IntegerField()
228     slug = models.SlugField()
229     comment = models.CharField(max_length=255, blank=True)
230
231     class Meta:
232         unique_together = [['book', 'number'], ['book', 'slug']]
233         ordering = ['number']
234
235     def __unicode__(self):
236         return "%d-%d: %s" % (self.book_id, self.number, self.comment)
237
238     def get_absolute_url(self):
239         return reverse("wiki_editor", args=[self.book.slug, self.slug])
240
241     @classmethod
242     def get(cls, slug, chunk=None):
243         if chunk is None:
244             return cls.objects.get(book__slug=slug, number=1)
245         else:
246             return cls.objects.get(book__slug=slug, slug=chunk)
247
248     def pretty_name(self, book_length=None):
249         title = self.book.title
250         if self.comment:
251             title += ", %s" % self.comment
252         if book_length > 1:
253             title += " (%d/%d)" % (self.number, book_length)
254         return title
255
256     def split(self, slug, comment='', creator=None, adjust_slug=False):
257         """ Create an empty chunk after this one """
258         self.book.chunk_set.filter(number__gt=self.number).update(
259                 number=models.F('number')+1)
260         new_chunk = None
261         while not new_chunk:
262             new_slug = self.book.make_chunk_slug(slug)
263             try:
264                 new_chunk = self.book.chunk_set.create(number=self.number+1,
265                     creator=creator, slug=new_slug, comment=comment)
266             except IntegrityError:
267                 pass
268         return new_chunk
269
270     @staticmethod
271     def listener_saved(sender, instance, created, **kwargs):
272         if instance.book:
273             # save book so that its _list_html is reset
274             instance.book.save()
275
276 models.signals.post_save.connect(Chunk.listener_saved, sender=Chunk)
277
278
279 class BookPublishRecord(models.Model):
280     """
281         A record left after publishing a Book.
282     """
283
284     book = models.ForeignKey(Book)
285     timestamp = models.DateTimeField(auto_now_add=True)
286     user = models.ForeignKey(User)
287
288     class Meta:
289         ordering = ['-timestamp']
290
291
292 class ChunkPublishRecord(models.Model):
293     """
294         BookPublishRecord details for each Chunk.
295     """
296
297     book_record = models.ForeignKey(BookPublishRecord)
298     change = models.ForeignKey(Chunk.change_model)