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.
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 catalogue.helpers import cached_in_field
11 from catalogue.managers import VisibleManager
12 from dvcs import models as dvcs_models
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
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)
26 _hidden = models.NullBooleanField(editable=False)
27 _changed = models.NullBooleanField(editable=False)
28 _new_publishable = models.NullBooleanField(editable=False)
31 objects = models.Manager()
32 visible_objects = VisibleManager()
35 app_label = 'catalogue'
36 unique_together = [['book', 'number'], ['book', 'slug']]
38 verbose_name = _('chunk')
39 verbose_name_plural = _('chunks')
40 permissions = [('can_pubmark', 'Can mark for publishing')]
46 return "%d:%d: %s" % (self.book_id, self.number, self.title)
48 def get_absolute_url(self):
49 return reverse("wiki_editor", args=[self.book.slug, self.slug])
51 def pretty_name(self, book_length=None):
52 title = self.book.title
54 title += ", %s" % self.title
55 if book_length and book_length > 1:
56 title += " (%d/%d)" % (self.number, book_length)
59 # Creating and manipulation
60 # =========================
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)
68 new_slug = self.book.make_chunk_slug(slug)
70 new_chunk = self.book.chunk_set.create(
72 slug=new_slug[:50], title=title[:255], **kwargs)
73 except IntegrityError:
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)
82 return cls.objects.get(book__slug=book_slug, slug=chunk_slug)
87 def is_new_publishable(self):
88 change = self.publishable()
91 return not change.publish_log.exists()
92 new_publishable = cached_in_field('_new_publishable')(is_new_publishable)
97 return not self.head.publishable
98 changed = cached_in_field('_changed')(is_changed)
101 return self.book.hidden()
102 hidden = cached_in_field('_hidden')(is_hidden)
106 "_changed": self.is_changed(),
107 "_new_publishable": self.is_new_publishable(),
108 "_hidden": self.is_hidden(),
110 Chunk.objects.filter(pk=self.pk).update(**update)