reorganization of some utility function, new before_flush sqla event added to auto-globalize new objects if a specified parent is already global