X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/d157af1061e9f03f59ea909d7d25f4a0b41f1c0e..1f9103b1e752a6c41d2304bf5e41a7d6ae0c45c8:/apps/catalogue/models/tag.py diff --git a/apps/catalogue/models/tag.py b/apps/catalogue/models/tag.py index 3c4509dda..5b99b8f0b 100644 --- a/apps/catalogue/models/tag.py +++ b/apps/catalogue/models/tag.py @@ -3,9 +3,10 @@ # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # from django.contrib.auth.models import User +from django.core.exceptions import ValidationError from django.db import models from django.db.models import permalink -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import ugettext, ugettext_lazy as _ from newtagging.models import TagBase @@ -18,6 +19,7 @@ TAG_CATEGORIES = ( ('theme', _('theme')), ('set', _('set')), ('book', _('book')), + ('thing', _('thing')), # things shown on pictures ) @@ -35,6 +37,7 @@ class Tag(TagBase): user = models.ForeignKey(User, blank=True, null=True) book_count = models.IntegerField(_('book count'), blank=True, null=True) + picture_count = models.IntegerField(_('picture count'), blank=True, null=True) gazeta_link = models.CharField(blank=True, max_length=240) wiki_link = models.CharField(blank=True, max_length=240) @@ -51,6 +54,7 @@ class Tag(TagBase): 'gatunek': 'genre', 'motyw': 'theme', 'polka': 'set', + 'obiekt': 'thing', } categories_dict = dict((item[::-1] for item in categories_rev.iteritems())) @@ -71,6 +75,11 @@ class Tag(TagBase): def get_absolute_url(self): return ('catalogue.views.tagged_object_list', [self.url_chunk]) + def clean(self): + if self.category == 'book' and (self.gazeta_link or self.wiki_link): + raise ValidationError(ugettext( + u"Book tags can't have attached links. Set them directly on the book instead of it's tag.")) + @classmethod @permalink def create_url(cls, category, slug): @@ -102,6 +111,22 @@ class Tag(TagBase): objects = objects.exclude(pk__in=descendants_keys) return objects.count() + # I shouldn't break the get_count() api + # just to include pictures. + def get_picture_count(self): + from picture.models import Picture + + if self.category == 'book': + # never used + objects = Book.objects.none() + elif self.category == 'theme': + objects = Picture.tagged.with_all((self,)) + elif self.category == 'thing': + objects = Picture.tagged.with_all((self,)) + else: + objects = Picture.tagged.with_all((self,)).order_by() + return objects.count() + @staticmethod def get_tag_list(tags): if isinstance(tags, basestring): @@ -146,7 +171,7 @@ class Tag(TagBase): @staticmethod def tags_from_info(info): - from slughifi import slughifi + from fnpdjango.utils.text.slughifi import slughifi from sortify import sortify meta_tags = [] categories = (('kinds', 'kind'), ('genres', 'genre'), ('authors', 'author'), ('epochs', 'epoch'))