forked from django-cms/django-cms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsignals.py
103 lines (78 loc) · 3.67 KB
/
signals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from django.db.models import signals
from cms import settings as cms_settings, appresolver
from cms.models import signals as cms_signals, Page, Title
from cms.models import CMSPlugin
def update_plugin_positions(**kwargs):
plugin = kwargs['instance']
plugins = CMSPlugin.objects.filter(page=plugin.page, language=plugin.language, placeholder=plugin.placeholder).order_by("position")
last = 0
for p in plugins:
if p.position != last:
p.position = last
p.save()
last += 1
signals.post_delete.connect(update_plugin_positions, sender=CMSPlugin)
def update_title_paths(instance, **kwargs):
"""Update child pages paths in case when page was moved.
"""
for title in instance.title_set.all():
title.save()
cms_signals.page_moved.connect(update_title_paths, sender=Page)
def pre_save_title(instance, raw, **kwargs):
"""Save old state to instance and setup path
"""
instance.tmp_path = None
instance.tmp_application_urls = None
if instance.id:
try:
tmp_title = Title.objects.get(pk=instance.id)
instance.tmp_path = tmp_title.path
instance.tmp_application_urls = tmp_title.application_urls
except:
pass # no Titles exist for this page yet
# Build path from parent page's path and slug
if instance.has_url_overwrite and instance.path:
instance.path = instance.path.strip(" /")
else:
parent_page = instance.page.parent
slug = u'%s' % instance.slug
if parent_page:
parent_path = Title.objects.get_title(parent_page, language=instance.language, language_fallback=True).path
instance.path = (u'%s/%s' % (parent_path, slug)).lstrip("/")
else:
instance.path = u'%s' % slug
signals.pre_save.connect(pre_save_title, sender=Title)
def post_save_title(instance, raw, created, **kwargs):
# Update descendants only if path changed
application_changed = False
if instance.path != instance.tmp_path and not hasattr(instance, 'tmp_prevent_descendant_update'):
descendant_titles = Title.objects.filter(
page__lft__gt=instance.page.lft,
page__rght__lt=instance.page.rght,
page__tree_id__exact=instance.page.tree_id,
language=instance.language
).order_by('page__tree_id', 'page__parent', 'page__lft')
for descendant_title in descendant_titles:
descendant_title.path = '' # just reset path
descendant_title.tmp_prevent_descendant_update = True
if descendant_title.application_urls:
application_changed = True
descendant_title.save()
if not hasattr(instance, 'tmp_prevent_descendant_update') and \
(instance.application_urls != instance.tmp_application_urls or application_changed):
# fire it if we have some application linked to this page or some descendant
cms_signals.application_post_changed.send(sender=Title, instance=instance)
# remove temporary attributes
del(instance.tmp_path)
del(instance.tmp_application_urls)
try:
del(instance.tmp_prevent_descendant_update)
except AttributeError:
pass
signals.post_save.connect(post_save_title, sender=Title)
def clear_appresolver_cache(instance, **kwargs):
# reset cached applications - there were a change probably
appresolver.dynamic_app_regex_url_resolver.reset_cache()
if cms_settings.CMS_APPLICATIONS_URLS:
# register this signal only if we have some hookable applications
cms_signals.application_post_changed.connect(clear_appresolver_cache, sender=Title)