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 documents.helpers import cached_in_field
11 from documents.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.BooleanField(editable=False, null=True)
27 _changed = models.BooleanField(editable=False, null=True)
28 _new_publishable = models.BooleanField(editable=False, null=True)
31 objects = models.Manager()
32 visible_objects = VisibleManager()
35 app_label = 'documents'
36 unique_together = [['book', 'number'], ['book', 'slug']]
38 verbose_name = _('chunk')
39 verbose_name_plural = _('chunks')
40 permissions = [('can_pubmark', 'Can mark for publishing')]
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)
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)
60 return "%d:%d: %s" % (self.book_id, self.number, self.title)
62 def get_absolute_url(self):
63 return reverse("wiki_editor", args=[self.book.slug, self.slug])
65 def pretty_name(self, book_length=None):
66 title = self.book.title
68 title += ", %s" % self.title
69 if book_length and book_length > 1:
70 title += " (%d/%d)" % (self.number, book_length)
73 # Creating and manipulation
74 # =========================
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)
82 new_slug = self.book.make_chunk_slug(slug)
84 new_chunk = self.book.chunk_set.create(
86 slug=new_slug[:50], title=title[:255], **kwargs)
87 except IntegrityError:
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)
96 return cls.objects.get(book__slug=book_slug, slug=chunk_slug)
101 def is_new_publishable(self):
102 change = self.publishable()
105 return not change.publish_log.exists()
106 new_publishable = cached_in_field('_new_publishable')(is_new_publishable)
108 def is_changed(self):
109 if self.head is None:
111 return not self.head.publishable
112 changed = cached_in_field('_changed')(is_changed)
115 return self.book.hidden()
116 hidden = cached_in_field('_hidden')(is_hidden)
120 "_changed": self.is_changed(),
121 "_new_publishable": self.is_new_publishable(),
122 "_hidden": self.is_hidden(),
124 Chunk.objects.filter(pk=self.pk).update(**update)