Update librarian
[wolnelektury.git] / src / catalogue / signals.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from django.conf import settings
5 from django.core.cache import caches
6 from django.core.exceptions import ImproperlyConfigured
7 from django.db.models.signals import post_save, post_delete
8 from django.dispatch import receiver
9 from ssify import flush_ssi_includes
10 from newtagging.models import tags_updated
11 from picture.models import Picture, PictureArea
12 from .models import BookMedia, Book, Collection, Fragment, Tag
13
14
15 ####
16 # BookMedia
17 ####
18
19
20 @receiver([post_save, post_delete], sender=BookMedia)
21 def bookmedia_save(sender, instance, **kwargs):
22     instance.book.set_audio_length()
23     instance.book.save()
24
25
26 ####
27 # Collection
28 ####
29
30
31 @receiver(post_save, sender=Collection)
32 def collection_save(sender, instance, **kwargs):
33     caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
34     flush_ssi_includes([
35         '/katalog/%s.json' % lang
36         for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
37
38
39 @receiver(post_delete, sender=Collection)
40 def collection_delete(sender, instance, **kwargs):
41     flush_ssi_includes([
42         '/katalog/%s.json' % lang
43         for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
44
45 ####
46 # Book
47 ####
48
49
50 @receiver(post_save, sender=Book)
51 def book_save(sender, instance, **kwargs):
52     # Books come out anywhere.
53     caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
54     # deleting selectively is too much work
55     try:
56         caches['template_fragments'].clear()
57     except ImproperlyConfigured:
58         pass
59     instance.flush_includes()
60
61
62 @receiver(post_delete, sender=Book)
63 def book_delete(sender, instance, **kwargs):
64     caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
65     flush_ssi_includes([
66         '/katalog/%s.json' % lang
67         for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
68
69     if not settings.NO_SEARCH_INDEX:
70         # remove the book from search index, when it is deleted.
71         from search.index import Index
72         idx = Index()
73         idx.remove_book(instance)
74         idx.index_tags()
75
76
77 ####
78 # Tag
79 ####
80
81
82 @receiver(Tag.after_change)
83 def tag_after_change(sender, instance, languages, **kwargs):
84     caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
85     flush_ssi_includes([
86         '/katalog/%s.json' % lang
87         for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
88
89     for model in Book, Picture:
90         for model_instance in model.tagged.with_all([instance]).only('pk'):
91             model_instance.flush_includes()
92
93     if instance.category == 'author':
94         for model in Fragment, PictureArea:
95             for model_instance in model.tagged.with_all([instance]).only('pk'):
96                 model_instance.flush_includes()
97
98
99 @receiver(tags_updated)
100 def receive_tags_updated(sender, instance, affected_tags, **kwargs):
101     categories = set(tag.category for tag in affected_tags if tag.category not in ('set', 'book'))
102     if not categories:
103         return
104
105     caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
106     instance.flush_includes()
107     flush_ssi_includes([
108         '/katalog/%s.json' % lang
109         for lang in [lc for (lc, _ln) in settings.LANGUAGES]])