|
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: |
|
|
21 |
logging.warning('No text found for text_version: %i' %text_version.id) |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
def connect_all(): |
|
|
25 |
# text updated by text_version |
|
|
26 |
signals.post_save.connect(update_text_from_last_version, sender=TextVersion) |
|
|
27 |
signals.post_delete.connect(update_text_from_last_version, sender=TextVersion) |
|
|
28 |
|
|
|
29 |
|
|
|
30 |
connect_all() |