| author | gibus |
| Mon, 14 May 2012 16:01:30 +0200 | |
| changeset 429 | fc7477d34489 |
| parent 407 | 2d14a80716e2 |
| permissions | -rw-r--r-- |
| 0 | 1 |
# This is the denormalisation engine |
2 |
# his goal is to leverage on django's signal to update |
|
3 |
# denormalized fields |
|
4 |
# this should be used with beanstalk or starling |
|
5 |
# python client (there is 2) http://github.com/earl/beanstalkc/tree/master |
|
6 |
||
7 |
import logging |
|
8 |
from django.db.models import signals |
|
9 |
from cm.models import TextVersion, Text |
|
10 |
||
11 |
# Text denormalisation |
|
12 |
def update_text_from_last_version(sender, **kwargs): |
|
13 |
""" |
|
14 |
Update text's last version |
|
15 |
""" |
|
16 |
text_version = kwargs['instance'] |
|
17 |
try: |
|
18 |
text = text_version.text |
|
19 |
text.update_denorm_fields() |
|
20 |
except Text.DoesNotExist: |
|
| 278 | 21 |
pass |
22 |
#logging.warning('No text found for text_version: %i' %text_version.id) |
|
| 0 | 23 |
|
|
407
2d14a80716e2
When last_version is deleted, do not delete text and previous versions in cascade, but really delete text only when there is no previous version, otherwise update text.last_version with previous version.
gibus
parents:
278
diff
changeset
|
24 |
# GIB when deleting last revision, do not delete related text |
|
2d14a80716e2
When last_version is deleted, do not delete text and previous versions in cascade, but really delete text only when there is no previous version, otherwise update text.last_version with previous version.
gibus
parents:
278
diff
changeset
|
25 |
def delete_last_version (sender, instance, signal, *args, **kwargs): |
|
2d14a80716e2
When last_version is deleted, do not delete text and previous versions in cascade, but really delete text only when there is no previous version, otherwise update text.last_version with previous version.
gibus
parents:
278
diff
changeset
|
26 |
if instance.id == instance.text.last_text_version_id: |
|
2d14a80716e2
When last_version is deleted, do not delete text and previous versions in cascade, but really delete text only when there is no previous version, otherwise update text.last_version with previous version.
gibus
parents:
278
diff
changeset
|
27 |
previous = instance.get_previous_version() |
|
2d14a80716e2
When last_version is deleted, do not delete text and previous versions in cascade, but really delete text only when there is no previous version, otherwise update text.last_version with previous version.
gibus
parents:
278
diff
changeset
|
28 |
if previous: |
|
2d14a80716e2
When last_version is deleted, do not delete text and previous versions in cascade, but really delete text only when there is no previous version, otherwise update text.last_version with previous version.
gibus
parents:
278
diff
changeset
|
29 |
instance.text.last_text_version_id = previous.id |
|
2d14a80716e2
When last_version is deleted, do not delete text and previous versions in cascade, but really delete text only when there is no previous version, otherwise update text.last_version with previous version.
gibus
parents:
278
diff
changeset
|
30 |
instance.text.save() |
| 0 | 31 |
|
32 |
def connect_all(): |
|
33 |
# text updated by text_version |
|
|
407
2d14a80716e2
When last_version is deleted, do not delete text and previous versions in cascade, but really delete text only when there is no previous version, otherwise update text.last_version with previous version.
gibus
parents:
278
diff
changeset
|
34 |
signals.pre_delete.connect(delete_last_version, sender=TextVersion) |
| 0 | 35 |
signals.post_save.connect(update_text_from_last_version, sender=TextVersion) |
36 |
signals.post_delete.connect(update_text_from_last_version, sender=TextVersion) |
|
37 |
||
38 |
||
|
407
2d14a80716e2
When last_version is deleted, do not delete text and previous versions in cascade, but really delete text only when there is no previous version, otherwise update text.last_version with previous version.
gibus
parents:
278
diff
changeset
|
39 |
connect_all() |