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 as _
 
   9 from datetime import datetime
 
  10 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(_(u'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')
 
  30     def __unicode__(self):
 
  34         return "Author(slug=%r)" % self.slug
 
  37     def get_absolute_url(self):
 
  38         return ('catalogue.views.tagged_object_list', [self.url_chunk])
 
  40     def has_description(self):
 
  41         return len(self.description) > 0
 
  42     has_description.short_description = _('description')
 
  43     has_description.boolean = True
 
  46         return self.death is None
 
  49         """ tests whether an author is in public domain """
 
  50         return self.death is not None and self.goes_to_pd() <= datetime.now().year
 
  53         """ calculates the year of public domain entry for an author """
 
  54         return self.death + 71 if self.death is not None else None
 
  58         return '/'.join(('autor', self.slug))
 
  61 class BookStub(models.Model):
 
  62     title = models.CharField(_('title'), max_length=120)
 
  63     author = models.CharField(_('author'), max_length=120)
 
  64     pd = models.IntegerField(_('goes to public domain'), null=True, blank=True)
 
  65     slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
 
  66     translator = models.TextField(_('translator'), blank=True)
 
  70         verbose_name = _('book stub')
 
  71         verbose_name_plural = _('book stubs')
 
  73     def __unicode__(self):
 
  77     def get_absolute_url(self):
 
  78         return ('catalogue.views.book_detail', [self.slug])
 
  81         return self.pd is not None and self.pd <= datetime.now().year
 
  87     def pretty_title(self, html_links=False):
 
  88         return ', '.join((self.author, self.title))
 
  91 if not settings.NO_SEARCH_INDEX:
 
  92     def update_index(sender, instance, **kwargs):
 
  94         print "update pd index %s [update %s]" % (instance, 'created' in kwargs)
 
  98             idx.index_tags(instance, remove_only=not 'created' in kwargs)
 
 102     post_delete.connect(update_index, Author)
 
 103     post_delete.connect(update_index, BookStub)
 
 104     post_save.connect(update_index, Author)
 
 105     post_save.connect(update_index, BookStub)