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