SQLalchemy query refactoring 1