Making the code pep8 compliant

I ran flake8 on the code and fixed most of the errors except for line
length and continuation indentation errors.

I also refactored how lookup_or_404 works to use the db session method
instead of using the raw sqlalchemy objects