tags wired to pictures
[wolnelektury.git] / apps / catalogue / models / collection.py
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.
4 #
5 from django.db import models
6 from django.utils.translation import ugettext_lazy as _
7
8
9 class Collection(models.Model):
10     """A collection of books, which might be defined before publishing them."""
11     title = models.CharField(_('title'), max_length=120, db_index=True)
12     slug = models.SlugField(_('slug'), max_length=120, primary_key=True)
13     description = models.TextField(_('description'), null=True, blank=True)
14
15     models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
16     book_slugs = models.TextField(_('book slugs'))
17
18     kind = models.CharField(_('kind'), max_length=10, blank=False, default='book', db_index=True, choices=((('book'), _('book')), (('picture'), ('picture'))))
19
20     class Meta:
21         ordering = ('title',)
22         verbose_name = _('collection')
23         verbose_name_plural = _('collections')
24         app_label = 'catalogue'
25
26     def __unicode__(self):
27         return self.title
28
29     @models.permalink
30     def get_absolute_url(self):
31         return ("collection", [self.slug])
32
33     def get_query(self):
34         slugs = self.book_slugs.split()
35         # allow URIs
36         slugs = [slug.rstrip('/').rsplit('/', 1)[-1] if '/' in slug else slug
37                     for slug in slugs]
38         return models.Q(slug__in=slugs)