def _get_or_create_history(obj): """ Get or create the history dict for this object. Gets or sets OBJECT_ATTRIBUTE on object. Get or sets attribute REQUEST_ATTRIBUTE on request. Get or creates history item for object on request attribute and returns that. """ if not hasattr(obj, OBJECT_ATTRIBUTE): setattr(obj, OBJECT_ATTRIBUTE, utils.object_hash(obj)) obj_hash = getattr(obj, OBJECT_ATTRIBUTE) if not hasattr(utils.active_request(), REQUEST_ATTRIBUTE): setattr(utils.active_request(), REQUEST_ATTRIBUTE, {}) history = getattr(utils.active_request(), REQUEST_ATTRIBUTE) if not obj_hash in history: history[obj_hash] = {SIGNALS_KEY: []} return history[obj_hash]
def db_handler(sender, instance, signal_name, raw=None, **kwargs): """ Store old and new objects on the active request. """ if raw or not utils.active_request(): return history = _get_or_create_history(instance) history[SIGNALS_KEY].append(signal_name) history[INSTANCE_KEY] = instance # Store initial status of the object on the request. if signal_name.startswith('pre_') and not PRE_COPY_KEY in history: history[PRE_COPY_KEY] = _get_db_copy(instance)
def process_request_handler(**kwargs): """ Log any changes recorded on the request object. """ if not hasattr(utils.active_request(), REQUEST_ATTRIBUTE): return try: actions = getattr(utils.active_request(), REQUEST_ATTRIBUTE).values() except AttributeError: return logged_models = set() for action in actions: pre_copy = action[PRE_COPY_KEY] post_copy = _get_db_copy(action[INSTANCE_KEY]) signals = [s for s in action[SIGNALS_KEY] if s in ('post_save', 'post_delete')] if len(signals) <= 0: continue last_signal = signals[-1] if last_signal == 'post_save': obj = post_copy action_flag = (utils.LIZARD_CHANGE if pre_copy else utils.LIZARD_ADDITION) elif last_signal == 'post_delete': obj = pre_copy action_flag = utils.LIZARD_DELETION else: continue # Custom handling starts here if utils.is_object_of(obj, ESF_MODELS): if logged_models.intersection(ESF_MODELS): continue # Already logged the esf data else: object_repr = obj.area.ident elif utils.is_object_of(obj, WBCONFIGURATION_MODELS): if logged_models.intersection(WBCONFIGURATION_MODELS): continue # Already logged the wbconfiguration data else: object_repr = obj.area.area.ident # Custom handling starts ends here else: object_repr = force_unicode(obj) change_message = utils.change_message( old_object=pre_copy, new_object=post_copy, instance=action['instance'], ) # Don't log if nothing was changed. if change_message is None: return # Insert a log entry in django's admin log. LogEntry.objects.log_action( user_id=utils.user_pk(), content_type_id=utils.get_contenttype_id(obj), object_id=obj.pk, object_repr=object_repr, action_flag=action_flag, change_message=change_message, ) logged_models.add(utils.model_for_object(obj))