1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.conf import settings
6 from django.contrib.contenttypes.fields import GenericRelation
7 from django.core.urlresolvers import reverse
8 from django.db import models
9 from django.utils.translation import ugettext_lazy as _
10 from newtagging import managers
11 from catalogue.models import Tag
12 from ssify import flush_ssi_includes
15 class Fragment(models.Model):
16 """Represents a themed fragment of a book."""
17 text = models.TextField()
18 short_text = models.TextField(editable=False)
19 anchor = models.CharField(max_length=120)
20 book = models.ForeignKey('Book', related_name='fragments')
22 objects = models.Manager()
23 tagged = managers.ModelTaggedItemManager(Tag)
24 tags = managers.TagDescriptor(Tag)
25 tag_relations = GenericRelation(Tag.intermediary_table_model)
27 short_html_url_name = 'catalogue_fragment_short'
30 ordering = ('book', 'anchor',)
31 verbose_name = _('fragment')
32 verbose_name_plural = _('fragments')
33 app_label = 'catalogue'
35 def get_absolute_url(self):
36 return '%s#m%s' % (reverse('book_text', args=[self.book.slug]), self.anchor)
38 def get_api_url(self):
39 return reverse('catalogue_api_fragment', args=[self.book.slug, self.anchor])
41 def get_short_text(self):
42 """Returns short version of the fragment."""
43 return self.short_text if self.short_text else self.text
47 return self.tags.filter(category='theme')
49 def flush_includes(self, languages=True):
53 languages = [lc for (lc, _ln) in settings.LANGUAGES]
55 template % (self.pk, lang)
57 '/katalog/f/%d/short.%s.html',
58 '/api/include/fragment/%d.%s.json',
59 '/api/include/fragment/%d.%s.xml',