ebeb9aeff58da8f234d7ebad28f39aeab4eac676
[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):
167         """Add all chunks of another book to self."""
168         number = self[len(self) - 1].number + 1
169         single = len(other) == 1
170         for chunk in other:
171             # move chunk to new book
172             chunk.book = self
173             chunk.number = number
174
175             # try some title guessing
176             if other.title.startswith(self.title):
177                 other_title_part = other.title[len(self.title):].lstrip(' /')
178             else:
179                 other_title_part = other.title
180
181             if single:
182                 # special treatment for appending one-parters:
183                 # just use the guessed title and original book slug
184                 chunk.comment = other_title_part
185                 if other.slug.startswith(self.slug):
186                     chunk_slug = other.slug[len(self.slug):].lstrip('-_')
187                 else:
188                     chunk_slug = other.slug
189                 chunk.slug = self.make_chunk_slug(chunk_slug)
190             else:
191                 chunk.comment = "%s, %s" % (other_title_part, chunk.comment)
192                 chunk.slug = self.make_chunk_slug(chunk.slug)
193             chunk.save()
194             number += 1
195         other.delete()
196
197     def add(self, *args, **kwargs):
198         """Add a new chunk at the end."""
199         return self.chunk_set.reverse()[0].split(*args, **kwargs)
200
201     @staticmethod
202     def listener_create(sender, instance, created, **kwargs):
203         if created:
204             instance.chunk_set.create(number=1, slug='1')
205
206 models.signals.post_save.connect(Book.listener_create, sender=Book)
207
208
209 class Chunk(dvcs_models.Document):
210     """ An editable chunk of text. Every Book text is divided into chunks. """
211
212     book = models.ForeignKey(Book, editable=False)
213     number = models.IntegerField()
214     slug = models.SlugField()
215     comment = models.CharField(max_length=255, blank=True)
216
217     class Meta:
218         unique_together = [['book', 'number'], ['book', 'slug']]
219         ordering = ['number']
220
221     def __unicode__(self):
222         return "%d-%d: %s" % (self.book_id, self.number, self.comment)
223
224     def get_absolute_url(self):
225         return reverse("wiki_editor", args=[self.book.slug, self.slug])
226
227     @classmethod
228     def get(cls, slug, chunk=None):
229         if chunk is None:
230             return cls.objects.get(book__slug=slug, number=1)
231         else:
232             return cls.objects.get(book__slug=slug, slug=chunk)
233
234     def pretty_name(self, book_length=None):
235         title = self.book.title
236         if self.comment:
237             title += ", %s" % self.comment
238         if book_length > 1:
239             title += " (%d/%d)" % (self.number, book_length)
240         return title
241
242     def split(self, slug, comment='', creator=None, adjust_slug=False):
243         """ Create an empty chunk after this one """
244         self.book.chunk_set.filter(number__gt=self.number).update(
245                 number=models.F('number')+1)
246         tries = 1
247         new_slug = slug
248         new_chunk = None
249         while not new_chunk:
250             try:
251                 new_chunk = self.book.chunk_set.create(number=self.number+1,
252                     creator=creator, slug=new_slug, comment=comment)
253             except IntegrityError:
254                 if not adjust_slug:
255                     raise
256                 new_slug = "%s_%d" % (slug, tries)
257                 tries += 1
258         return new_chunk
259
260     @staticmethod
261     def listener_saved(sender, instance, created, **kwargs):
262         if instance.book:
263             # save book so that its _list_html is reset
264             instance.book.save()
265
266 models.signals.post_save.connect(Chunk.listener_saved, sender=Chunk)
267
268
269 class BookPublishRecord(models.Model):
270     """
271         A record left after publishing a Book.
272     """
273
274     book = models.ForeignKey(Book)
275     timestamp = models.DateTimeField(auto_now_add=True)
276     user = models.ForeignKey(User)
277
278     class Meta:
279         ordering = ['-timestamp']
280
281
282 class ChunkPublishRecord(models.Model):
283     """
284         BookPublishRecord details for each Chunk.
285     """
286
287     book_record = models.ForeignKey(BookPublishRecord)
288     change = models.ForeignKey(Chunk.change_model)