Fix.
[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 import cover.models
7 from documents.models import (Book, Chunk, Image, BookPublishRecord,
8         ImagePublishRecord)
9 from documents.signals import post_publish
10 from dvcs.signals import post_publishable
11
12
13 def book_changed(sender, instance, created, **kwargs):
14     instance.touch()
15     for c in instance:
16         c.touch()
17 models.signals.post_save.connect(book_changed, sender=Book)
18
19
20 def chunk_changed(sender, instance, created, **kwargs):
21     instance.book.touch()
22     instance.touch()
23 models.signals.post_save.connect(chunk_changed, sender=Chunk)
24
25
26 def image_changed(sender, instance, created, **kwargs):
27     instance.touch()
28 models.signals.post_save.connect(image_changed, sender=Image)
29
30
31 def publish_listener(sender, *args, **kwargs):
32     if isinstance(sender, BookPublishRecord):
33         sender.book.touch()
34         for c in sender.book:
35             c.touch()
36     elif isinstance(sender, ImagePublishRecord):
37         sender.image.touch()
38 post_publish.connect(publish_listener)
39
40
41 def chunk_publishable_listener(sender, *args, **kwargs):
42     sender.tree.touch()
43     if isinstance(sender.tree, Chunk):
44         sender.tree.book.touch()
45 post_publishable.connect(chunk_publishable_listener)
46
47 def publishable_listener(sender, *args, **kwargs):
48     sender.tree.touch()
49 post_publishable.connect(publishable_listener, sender=Image)
50
51
52 def listener_create(sender, instance, created, **kwargs):
53     if created:
54         instance.chunk_set.create(number=1, slug='1')
55 models.signals.post_save.connect(listener_create, sender=Book)
56
57
58 def cover_changed(sender, instance, created, **kwargs):
59     for book in instance.book_set.all():
60         book.build_cover()
61 models.signals.post_save.connect(cover_changed, sender=cover.models.Image)
62