Allow books with internationalized metadata.
[wolnelektury.git] / apps / catalogue / models / tag.py
index 353d567..1309cbb 100644 (file)
@@ -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,18 @@ 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):
+        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')
@@ -139,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'))
@@ -153,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