-# -*- coding: utf-8 -*-
-#
# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
from django.db import models
from django.db.utils import IntegrityError
from django.template.loader import render_to_string
+from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
from catalogue.helpers import cached_in_field
from catalogue.managers import VisibleManager
-from catalogue.tasks import refresh_instance
from dvcs import models as dvcs_models
""" An editable chunk of text. Every Book text is divided into chunks. """
REPO_PATH = settings.CATALOGUE_REPO_PATH
- book = models.ForeignKey('Book', editable=False, verbose_name=_('book'))
+ book = models.ForeignKey('Book', models.CASCADE, editable=False, verbose_name=_('book'))
number = models.IntegerField(_('number'))
title = models.CharField(_('title'), max_length=255, blank=True)
slug = models.SlugField(_('slug'))
gallery_start = models.IntegerField(_('gallery start'), null=True, blank=True, default=1)
# cache
- _short_html = models.TextField(null=True, blank=True, editable=False)
_hidden = models.NullBooleanField(editable=False)
_changed = models.NullBooleanField(editable=False)
+ _new_publishable = models.NullBooleanField(editable=False)
# managers
objects = models.Manager()
# Representing
# ============
- def __unicode__(self):
+ def __str__(self):
return "%d:%d: %s" % (self.book_id, self.number, self.title)
- @models.permalink
def get_absolute_url(self):
- return "wiki_editor", [self.book.slug, self.slug]
+ return reverse("wiki_editor", args=[self.book.slug, self.slug])
def pretty_name(self, book_length=None):
title = self.book.title
if self.title:
title += ", %s" % self.title
- if book_length > 1:
+ if book_length and book_length > 1:
title += " (%d/%d)" % (self.number, book_length)
return title
# State & cache
# =============
- def new_publishable(self):
+ def is_new_publishable(self):
change = self.publishable()
if not change:
return False
return not change.publish_log.exists()
+ new_publishable = cached_in_field('_new_publishable')(is_new_publishable)
def is_changed(self):
if self.head is None:
return self.book.hidden()
hidden = cached_in_field('_hidden')(is_hidden)
- @cached_in_field('_short_html')
- def short_html(self):
- return render_to_string(
- 'catalogue/book_list/chunk.html', {'chunk': self})
-
def touch(self):
update = {
"_changed": self.is_changed(),
+ "_new_publishable": self.is_new_publishable(),
"_hidden": self.is_hidden(),
- "_short_html": None,
}
Chunk.objects.filter(pk=self.pk).update(**update)
- refresh_instance(self)
-
- def refresh(self):
- """This should be done offline."""
- self.changed
- self.hidden
- self.short_html