Fix annotations window.
[redakcja.git] / src / documents / models / listeners.py
1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from django.contrib.auth.models import User
5 from django.db import models
6 from documents.models import (Book, Chunk, Image, BookPublishRecord,
7         ImagePublishRecord)
8 from documents.signals import post_publish
9 from dvcs.signals import post_publishable
10
11
12 def book_changed(sender, instance, created, **kwargs):
13     instance.touch()
14     for c in instance:
15         c.touch()
16 models.signals.post_save.connect(book_changed, sender=Book)
17
18
19 def chunk_changed(sender, instance, created, **kwargs):
20     instance.book.touch()
21     instance.touch()
22 models.signals.post_save.connect(chunk_changed, sender=Chunk)
23
24
25 def image_changed(sender, instance, created, **kwargs):
26     instance.touch()
27 models.signals.post_save.connect(image_changed, sender=Image)
28
29
30 def publish_listener(sender, *args, **kwargs):
31     if isinstance(sender, BookPublishRecord):
32         sender.book.touch()
33         for c in sender.book:
34             c.touch()
35     elif isinstance(sender, ImagePublishRecord):
36         sender.image.touch()
37 post_publish.connect(publish_listener)
38
39
40 def chunk_publishable_listener(sender, *args, **kwargs):
41     sender.tree.touch()
42     if isinstance(sender.tree, Chunk):
43         sender.tree.book.touch()
44 post_publishable.connect(chunk_publishable_listener)
45
46 def publishable_listener(sender, *args, **kwargs):
47     sender.tree.touch()
48 post_publishable.connect(publishable_listener, sender=Image)
49
50
51 def listener_create(sender, instance, created, **kwargs):
52     if created:
53         instance.chunk_set.create(number=1, slug='1')
54 models.signals.post_save.connect(listener_create, sender=Book)
55