Minor doc fixes.
[wolnelektury.git] / src / catalogue / signals.py
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.
4 #
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
14
15
16 ####
17 # BookMedia
18 ####
19
20
21 @receiver([post_save, post_delete], sender=BookMedia)
22 def bookmedia_save(sender, instance, **kwargs):
23     instance.book.set_audio_length()
24     instance.book.save()
25
26
27 ####
28 # Collection
29 ####
30
31
32 @receiver(post_save, sender=Collection)
33 def collection_save(sender, instance, **kwargs):
34     caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
35     flush_ssi_includes([
36         '/katalog/%s.json' % lang
37         for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
38
39
40 @receiver(post_delete, sender=Collection)
41 def collection_delete(sender, instance, **kwargs):
42     flush_ssi_includes([
43         '/katalog/%s.json' % lang
44         for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
45
46 ####
47 # Book
48 ####
49
50
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
56     try:
57         caches['template_fragments'].clear()
58     except ImproperlyConfigured:
59         pass
60     instance.flush_includes()
61
62
63 @receiver(post_delete, sender=Book)
64 def book_delete(sender, instance, **kwargs):
65     caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
66     flush_ssi_includes([
67         '/katalog/%s.json' % lang
68         for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
69
70     if not settings.NO_SEARCH_INDEX:
71         # remove the book from search index, when it is deleted.
72         from search.index import Index
73         idx = Index()
74         idx.remove_book(instance)
75         idx.index_tags()
76
77
78 ####
79 # Tag
80 ####
81
82
83 @receiver(Tag.after_change)
84 def tag_after_change(sender, instance, languages, **kwargs):
85     caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
86     flush_ssi_includes([
87         '/katalog/%s.json' % lang
88         for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
89
90     for model in Book, Picture:
91         for model_instance in model.tagged.with_all([instance]).only('pk'):
92             model_instance.flush_includes()
93
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()
98
99
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'))
103     if not categories:
104         return
105
106     caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
107     instance.flush_includes()
108     flush_ssi_includes([
109         '/katalog/%s.json' % lang
110         for lang in [lc for (lc, _ln) in settings.LANGUAGES]])