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.core.cache import caches
7 from django.core.exceptions import ImproperlyConfigured
8 from django.db.models.signals import post_save, post_delete
9 from django.dispatch import receiver
10 from ssify import flush_ssi_includes
11 from newtagging.models import tags_updated
12 from picture.models import Picture, PictureArea
13 from .models import BookMedia, Book, Collection, Fragment, Tag
21 @receiver([post_save, post_delete], sender=BookMedia)
22 def bookmedia_save(sender, instance, **kwargs):
23 instance.book.set_audio_length()
32 @receiver(post_save, sender=Collection)
33 def collection_save(sender, instance, **kwargs):
34 caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
36 '/katalog/%s.json' % lang
37 for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
40 @receiver(post_delete, sender=Collection)
41 def collection_delete(sender, instance, **kwargs):
43 '/katalog/%s.json' % lang
44 for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
51 @receiver(post_save, sender=Book)
52 def book_save(sender, instance, **kwargs):
53 # Books come out anywhere.
54 caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
55 # deleting selectively is too much work
57 caches['template_fragments'].clear()
58 except ImproperlyConfigured:
60 instance.flush_includes()
63 @receiver(post_delete, sender=Book)
64 def book_delete(sender, instance, **kwargs):
65 caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
67 '/katalog/%s.json' % lang
68 for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
70 if not settings.NO_SEARCH_INDEX:
71 # remove the book from search index, when it is deleted.
72 from search.index import Index
74 idx.remove_book(instance)
83 @receiver(Tag.after_change)
84 def tag_after_change(sender, instance, languages, **kwargs):
85 caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
87 '/katalog/%s.json' % lang
88 for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
90 for model in Book, Picture:
91 for model_instance in model.tagged.with_all([instance]).only('pk'):
92 model_instance.flush_includes()
94 if instance.category == 'author':
95 for model in Fragment, PictureArea:
96 for model_instance in model.tagged.with_all([instance]).only('pk'):
97 model_instance.flush_includes()
100 @receiver(tags_updated)
101 def receive_tags_updated(sender, instance, affected_tags, **kwargs):
102 categories = set(tag.category for tag in affected_tags if tag.category not in ('set', 'book'))
106 caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
107 instance.flush_includes()
109 '/katalog/%s.json' % lang
110 for lang in [lc for (lc, _ln) in settings.LANGUAGES]])