Split middleware logic into functions
Add a simple django middleware