X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/2ff7d6d98d7ba61ce305fb2906bddaa37f320eb1..40b28c6172db2f68bd9199024bb4e27e6741ded4:/apps/catalogue/models/tag.py diff --git a/apps/catalogue/models/tag.py b/apps/catalogue/models/tag.py index b0c75fbb4..acfb9d8f3 100644 --- a/apps/catalogue/models/tag.py +++ b/apps/catalogue/models/tag.py @@ -2,6 +2,7 @@ # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # +from django.conf import settings from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.db import models @@ -19,6 +20,7 @@ TAG_CATEGORIES = ( ('theme', _('theme')), ('set', _('set')), ('book', _('book')), + ('thing', _('thing')), # things shown on pictures ) @@ -36,7 +38,9 @@ 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) + culturepl_link = models.CharField(blank=True, max_length=240) wiki_link = models.CharField(blank=True, max_length=240) created_at = models.DateTimeField(_('creation date'), auto_now_add=True, db_index=True) @@ -52,6 +56,7 @@ class Tag(TagBase): 'gatunek': 'genre', 'motyw': 'theme', 'polka': 'set', + 'obiekt': 'thing', } categories_dict = dict((item[::-1] for item in categories_rev.iteritems())) @@ -108,6 +113,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, PictureArea + + if self.category == 'book': + # never used + objects = Picture.objects.none() + elif self.category == 'theme': + objects = PictureArea.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): @@ -166,14 +187,28 @@ class Tag(TagBase): # For instance, Pictures do not have 'genre' field. continue for tag_name in tag_names: + lang = getattr(tag_name, 'lang', settings.LANGUAGE_CODE) tag_sort_key = tag_name if category == 'author': tag_sort_key = tag_name.last_name tag_name = tag_name.readable() - tag, created = Tag.objects.get_or_create(slug=slughifi(tag_name), category=category) - if created: - tag.name = tag_name - tag.sort_key = sortify(tag_sort_key.lower()) - tag.save() - meta_tags.append(tag) + if lang == settings.LANGUAGE_CODE: + # Allow creating new tag, if it's in default language. + tag, created = Tag.objects.get_or_create(slug=slughifi(tag_name), category=category) + if created: + tag_name = unicode(tag_name) + tag.name = tag_name + setattr(tag, "name_%s" % lang, tag_name) + tag.sort_key = sortify(tag_sort_key.lower()) + tag.save() + + meta_tags.append(tag) + else: + # Ignore unknown tags in non-default languages. + try: + tag = Tag.objects.get(category=category, **{"name_%s" % lang: tag_name}) + except Tag.DoesNotExist: + pass + else: + meta_tags.append(tag) return meta_tags