# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
from django.conf import settings
+from django.core.cache import caches
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, ugettext_lazy as _
+from django.dispatch import Signal
+from django.utils.translation import ugettext_lazy as _
from newtagging.models import TagBase
+from ssify import flush_ssi_includes
# Those are hard-coded here so that makemessages sees them.
('genre', _('genre')),
('theme', _('theme')),
('set', _('set')),
- ('book', _('book')),
('thing', _('thing')), # things shown on pictures
)
description = models.TextField(_('description'), blank=True)
user = models.ForeignKey(User, blank=True, null=True)
- book_count = models.IntegerField(_('book count'), blank=True, null=True)
- picture_count = models.IntegerField(_('picture 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)
+ after_change = Signal(providing_args=['instance', 'languages'])
+
class UrlDeprecationWarning(DeprecationWarning):
pass
unique_together = (("slug", "category"),)
app_label = 'catalogue'
+ def save(self, *args, **kwargs):
+ flush_cache = flush_all_includes = False
+ if self.pk and self.category != 'set':
+ # Flush the whole views cache.
+ # Seem a little harsh, but changed tag names, descriptions
+ # and links come up at any number of places.
+ flush_cache = True
+
+ # Find in which languages we need to flush related includes.
+ old_self = type(self).objects.get(pk=self.pk)
+ # Category shouldn't normally be changed, but just in case.
+ if self.category != old_self.category:
+ flush_all_includes = True
+ languages_changed = self.languages_changed(old_self)
+
+ ret = super(Tag, self).save(*args, **kwargs)
+
+ if flush_cache:
+ caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
+ if flush_all_includes:
+ flush_ssi_includes()
+ else:
+ self.flush_includes()
+ self.after_change.send(sender=type(self), instance=self, languages=languages_changed)
+
+ return ret
+
+ def languages_changed(self, old):
+ all_langs = [lc for (lc, _ln) in settings.LANGUAGES]
+ if (old.category, old.slug) != (self.category, self.slug):
+ return all_langs
+ languages = set()
+ for lang in all_langs:
+ name_field = 'name_%s' % lang
+ if getattr(old, name_field) != getattr(self, name_field):
+ languages.add(lang)
+ return languages
+
+ def flush_includes(self, languages=True):
+ if not languages:
+ return
+ if languages is True:
+ languages = [lc for (lc, _ln) in settings.LANGUAGES]
+ flush_ssi_includes([
+ template % (self.pk, lang)
+ for template in [
+ '/api/include/tag/%d.%s.json',
+ '/api/include/tag/%d.%s.xml',
+ ]
+ for lang in languages
+ ])
+ flush_ssi_includes([
+ '/katalog/%s.json' % lang for lang in languages])
+
def __unicode__(self):
return self.name
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):
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()
-
- # I shouldn't break the get_count() api
- # just to include pictures.
- def get_picture_count(self):
- from picture.models import Picture, PictureArea
-
- if self.category == 'book':
- # never used
- objects = Picture.objects.none()
- elif self.category == 'theme':
- objects = PictureArea.tagged.with_all((self,))
- elif self.category == 'thing':
- objects = Picture.tagged.with_all((self,))
- else:
- objects = Picture.tagged.with_all((self,)).order_by()
- return objects.count()
-
@staticmethod
def get_tag_list(tags):
if isinstance(tags, basestring):
category = Tag.categories_rev[name]
else:
try:
- real_tags.append(Tag.objects.exclude(category='book').get(slug=name))
+ real_tags.append(Tag.objects.get(slug=name))
deprecated = True
except Tag.MultipleObjectsReturned, e:
ambiguous_slugs.append(name)
# Pickle complains about not having this.
-TagRelation = Tag.intermediary_table_model
\ No newline at end of file
+TagRelation = Tag.intermediary_table_model