fix revisions and diff
[redakcja.git] / apps / catalogue / models / chunk.py
1 # -*- coding: utf-8 -*-
2 #
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.
5 #
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 catalogue.tasks import refresh_instance
14 from dvcs import models as dvcs_models
15
16
17 class Chunk(dvcs_models.Document):
18     """ An editable chunk of text. Every Book text is divided into chunks. """
19     REPO_PATH = settings.CATALOGUE_REPO_PATH
20
21     book = models.ForeignKey('Book', editable=False, verbose_name=_('book'))
22     number = models.IntegerField(_('number'))
23     slug = models.SlugField(_('slug'))
24     title = models.CharField(_('title'), max_length=255, blank=True)
25
26     # cache
27     _short_html = models.TextField(null=True, blank=True, editable=False)
28     _hidden = models.NullBooleanField(editable=False)
29     _changed = models.NullBooleanField(editable=False)
30
31     # managers
32     objects = models.Manager()
33     visible_objects = VisibleManager()
34
35     class Meta:
36         app_label = 'catalogue'
37         unique_together = [['book', 'number'], ['book', 'slug']]
38         ordering = ['number']
39         verbose_name = _('chunk')
40         verbose_name_plural = _('chunks')
41
42     # Representing
43     # ============
44
45     def __unicode__(self):
46         return "%d:%d: %s" % (self.book_id, self.number, self.title)
47
48     @models.permalink
49     def get_absolute_url(self):
50         return ("wiki_editor", [self.book.slug, self.slug])
51
52     def pretty_name(self, book_length=None):
53         title = self.book.title
54         if self.title:
55             title += ", %s" % self.title
56         if book_length > 1:
57             title += " (%d/%d)" % (self.number, book_length)
58         return title
59
60
61     # Creating and manipulation
62     # =========================
63
64     def split(self, slug, title='', adjust_slug=False, **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)
68         new_chunk = None
69         while not new_chunk:
70             new_slug = self.book.make_chunk_slug(slug)
71             try:
72                 new_chunk = self.book.chunk_set.create(number=self.number+1,
73                     slug=new_slug[:50], title=title[:255], **kwargs)
74             except IntegrityError:
75                 pass
76         return new_chunk
77
78     @classmethod
79     def get(cls, book_slug, chunk_slug=None):
80         if chunk_slug is None:
81             return cls.objects.get(book__slug=book_slug, number=1)
82         else:
83             return cls.objects.get(book__slug=book_slug, slug=chunk_slug)
84
85
86     # State & cache
87     # =============
88
89     def new_publishable(self):
90         change = self.publishable()
91         if not change:
92             return False
93         return change.publish_log.exists()
94
95     def is_changed(self):
96         if self.head is None:
97             return False
98         return not self.head.publishable
99     changed = cached_in_field('_changed')(is_changed)
100
101     def is_hidden(self):
102         return self.book.hidden()
103     hidden = cached_in_field('_hidden')(is_hidden)
104
105     @cached_in_field('_short_html')
106     def short_html(self):
107         return render_to_string(
108                     'catalogue/book_list/chunk.html', {'chunk': self})
109
110     def touch(self):
111         update = {
112             "_changed": self.is_changed(),
113             "_hidden": self.is_hidden(),
114             "_short_html": None,
115         }
116         Chunk.objects.filter(pk=self.pk).update(**update)
117         refresh_instance(self)
118
119     def refresh(self):
120         """This should be done offline."""
121         self.changed
122         self.hidden
123         self.short_html