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.db import models
7 from django.utils.translation import ugettext_lazy as _
8 from ssify import flush_ssi_includes
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'), null=True, blank=True)
18 models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
19 book_slugs = models.TextField(_('book slugs'))
21 kind = models.CharField(_('kind'), max_length=10, blank=False, default='book', db_index=True,
22 choices=(('book', _('book')), ('picture', _('picture'))))
26 verbose_name = _('collection')
27 verbose_name_plural = _('collections')
28 app_label = 'catalogue'
30 def __unicode__(self):
33 def get_initial(self):
35 return re.search(r'\w', self.title, re.U).group(0)
36 except AttributeError:
40 def get_absolute_url(self):
41 return "collection", [self.slug]
44 slugs = self.book_slugs.split()
47 slugs = [slug.rstrip('/').rsplit('/', 1)[-1] if '/' in slug else slug for slug in slugs]
48 return models.Q(slug__in=slugs)
51 from catalogue.models import Book
52 return Book.objects.filter(self.get_query()).order_by('sort_key_author', 'sort_key')
54 def flush_includes(self, languages=True):
58 languages = [lc for (lc, _ln) in settings.LANGUAGES]
61 '/katalog/%s.json' % lang for lang in languages])