X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/c6db46d42f0a6b9dbd5abb6ce2be58fe306752b6..c7c0b88f1f628f0498c8c53c76a061ec128a228a:/apps/catalogue/models/tag.py diff --git a/apps/catalogue/models/tag.py b/apps/catalogue/models/tag.py index 3c4509dda..1309cbbd2 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 @@ -71,6 +73,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): @@ -146,7 +153,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')) @@ -160,14 +167,26 @@ 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 = 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