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.db import models
6 from django.urls import reverse
8 from wolnelektury.utils import cached_render, clear_cached_renders
11 class Collection(models.Model):
12 """A collection of books, which might be defined before publishing them."""
13 title = models.CharField('tytuł', max_length=120, db_index=True)
14 slug = models.SlugField('slug', max_length=120, primary_key=True)
15 description = models.TextField('opis', blank=True)
16 book_slugs = models.TextField('slugi książek')
17 authors = models.ManyToManyField(
19 limit_choices_to={'category': 'author'},
22 listed = models.BooleanField('na liście', default=True, db_index=True)
23 role = models.CharField(max_length=128, blank=True, db_index=True, choices=[
25 ('recommend', 'polecane'),
30 verbose_name = 'kolekcja'
31 verbose_name_plural = 'kolekcje'
32 app_label = 'catalogue'
37 def get_initial(self):
39 return re.search(r'\w', self.title, re.U).group(0)
40 except AttributeError:
43 def get_absolute_url(self):
44 return reverse("collection", args=[self.slug])
47 slugs = self.book_slugs.split()
49 slugs = [slug.rstrip('/').rsplit('/', 1)[-1] if '/' in slug else slug for slug in slugs]
50 return models.Q(slug__in=slugs)
53 from catalogue.models import Book
54 return Book.objects.filter(self.get_query())
56 def get_5_books(self):
57 return self.get_books()[:5]
60 return self.get_books()[:3]
62 def clear_cache(self):
63 clear_cached_renders(self.box)