def modify(entity, auth_user): assert entity model = next((model for model in db.Base._decl_class_registry.values() if hasattr(model, '__table__') and model.__table__.fullname == entity.__table__.fullname), None) assert model snapshot = _create(db.Snapshot.OPERATION_MODIFIED, entity, auth_user) snapshot_factory = _snapshot_factories[snapshot.resource_type] detached_session = db.sessionmaker() detached_entity = detached_session.query(model).get(snapshot.resource_pkey) assert detached_entity, 'Entity not found in DB, have you committed it?' detached_snapshot = snapshot_factory(detached_entity) detached_session.close() active_snapshot = snapshot_factory(entity) snapshot.data = diff.get_dict_diff(detached_snapshot, active_snapshot) if not snapshot.data: return db.session.add(snapshot)
def modify(entity: model.Base, auth_user: Optional[model.User]) -> None: assert entity table = next( ( cls for cls in model.Base._decl_class_registry.values() if hasattr(cls, '__table__') and cls.__table__.fullname == entity.__table__.fullname ), None) assert table snapshot = _create(model.Snapshot.OPERATION_MODIFIED, entity, auth_user) snapshot_factory = _snapshot_factories[snapshot.resource_type] detached_session = db.sessionmaker() detached_entity = detached_session.query(table).get(snapshot.resource_pkey) assert detached_entity, 'Entity not found in DB, have you committed it?' detached_snapshot = snapshot_factory(detached_entity) detached_session.close() active_snapshot = snapshot_factory(entity) snapshot.data = diff.get_dict_diff(detached_snapshot, active_snapshot) if not snapshot.data: return db.session.add(snapshot)
def modify(entity: model.Base, auth_user: Optional[model.User]) -> None: assert entity table = next( ( cls for cls in model.Base._decl_class_registry.values() if hasattr(cls, "__table__") and cls.__table__.fullname == entity.__table__.fullname ), None, ) assert table snapshot = _create(model.Snapshot.OPERATION_MODIFIED, entity, auth_user) snapshot_factory = _snapshot_factories[snapshot.resource_type] detached_session = sa.orm.sessionmaker(bind=db.session.get_bind())() detached_entity = detached_session.query(table).get(snapshot.resource_pkey) assert detached_entity, "Entity not found in DB, have you committed it?" detached_snapshot = snapshot_factory(detached_entity) detached_session.close() active_snapshot = snapshot_factory(entity) snapshot.data = diff.get_dict_diff(detached_snapshot, active_snapshot) if not snapshot.data: return db.session.add(snapshot)
def test_get_dict_diff(old, new, expected): assert diff.get_dict_diff(old, new) == expected
def test_get_dict_diff(old, new, expected): assert diff.get_dict_diff(old, new) == expected