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.db import models
6 from django.urls import reverse
7 from django.utils.translation import gettext_lazy as _
9 from wolnelektury.utils import cached_render, clear_cached_renders
12 class Collection(models.Model):
13 """A collection of books, which might be defined before publishing them."""
14 title = models.CharField(_('title'), max_length=120, db_index=True)
15 slug = models.SlugField(_('slug'), max_length=120, primary_key=True)
16 description = models.TextField(_('description'), blank=True)
17 book_slugs = models.TextField(_('book slugs'))
18 authors = models.ManyToManyField(
20 limit_choices_to={'category': 'author'},
23 kind = models.CharField(_('kind'), max_length=10, blank=False, default='book', db_index=True,
24 choices=(('book', _('book')), ('picture', _('picture'))))
25 listed = models.BooleanField(_('listed'), default=True, db_index=True)
26 role = models.CharField(max_length=128, blank=True, db_index=True, choices=[
28 ('recommend', _('recommended')),
33 verbose_name = _('collection')
34 verbose_name_plural = _('collections')
35 app_label = 'catalogue'
40 def get_initial(self):
42 return re.search(r'\w', self.title, re.U).group(0)
43 except AttributeError:
46 def get_absolute_url(self):
47 return reverse("collection", args=[self.slug])
50 slugs = self.book_slugs.split()
52 slugs = [slug.rstrip('/').rsplit('/', 1)[-1] if '/' in slug else slug for slug in slugs]
53 return models.Q(slug__in=slugs)
56 from catalogue.models import Book
57 return Book.objects.filter(self.get_query())
59 @cached_render('catalogue/collection_box.html')
65 def clear_cache(self):
66 clear_cached_renders(self.box)