top align in tables
[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.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.save()
23
24
25 ####
26 # Collection
27 ####
28
29
30 @receiver(post_save, sender=Collection)
31 def collection_save(sender, instance, **kwargs):
32     caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
33     flush_ssi_includes([
34         '/katalog/%s.json' % lang
35         for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
36
37
38 @receiver(post_delete, sender=Collection)
39 def collection_delete(sender, instance, **kwargs):
40     flush_ssi_includes([
41         '/katalog/%s.json' % lang
42         for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
43
44 ####
45 # Book
46 ####
47
48
49 @receiver(post_save, sender=Book)
50 def book_save(sender, instance, **kwargs):
51     # Books come out anywhere.
52     caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
53     instance.flush_includes()
54
55
56 @receiver(post_delete, sender=Book)
57 def book_delete(sender, instance, **kwargs):
58     caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
59     flush_ssi_includes([
60         '/katalog/%s.json' % lang
61         for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
62
63     if not settings.NO_SEARCH_INDEX:
64         # remove the book from search index, when it is deleted.
65         from search.index import Index
66         idx = Index()
67         idx.remove_book(instance)
68         idx.index_tags()
69
70
71 ####
72 # Tag
73 ####
74
75
76 @receiver(Tag.after_change)
77 def tag_after_change(sender, instance, languages, **kwargs):
78     caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
79     flush_ssi_includes([
80         '/katalog/%s.json' % lang
81         for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
82
83     for model in Book, Picture:
84         for model_instance in model.tagged.with_all([instance]).only('pk'):
85             model_instance.flush_includes()
86
87     if instance.category == 'author':
88         for model in Fragment, PictureArea:
89             for model_instance in model.tagged.with_all([instance]).only('pk'):
90                 model_instance.flush_includes()
91
92
93 @receiver(tags_updated)
94 def receive_tags_updated(sender, instance, affected_tags, **kwargs):
95     categories = set(tag.category for tag in affected_tags if tag.category not in ('set', 'book'))
96     if not categories:
97         return
98
99     caches[settings.CACHE_MIDDLEWARE_ALIAS].clear()
100     instance.flush_includes()
101     flush_ssi_includes([
102         '/katalog/%s.json' % lang
103         for lang in [lc for (lc, _ln) in settings.LANGUAGES]])