X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/c661073dd0969caa9a47a225f3d3fae767295c3d..dfd584e3b136d770bf56569030d10712a8722569:/apps/catalogue/models/tag.py diff --git a/apps/catalogue/models/tag.py b/apps/catalogue/models/tag.py index 353d567bb..bff993248 100644 --- a/apps/catalogue/models/tag.py +++ b/apps/catalogue/models/tag.py @@ -2,10 +2,12 @@ # 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 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 @@ -17,13 +19,13 @@ TAG_CATEGORIES = ( ('genre', _('genre')), ('theme', _('theme')), ('set', _('set')), - ('book', _('book')), + ('thing', _('thing')), # things shown on pictures ) class Tag(TagBase): """A tag attachable to books and fragments (and possibly anything). - + Used to represent searchable metadata (authors, epochs, genres, kinds), fragment themes (motifs) and some book hierarchy related kludges.""" name = models.CharField(_('name'), max_length=50, db_index=True) @@ -34,12 +36,12 @@ class Tag(TagBase): description = models.TextField(_('description'), blank=True) user = models.ForeignKey(User, blank=True, null=True) - book_count = models.IntegerField(_('book 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) - changed_at = models.DateTimeField(_('creation date'), auto_now=True, db_index=True) + created_at = models.DateTimeField(_('creation date'), auto_now_add=True, db_index=True) + changed_at = models.DateTimeField(_('creation date'), auto_now=True, db_index=True) class UrlDeprecationWarning(DeprecationWarning): pass @@ -51,6 +53,7 @@ class Tag(TagBase): 'gatunek': 'genre', 'motyw': 'theme', 'polka': 'set', + 'obiekt': 'thing', } categories_dict = dict((item[::-1] for item in categories_rev.iteritems())) @@ -71,30 +74,18 @@ class Tag(TagBase): def get_absolute_url(self): return ('catalogue.views.tagged_object_list', [self.url_chunk]) + @classmethod + @permalink + def create_url(cls, category, slug): + return ('catalogue.views.tagged_object_list', [ + '/'.join((cls.categories_dict[category], slug)) + ]) + def has_description(self): return len(self.description) > 0 has_description.short_description = _('description') has_description.boolean = True - def get_count(self): - """Returns global book count for book tags, fragment count for themes.""" - from catalogue.models import Book, Fragment - - if self.category == 'book': - # never used - objects = Book.objects.none() - elif self.category == 'theme': - objects = Fragment.tagged.with_all((self,)) - else: - objects = Book.tagged.with_all((self,)).order_by() - if self.category != 'set': - # eliminate descendants - l_tags = Tag.objects.filter(slug__in=[book.book_tag_slug() for book in objects.iterator()]) - descendants_keys = [book.pk for book in Book.tagged.with_any(l_tags).iterator()] - if descendants_keys: - objects = objects.exclude(pk__in=descendants_keys) - return objects.count() - @staticmethod def get_tag_list(tags): if isinstance(tags, basestring): @@ -111,8 +102,8 @@ class Tag(TagBase): category = Tag.categories_rev[name] else: try: - real_tags.append(Tag.objects.exclude(category='book').get(slug=name)) - deprecated = True + real_tags.append(Tag.objects.get(slug=name)) + deprecated = True except Tag.MultipleObjectsReturned, e: ambiguous_slugs.append(name) @@ -139,7 +130,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')) @@ -153,14 +144,32 @@ 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 + + +# Pickle complains about not having this. +TagRelation = Tag.intermediary_table_model \ No newline at end of file