code check by joel, fix style issues, use enumerate with an item in list loop, do sqlalchemy joins to do fewer queries