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.apps import apps
5 from django.conf import settings
6 from django.db import models
7 from django.urls import reverse
8 from django.utils.translation import gettext_lazy as _
9 from datetime import datetime
10 from django.db.models.signals import post_save, post_delete
11 from search.utils import UnaccentSearchVector
14 class Author(models.Model):
15 name = models.CharField(_('name'), max_length=50, db_index=True)
16 slug = models.SlugField(_('slug'), max_length=120, db_index=True, unique=True)
17 sort_key = models.CharField(_('sort key'), max_length=120, db_index=True)
18 description = models.TextField(_('description'), blank=True)
19 death = models.IntegerField(_('year of death'), blank=True, null=True)
20 gazeta_link = models.CharField(blank=True, max_length=240)
21 wiki_link = models.CharField(blank=True, max_length=240)
24 ordering = ('sort_key',)
25 verbose_name = _('author')
26 verbose_name_plural = _('authors')
36 return "Author(slug=%r)" % self.slug
38 def get_absolute_url(self):
39 return reverse('tagged_object_list', args=[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 def search(cls, query, qs=None):
48 Tag = apps.get_model('catalogue', 'Tag')
50 qs = cls.objects.all()
51 pd_authors = qs.annotate(search_vector=UnaccentSearchVector('name')).filter(search_vector=query)
52 existing_slugs = Tag.objects.filter(
53 category='author', slug__in=list(pd_authors.values_list('slug', flat=True))) \
54 .values_list('slug', flat=True)
55 pd_authors = pd_authors.exclude(slug__in=existing_slugs)
59 return self.death is None
62 """ tests whether an author is in public domain """
63 return self.death is not None and self.goes_to_pd() <= datetime.now().year
66 """ calculates the year of public domain entry for an author """
67 return self.death + 71 if self.death is not None else None
71 return '/'.join(('autor', self.slug))
74 class BookStub(models.Model):
75 title = models.CharField(_('title'), max_length=120)
76 author = models.CharField(_('author'), max_length=120)
77 pd = models.IntegerField(_('goes to public domain'), null=True, blank=True)
78 slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
79 translator = models.TextField(_('translator'), blank=True)
83 verbose_name = _('book stub')
84 verbose_name_plural = _('book stubs')
90 def search(cls, query, qs=None):
91 Book = apps.get_model('catalogue', 'Book')
93 qs = cls.objects.all()
94 pd_books = qs.annotate(search_vector=UnaccentSearchVector('title')).filter(search_vector=query)
95 existing_slugs = Book.objects.filter(
96 slug__in=list(pd_books.values_list('slug', flat=True))) \
97 .values_list('slug', flat=True)
98 pd_books = pd_books.exclude(slug__in=existing_slugs)
101 def get_absolute_url(self):
102 return reverse('book_detail', args=[self.slug])
105 return self.pd is not None and self.pd <= datetime.now().year
111 def pretty_title(self, html_links=False):
112 return ', '.join((self.author, self.title))