1 # -*- coding: utf-8 -*-
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.
6 from django.conf import settings
7 from django.db import models
8 from django.db.utils import IntegrityError
9 from django.template.loader import render_to_string
10 from django.utils.translation import ugettext_lazy as _
11 from catalogue.helpers import cached_in_field
12 from catalogue.managers import VisibleManager
13 from dvcs import models as dvcs_models
16 class Chunk(dvcs_models.Document):
17 """ An editable chunk of text. Every Book text is divided into chunks. """
18 REPO_PATH = settings.CATALOGUE_REPO_PATH
20 book = models.ForeignKey('Book', editable=False, verbose_name=_('book'))
21 number = models.IntegerField(_('number'))
22 title = models.CharField(_('title'), max_length=255, blank=True)
23 slug = models.SlugField(_('slug'))
24 gallery_start = models.IntegerField(_('gallery start'), null=True, blank=True, default=1)
27 _hidden = models.NullBooleanField(editable=False)
28 _changed = models.NullBooleanField(editable=False)
29 _new_publishable = models.NullBooleanField(editable=False)
32 objects = models.Manager()
33 visible_objects = VisibleManager()
36 app_label = 'catalogue'
37 unique_together = [['book', 'number'], ['book', 'slug']]
39 verbose_name = _('chunk')
40 verbose_name_plural = _('chunks')
41 permissions = [('can_pubmark', 'Can mark for publishing')]
46 def __unicode__(self):
47 return "%d:%d: %s" % (self.book_id, self.number, self.title)
50 def get_absolute_url(self):
51 return "wiki_editor", [self.book.slug, self.slug]
53 def pretty_name(self, book_length=None):
54 title = self.book.title
56 title += ", %s" % self.title
58 title += " (%d/%d)" % (self.number, book_length)
61 # Creating and manipulation
62 # =========================
64 def split(self, slug, title='', **kwargs):
65 """ Create an empty chunk after this one """
66 self.book.chunk_set.filter(number__gt=self.number).update(
67 number=models.F('number')+1)
70 new_slug = self.book.make_chunk_slug(slug)
72 new_chunk = self.book.chunk_set.create(
74 slug=new_slug[:50], title=title[:255], **kwargs)
75 except IntegrityError:
80 def get(cls, book_slug, chunk_slug=None):
81 if chunk_slug is None:
82 return cls.objects.get(book__slug=book_slug, number=1)
84 return cls.objects.get(book__slug=book_slug, slug=chunk_slug)
89 def is_new_publishable(self):
90 change = self.publishable()
93 return not change.publish_log.exists()
94 new_publishable = cached_in_field('_new_publishable')(is_new_publishable)
99 return not self.head.publishable
100 changed = cached_in_field('_changed')(is_changed)
103 return self.book.hidden()
104 hidden = cached_in_field('_hidden')(is_hidden)
108 "_changed": self.is_changed(),
109 "_new_publishable": self.is_new_publishable(),
110 "_hidden": self.is_hidden(),
112 Chunk.objects.filter(pk=self.pk).update(**update)