Always ignore certain data classes when recording instance changes.
This commit is contained in:
parent
eb317d9b3f
commit
7eb44e333c
|
@ -24,7 +24,7 @@
|
||||||
Data Changes Interface
|
Data Changes Interface
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals, absolute_import
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
@ -32,6 +32,7 @@ from sqlalchemy.orm import object_mapper, RelationshipProperty
|
||||||
from sqlalchemy.orm.interfaces import SessionExtension
|
from sqlalchemy.orm.interfaces import SessionExtension
|
||||||
from sqlalchemy.orm.session import Session
|
from sqlalchemy.orm.session import Session
|
||||||
|
|
||||||
|
from rattail.db import model
|
||||||
from rattail.core import get_uuid
|
from rattail.core import get_uuid
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -81,6 +82,7 @@ class ChangeRecorder(object):
|
||||||
|
|
||||||
def __init__(self, ignore_role_changes=True):
|
def __init__(self, ignore_role_changes=True):
|
||||||
self.ignore_role_changes = ignore_role_changes
|
self.ignore_role_changes = ignore_role_changes
|
||||||
|
self.ignored_classes = (model.Setting, model.Change, model.DataSyncChange)
|
||||||
|
|
||||||
def __call__(self, session, flush_context, instances):
|
def __call__(self, session, flush_context, instances):
|
||||||
"""
|
"""
|
||||||
|
@ -93,13 +95,15 @@ class ChangeRecorder(object):
|
||||||
versioning_manager.before_flush(session, flush_context, instances)
|
versioning_manager.before_flush(session, flush_context, instances)
|
||||||
|
|
||||||
for instance in session.deleted:
|
for instance in session.deleted:
|
||||||
log.debug("found deleted instance: {0}".format(repr(instance)))
|
if not isinstance(instance, self.ignored_classes):
|
||||||
|
log.debug("found deleted instance: {}".format(repr(instance)))
|
||||||
self.record_change(session, instance, deleted=True)
|
self.record_change(session, instance, deleted=True)
|
||||||
for instance in session.new:
|
for instance in session.new:
|
||||||
log.debug("found new instance: {0}".format(repr(instance)))
|
if not isinstance(instance, self.ignored_classes):
|
||||||
|
log.debug("found new instance: {}".format(repr(instance)))
|
||||||
self.record_change(session, instance)
|
self.record_change(session, instance)
|
||||||
for instance in session.dirty:
|
for instance in session.dirty:
|
||||||
if session.is_modified(instance, passive=True):
|
if not isinstance(instance, self.ignored_classes) and session.is_modified(instance, passive=True):
|
||||||
# Orphaned objects which really are pending deletion show up in
|
# Orphaned objects which really are pending deletion show up in
|
||||||
# session.dirty instead of session.deleted, hence this check.
|
# session.dirty instead of session.deleted, hence this check.
|
||||||
# See also https://groups.google.com/d/msg/sqlalchemy/H4nQTHphc0M/Xr8-Cgra0Z4J
|
# See also https://groups.google.com/d/msg/sqlalchemy/H4nQTHphc0M/Xr8-Cgra0Z4J
|
||||||
|
@ -150,8 +154,6 @@ class ChangeRecorder(object):
|
||||||
:returns: ``True`` if a change was recorded, or ``False`` if it was
|
:returns: ``True`` if a change was recorded, or ``False`` if it was
|
||||||
ignored.
|
ignored.
|
||||||
"""
|
"""
|
||||||
from rattail.db import model
|
|
||||||
|
|
||||||
# No need to record changes for changes.
|
# No need to record changes for changes.
|
||||||
if isinstance(instance, (model.Change, model.DataSyncChange)):
|
if isinstance(instance, (model.Change, model.DataSyncChange)):
|
||||||
return False
|
return False
|
||||||
|
@ -177,7 +179,7 @@ class ChangeRecorder(object):
|
||||||
instance_uuid=instance.uuid,
|
instance_uuid=instance.uuid,
|
||||||
deleted=deleted)
|
deleted=deleted)
|
||||||
session.add(change)
|
session.add(change)
|
||||||
log.debug("recorded change: {0}".format(repr(change)))
|
log.debug("recorded {}".format(repr(change)))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def ensure_uuid(self, instance):
|
def ensure_uuid(self, instance):
|
||||||
|
|
Loading…
Reference in a new issue