1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. 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 newtagging import managers
9 from catalogue.models import Tag
10 from wolnelektury.utils import cached_render, clear_cached_renders
13 class Fragment(models.Model):
14 """Represents a themed fragment of a book."""
15 text = models.TextField()
16 short_text = models.TextField(editable=False)
17 anchor = models.CharField(max_length=120)
18 book = models.ForeignKey('Book', models.CASCADE, related_name='fragments')
20 objects = models.Manager()
21 tagged = managers.ModelTaggedItemManager(Tag)
22 tags = managers.TagDescriptor(Tag)
23 tag_relations = GenericRelation(Tag.intermediary_table_model)
26 ordering = ('book', 'anchor',)
27 verbose_name = 'fragment'
28 verbose_name_plural = 'fragmenty'
29 app_label = 'catalogue'
31 def get_absolute_url(self):
32 return '%s#m%s' % (reverse('book_text', args=[self.book.slug]), self.anchor)
34 def get_api_url(self):
35 return reverse('catalogue_api_fragment', args=[self.book.slug, self.anchor])
37 def get_short_text(self):
38 """Returns short version of the fragment."""
39 return self.short_text if self.short_text else self.text
43 return self.tags.filter(category='theme')