refactoring code using flask, flask-sqlalchemy.