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 _
8 from datetime import datetime
9 from django.db.models.signals import post_save, post_delete
12 class Author(models.Model):
13 name = models.CharField(_('name'), max_length=50, db_index=True)
14 slug = models.SlugField(_('slug'), max_length=120, db_index=True, unique=True)
15 sort_key = models.CharField(_('sort key'), max_length=120, db_index=True)
16 description = models.TextField(_('description'), blank=True)
17 death = models.IntegerField(_('year of death'), blank=True, null=True)
18 gazeta_link = models.CharField(blank=True, max_length=240)
19 wiki_link = models.CharField(blank=True, max_length=240)
22 ordering = ('sort_key',)
23 verbose_name = _('author')
24 verbose_name_plural = _('authors')
34 return "Author(slug=%r)" % self.slug
36 def get_absolute_url(self):
37 return reverse('tagged_object_list', args=[self.url_chunk])
39 def has_description(self):
40 return len(self.description) > 0
41 has_description.short_description = _('description')
42 has_description.boolean = True
45 return self.death is None
48 """ tests whether an author is in public domain """
49 return self.death is not None and self.goes_to_pd() <= datetime.now().year
52 """ calculates the year of public domain entry for an author """
53 return self.death + 71 if self.death is not None else None
57 return '/'.join(('autor', self.slug))
60 class BookStub(models.Model):
61 title = models.CharField(_('title'), max_length=120)
62 author = models.CharField(_('author'), max_length=120)
63 pd = models.IntegerField(_('goes to public domain'), null=True, blank=True)
64 slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
65 translator = models.TextField(_('translator'), blank=True)
69 verbose_name = _('book stub')
70 verbose_name_plural = _('book stubs')
75 def get_absolute_url(self):
76 return reverse('book_detail', args=[self.slug])
79 return self.pd is not None and self.pd <= datetime.now().year
85 def pretty_title(self, html_links=False):
86 return ', '.join((self.author, self.title))
89 if not settings.NO_SEARCH_INDEX:
90 def update_index(sender, instance, **kwargs):
91 from search.index import Index
93 idx.index_tags(instance, remove_only='created' not in kwargs)
95 post_delete.connect(update_index, Author)
96 post_delete.connect(update_index, BookStub)
97 post_save.connect(update_index, Author)
98 post_save.connect(update_index, BookStub)