X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/b2d342589a7889a3b096e7192453d53bd28eed7d..8f031335a8a0dabceec381b89eb0cbbcb2f4b637:/src/pdcounter/models.py?ds=inline diff --git a/src/pdcounter/models.py b/src/pdcounter/models.py index 69c70c1c0..2e1e0b90e 100644 --- a/src/pdcounter/models.py +++ b/src/pdcounter/models.py @@ -1,13 +1,14 @@ -# -*- coding: utf-8 -*- # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # +from django.apps import apps from django.conf import settings from django.db import models -from django.db.models import permalink -from django.utils.translation import ugettext_lazy as _ +from django.urls import reverse +from django.utils.translation import gettext_lazy as _ from datetime import datetime from django.db.models.signals import post_save, post_delete +from search.utils import UnaccentSearchVector class Author(models.Model): @@ -15,7 +16,7 @@ class Author(models.Model): slug = models.SlugField(_('slug'), max_length=120, db_index=True, unique=True) sort_key = models.CharField(_('sort key'), max_length=120, db_index=True) description = models.TextField(_('description'), blank=True) - death = models.IntegerField(_(u'year of death'), blank=True, null=True) + death = models.IntegerField(_('year of death'), blank=True, null=True) gazeta_link = models.CharField(blank=True, max_length=240) wiki_link = models.CharField(blank=True, max_length=240) @@ -28,21 +29,32 @@ class Author(models.Model): def category(self): return "author" - def __unicode__(self): + def __str__(self): return self.name def __repr__(self): return "Author(slug=%r)" % self.slug - @permalink def get_absolute_url(self): - return 'catalogue.views.tagged_object_list', [self.url_chunk] + return reverse('tagged_object_list', args=[self.url_chunk]) def has_description(self): return len(self.description) > 0 has_description.short_description = _('description') has_description.boolean = True + @classmethod + def search(cls, query, qs=None): + Tag = apps.get_model('catalogue', 'Tag') + if qs is None: + qs = cls.objects.all() + pd_authors = qs.annotate(search_vector=UnaccentSearchVector('name')).filter(search_vector=query) + existing_slugs = Tag.objects.filter( + category='author', slug__in=list(pd_authors.values_list('slug', flat=True))) \ + .values_list('slug', flat=True) + pd_authors = pd_authors.exclude(slug__in=existing_slugs) + return pd_authors + def alive(self): return self.death is None @@ -71,12 +83,23 @@ class BookStub(models.Model): verbose_name = _('book stub') verbose_name_plural = _('book stubs') - def __unicode__(self): + def __str__(self): return self.title - @permalink + @classmethod + def search(cls, query, qs=None): + Book = apps.get_model('catalogue', 'Book') + if qs is None: + qs = cls.objects.all() + pd_books = qs.annotate(search_vector=UnaccentSearchVector('title')).filter(search_vector=query) + existing_slugs = Book.objects.filter( + slug__in=list(pd_books.values_list('slug', flat=True))) \ + .values_list('slug', flat=True) + pd_books = pd_books.exclude(slug__in=existing_slugs) + return pd_books + def get_absolute_url(self): - return 'catalogue.views.book_detail', [self.slug] + return reverse('book_detail', args=[self.slug]) def in_pd(self): return self.pd is not None and self.pd <= datetime.now().year