Completed new query function in Flask views