SQLalchemy query refactoring 2