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