src/cm/denorm_engine.py
author Simon Descarpentries <sid@sopinspace.com>
Wed, 30 Oct 2013 18:08:42 +0100
changeset 556 69503659fe8f
parent 407 2d14a80716e2
permissions -rw-r--r--
[c_selection.js] If safari_mobile, get current selection from a previously created global variable [c_sync.js] ref where the safari_mobile global is used [c_text_view_comments.js] if safari_mobile update selection also on selectionChange event [text_view_comments.html] if safari_mobile store a clone of the current selection on each selectionChange set layout width to 99% to improve display factorize safari mobile detection code

# This is the denormalisation engine
# his goal is to leverage on django's signal to update
# denormalized fields
# this should be used with beanstalk or starling
# python client (there is 2) http://github.com/earl/beanstalkc/tree/master

import logging
from django.db.models import signals
from cm.models import TextVersion, Text

# Text denormalisation
def update_text_from_last_version(sender, **kwargs):
    """
    Update text's last version
    """
    text_version = kwargs['instance']
    try:
        text = text_version.text
        text.update_denorm_fields()
    except Text.DoesNotExist:
        pass
        #logging.warning('No text found for text_version: %i' %text_version.id)
        
# GIB when deleting last revision, do not delete related text
def delete_last_version (sender, instance, signal, *args, **kwargs):
  if instance.id == instance.text.last_text_version_id:
    previous = instance.get_previous_version()
    if previous:
      instance.text.last_text_version_id = previous.id
      instance.text.save()
        
def connect_all():
    # text updated by text_version
    signals.pre_delete.connect(delete_last_version, sender=TextVersion)
    signals.post_save.connect(update_text_from_last_version, sender=TextVersion)
    signals.post_delete.connect(update_text_from_last_version, sender=TextVersion)


connect_all()