1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.conf import settings
6 from django.contrib.auth.models import User
7 from django.db import models
8 from django.db.models import permalink
9 from django.utils.translation import ugettext_lazy as _
10 from newtagging.models import TagBase
13 # Those are hard-coded here so that makemessages sees them.
15 ('author', _('author')),
16 ('epoch', _('epoch')),
18 ('genre', _('genre')),
19 ('theme', _('theme')),
21 ('thing', _('thing')), # things shown on pictures
26 """A tag attachable to books and fragments (and possibly anything).
28 Used to represent searchable metadata (authors, epochs, genres, kinds),
29 fragment themes (motifs) and some book hierarchy related kludges."""
30 name = models.CharField(_('name'), max_length=50, db_index=True)
31 slug = models.SlugField(_('slug'), max_length=120, db_index=True)
32 sort_key = models.CharField(_('sort key'), max_length=120, db_index=True)
33 category = models.CharField(_('category'), max_length=50, blank=False, null=False,
34 db_index=True, choices=TAG_CATEGORIES)
35 description = models.TextField(_('description'), blank=True)
37 user = models.ForeignKey(User, blank=True, null=True)
38 gazeta_link = models.CharField(blank=True, max_length=240)
39 culturepl_link = models.CharField(blank=True, max_length=240)
40 wiki_link = models.CharField(blank=True, max_length=240)
42 created_at = models.DateTimeField(_('creation date'), auto_now_add=True, db_index=True)
43 changed_at = models.DateTimeField(_('creation date'), auto_now=True, db_index=True)
45 class UrlDeprecationWarning(DeprecationWarning):
57 categories_dict = dict((item[::-1] for item in categories_rev.iteritems()))
60 ordering = ('sort_key',)
61 verbose_name = _('tag')
62 verbose_name_plural = _('tags')
63 unique_together = (("slug", "category"),)
64 app_label = 'catalogue'
66 def __unicode__(self):
70 return "Tag(slug=%r)" % self.slug
73 def get_absolute_url(self):
74 return ('catalogue.views.tagged_object_list', [self.url_chunk])
78 def create_url(cls, category, slug):
79 return ('catalogue.views.tagged_object_list', [
80 '/'.join((cls.categories_dict[category], slug))
83 def has_description(self):
84 return len(self.description) > 0
85 has_description.short_description = _('description')
86 has_description.boolean = True
89 def get_tag_list(tags):
90 if isinstance(tags, basestring):
95 tags_splitted = tags.split('/')
96 for name in tags_splitted:
98 real_tags.append(Tag.objects.get(slug=name, category=category))
100 elif name in Tag.categories_rev:
101 category = Tag.categories_rev[name]
104 real_tags.append(Tag.objects.get(slug=name))
106 except Tag.MultipleObjectsReturned, e:
107 ambiguous_slugs.append(name)
110 # something strange left off
111 raise Tag.DoesNotExist()
113 # some tags should be qualified
114 e = Tag.MultipleObjectsReturned()
116 e.ambiguous_slugs = ambiguous_slugs
119 e = Tag.UrlDeprecationWarning()
124 return TagBase.get_tag_list(tags)
128 return '/'.join((Tag.categories_dict[self.category], self.slug))
131 def tags_from_info(info):
132 from fnpdjango.utils.text.slughifi import slughifi
133 from sortify import sortify
135 categories = (('kinds', 'kind'), ('genres', 'genre'), ('authors', 'author'), ('epochs', 'epoch'))
136 for field_name, category in categories:
138 tag_names = getattr(info, field_name)
141 tag_names = [getattr(info, category)]
143 # For instance, Pictures do not have 'genre' field.
145 for tag_name in tag_names:
146 lang = getattr(tag_name, 'lang', settings.LANGUAGE_CODE)
147 tag_sort_key = tag_name
148 if category == 'author':
149 tag_sort_key = tag_name.last_name
150 tag_name = tag_name.readable()
151 if lang == settings.LANGUAGE_CODE:
152 # Allow creating new tag, if it's in default language.
153 tag, created = Tag.objects.get_or_create(slug=slughifi(tag_name), category=category)
155 tag_name = unicode(tag_name)
157 setattr(tag, "name_%s" % lang, tag_name)
158 tag.sort_key = sortify(tag_sort_key.lower())
161 meta_tags.append(tag)
163 # Ignore unknown tags in non-default languages.
165 tag = Tag.objects.get(category=category, **{"name_%s" % lang: tag_name})
166 except Tag.DoesNotExist:
169 meta_tags.append(tag)
173 # Pickle complains about not having this.
174 TagRelation = Tag.intermediary_table_model