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