refactor logic into views.py