X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/d36673cef5c5f35b1043a03e6c5ddbb7688788ae..1ca64302e8447c9715ab42dd1a33075f312e6059:/src/catalogue/models/tag.py diff --git a/src/catalogue/models/tag.py b/src/catalogue/models/tag.py index 89ca65185..06aa7a1d5 100644 --- a/src/catalogue/models/tag.py +++ b/src/catalogue/models/tag.py @@ -9,6 +9,7 @@ from django.db import models from django.db.models import permalink from django.dispatch import Signal from django.utils.translation import ugettext_lazy as _ + from newtagging.models import TagBase from ssify import flush_ssi_includes @@ -21,7 +22,7 @@ TAG_CATEGORIES = ( ('genre', _('genre')), ('theme', _('theme')), ('set', _('set')), - ('thing', _('thing')), # things shown on pictures + ('thing', _('thing')), # things shown on pictures ) @@ -33,8 +34,8 @@ class Tag(TagBase): name = models.CharField(_('name'), max_length=120, db_index=True) slug = models.SlugField(_('slug'), max_length=120, db_index=True) sort_key = models.CharField(_('sort key'), max_length=120, db_index=True) - category = models.CharField(_('category'), max_length=50, blank=False, null=False, - db_index=True, choices=TAG_CATEGORIES) + category = models.CharField( + _('category'), max_length=50, blank=False, null=False, db_index=True, choices=TAG_CATEGORIES) description = models.TextField(_('description'), blank=True) user = models.ForeignKey(User, blank=True, null=True) @@ -48,7 +49,9 @@ class Tag(TagBase): after_change = Signal(providing_args=['instance', 'languages']) class UrlDeprecationWarning(DeprecationWarning): - pass + def __init__(self, tags=None): + super(Tag.UrlDeprecationWarning, self).__init__() + self.tags = tags categories_rev = { 'autor': 'author', @@ -138,18 +141,18 @@ class Tag(TagBase): @permalink def get_absolute_url(self): - return ('tagged_object_list', [self.url_chunk]) + return 'tagged_object_list', [self.url_chunk] @permalink def get_absolute_gallery_url(self): - return ('tagged_object_list_gallery', [self.url_chunk]) + return 'tagged_object_list_gallery', [self.url_chunk] @classmethod @permalink def create_url(cls, category, slug): return ('catalogue.views.tagged_object_list', [ - '/'.join((cls.categories_dict[category], slug)) - ]) + '/'.join((cls.categories_dict[category], slug)) + ]) def has_description(self): return len(self.description) > 0 @@ -159,7 +162,8 @@ class Tag(TagBase): @staticmethod def get_tag_list(tags): if isinstance(tags, basestring): - if not tags: return [] + if not tags: + return [] real_tags = [] ambiguous_slugs = [] category = None @@ -175,7 +179,7 @@ class Tag(TagBase): try: real_tags.append(Tag.objects.get(slug=name)) deprecated = True - except Tag.MultipleObjectsReturned, e: + except Tag.MultipleObjectsReturned: ambiguous_slugs.append(name) if category: @@ -188,9 +192,7 @@ class Tag(TagBase): e.ambiguous_slugs = ambiguous_slugs raise e if deprecated: - e = Tag.UrlDeprecationWarning() - e.tags = real_tags - raise e + raise Tag.UrlDeprecationWarning(tags=real_tags) return real_tags else: return TagBase.get_tag_list(tags) @@ -208,17 +210,17 @@ class Tag(TagBase): for field_name, category in categories: try: tag_names = getattr(info, field_name) - except: + except KeyError: try: tag_names = [getattr(info, category)] - except: + except KeyError: # 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_sort_key = ' '.join((tag_name.last_name,) + tag_name.first_names) tag_name = tag_name.readable() if lang == settings.LANGUAGE_CODE: # Allow creating new tag, if it's in default language.