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.db.models import permalink
8 from django.utils.translation import ugettext_lazy as _
9 from datetime import datetime
10 from django.db.models.signals import post_save, post_delete
13 class Author(models.Model):
14 name = models.CharField(_('name'), max_length=50, db_index=True)
15 slug = models.SlugField(_('slug'), max_length=120, db_index=True, unique=True)
16 sort_key = models.CharField(_('sort key'), max_length=120, db_index=True)
17 description = models.TextField(_('description'), blank=True)
18 death = models.IntegerField(_(u'year of death'), blank=True, null=True)
19 gazeta_link = models.CharField(blank=True, max_length=240)
20 wiki_link = models.CharField(blank=True, max_length=240)
23 ordering = ('sort_key',)
24 verbose_name = _('author')
25 verbose_name_plural = _('authors')
31 def __unicode__(self):
35 return "Author(slug=%r)" % self.slug
38 def get_absolute_url(self):
39 return 'catalogue.views.tagged_object_list', [self.url_chunk]
41 def has_description(self):
42 return len(self.description) > 0
43 has_description.short_description = _('description')
44 has_description.boolean = True
47 return self.death is None
50 """ tests whether an author is in public domain """
51 return self.death is not None and self.goes_to_pd() <= datetime.now().year
54 """ calculates the year of public domain entry for an author """
55 return self.death + 71 if self.death is not None else None
59 return '/'.join(('autor', self.slug))
62 class BookStub(models.Model):
63 title = models.CharField(_('title'), max_length=120)
64 author = models.CharField(_('author'), max_length=120)
65 pd = models.IntegerField(_('goes to public domain'), null=True, blank=True)
66 slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
67 translator = models.TextField(_('translator'), blank=True)
71 verbose_name = _('book stub')
72 verbose_name_plural = _('book stubs')
74 def __unicode__(self):
78 def get_absolute_url(self):
79 return 'catalogue.views.book_detail', [self.slug]
82 return self.pd is not None and self.pd <= datetime.now().year
88 def pretty_title(self, html_links=False):
89 return ', '.join((self.author, self.title))
92 if not settings.NO_SEARCH_INDEX:
93 def update_index(sender, instance, **kwargs):
94 from search.index import Index
96 idx.index_tags(instance, remove_only='created' not in kwargs)
98 post_delete.connect(update_index, Author)
99 post_delete.connect(update_index, BookStub)
100 post_save.connect(update_index, Author)
101 post_save.connect(update_index, BookStub)