Fixes #4188: Ordering editing phases directly at the phases list page.
[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 ugettext_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.NullBooleanField(editable=False)
27     _changed = models.NullBooleanField(editable=False)
28     _new_publishable = models.NullBooleanField(editable=False)
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     # Representing
43     # ============
44
45     def __str__(self):
46         return "%d:%d: %s" % (self.book_id, self.number, self.title)
47
48     def get_absolute_url(self):
49         return reverse("wiki_editor", args=[self.book.slug, self.slug])
50
51     def pretty_name(self, book_length=None):
52         title = self.book.title
53         if self.title:
54             title += ", %s" % self.title
55         if book_length and book_length > 1:
56             title += " (%d/%d)" % (self.number, book_length)
57         return title
58
59     # Creating and manipulation
60     # =========================
61
62     def split(self, slug, title='', **kwargs):
63         """ Create an empty chunk after this one """
64         self.book.chunk_set.filter(number__gt=self.number).update(
65                 number=models.F('number')+1)
66         new_chunk = None
67         while not new_chunk:
68             new_slug = self.book.make_chunk_slug(slug)
69             try:
70                 new_chunk = self.book.chunk_set.create(
71                     number=self.number+1,
72                     slug=new_slug[:50], title=title[:255], **kwargs)
73             except IntegrityError:
74                 pass
75         return new_chunk
76
77     @classmethod
78     def get(cls, book_slug, chunk_slug=None):
79         if chunk_slug is None:
80             return cls.objects.get(book__slug=book_slug, number=1)
81         else:
82             return cls.objects.get(book__slug=book_slug, slug=chunk_slug)
83
84     # State & cache
85     # =============
86
87     def is_new_publishable(self):
88         change = self.publishable()
89         if not change:
90             return False
91         return not change.publish_log.exists()
92     new_publishable = cached_in_field('_new_publishable')(is_new_publishable)
93
94     def is_changed(self):
95         if self.head is None:
96             return False
97         return not self.head.publishable
98     changed = cached_in_field('_changed')(is_changed)
99
100     def is_hidden(self):
101         return self.book.hidden()
102     hidden = cached_in_field('_hidden')(is_hidden)
103
104     def touch(self):
105         update = {
106             "_changed": self.is_changed(),
107             "_new_publishable": self.is_new_publishable(),
108             "_hidden": self.is_hidden(),
109         }
110         Chunk.objects.filter(pk=self.pk).update(**update)