Epub debugging.
[redakcja.git] / src / documents / models / chunk.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.conf import settings
5 from django.db import models
6 from django.db.utils import IntegrityError
7 from django.template.loader import render_to_string
8 from django.urls import reverse
9 from django.utils.translation import gettext_lazy as _
10 from documents.helpers import cached_in_field
11 from documents.managers import VisibleManager
12 from dvcs import models as dvcs_models
13
14
15 class Chunk(dvcs_models.Document):
16     """ An editable chunk of text. Every Book text is divided into chunks. """
17     REPO_PATH = settings.CATALOGUE_REPO_PATH
18
19     book = models.ForeignKey('Book', models.CASCADE, editable=False, verbose_name=_('book'))
20     number = models.IntegerField(_('number'))
21     title = models.CharField(_('title'), max_length=255, blank=True)
22     slug = models.SlugField(_('slug'))
23     gallery_start = models.IntegerField(_('gallery start'), null=True, blank=True, default=1)
24
25     # cache
26     _hidden = models.BooleanField(editable=False, null=True)
27     _changed = models.BooleanField(editable=False, null=True)
28     _new_publishable = models.BooleanField(editable=False, null=True)
29
30     # managers
31     objects = models.Manager()
32     visible_objects = VisibleManager()
33
34     class Meta:
35         app_label = 'documents'
36         unique_together = [['book', 'number'], ['book', 'slug']]
37         ordering = ['number']
38         verbose_name = _('chunk')
39         verbose_name_plural = _('chunks')
40         permissions = [('can_pubmark', 'Can mark for publishing')]
41
42     @classmethod
43     def get_visible_for(cls, user):
44         qs = cls.objects.all()
45         if not user.is_authenticated:
46             qs = qs.filter(book__public=True)
47         return qs
48
49     @classmethod
50     def get_revisions_visible_for(cls, user):
51         qs = cls.change_model.objects.all()
52         if not user.is_authenticated:
53             qs = qs.filter(tree__book__public=True)
54         return qs
55     
56     # Representing
57     # ============
58
59     def __str__(self):
60         return "%d:%d: %s" % (self.book_id, self.number, self.title)
61
62     def get_absolute_url(self):
63         return reverse("wiki_editor", args=[self.book.slug, self.slug])
64
65     def pretty_name(self, book_length=None):
66         title = self.book.title
67         if self.title:
68             title += ", %s" % self.title
69         if book_length and book_length > 1:
70             title += " (%d/%d)" % (self.number, book_length)
71         return title
72
73     # Creating and manipulation
74     # =========================
75
76     def split(self, slug, title='', **kwargs):
77         """ Create an empty chunk after this one """
78         self.book.chunk_set.filter(number__gt=self.number).update(
79                 number=models.F('number')+1)
80         new_chunk = None
81         while not new_chunk:
82             new_slug = self.book.make_chunk_slug(slug)
83             try:
84                 new_chunk = self.book.chunk_set.create(
85                     number=self.number+1,
86                     slug=new_slug[:50], title=title[:255], **kwargs)
87             except IntegrityError:
88                 pass
89         return new_chunk
90
91     @classmethod
92     def get(cls, book_slug, chunk_slug=None):
93         if chunk_slug is None:
94             return cls.objects.get(book__slug=book_slug, number=1)
95         else:
96             return cls.objects.get(book__slug=book_slug, slug=chunk_slug)
97
98     # State & cache
99     # =============
100
101     def is_new_publishable(self):
102         change = self.publishable()
103         if not change:
104             return False
105         return not change.publish_log.exists()
106     new_publishable = cached_in_field('_new_publishable')(is_new_publishable)
107
108     def is_changed(self):
109         if self.head is None:
110             return False
111         return not self.head.publishable
112     changed = cached_in_field('_changed')(is_changed)
113
114     def is_hidden(self):
115         return self.book.hidden()
116     hidden = cached_in_field('_hidden')(is_hidden)
117
118     def touch(self):
119         update = {
120             "_changed": self.is_changed(),
121             "_new_publishable": self.is_new_publishable(),
122             "_hidden": self.is_hidden(),
123         }
124         Chunk.objects.filter(pk=self.pk).update(**update)