Massively refactor the document() view.
Massively refactor the document() view.

This splits up the original monolithic document() view into quite a
few pieces. Many operations have been pulled out into standalone
utility functions, for ease of future maintenance and tracing the
control flow. Additionally, two of the original exit points (showing
deletion logs for a deleted Document, and showing a raw Document) have
been spun out into views of their own.

Along the way, some code has been cleaned up or tightened for better
Pythonic style, and the "set_common_headers" closure has been promoted
into its own standalone function, albeit with a different signature
than the original (the ability to pass in a set of response headers
and have them preserved was never actually used).

escattone/kuma,jgmize/kuma,bluemini/kuma,Elchi3/kuma,chirilo/kuma,robhudson/kuma,davidyezsetz/kuma,utkbansal/kuma,Elchi3/kuma,groovecoder/kuma,scrollback/kuma,davehunt/kuma,chirilo/kuma,ollie314/kuma,mozilla/kuma,MenZil/kuma,escattone/kuma,MenZil/kuma,a2sheppy/kuma,groovecoder/kuma,jgmize/kuma,ronakkhunt/kuma,whip112/Whip112,surajssd/kuma,yfdyh000/kuma,RanadeepPolavarapu/kuma,hoosteeno/kuma,hoosteeno/kuma,surajssd/kuma,chirilo/kuma,robhudson/kuma,varunkamra/kuma,robhudson/kuma,whip112/Whip112,robhudson/kuma,varunkamra/kuma,scrollback/kuma,yfdyh000/kuma,a2sheppy/kuma,safwanrahman/kuma,biswajitsahu/kuma,darkwing/kuma,YOTOV-LIMITED/kuma,FrankBian/kuma,carnell69/kuma,groovecoder/kuma,nhenezi/kuma,jezdez/kuma,nhenezi/kuma,surajssd/kuma,escattone/kuma,FrankBian/kuma,openjck/kuma,utkbansal/kuma,davehunt/kuma,varunkamra/kuma,bluemini/kuma,safwanrahman/kuma,biswajitsahu/kuma,utkbansal/kuma,tximikel/kuma,MenZil/kuma,anaran/kuma,FrankBian/kuma,mozilla/kuma,jwhitlock/kuma,tximikel/kuma,yfdyh000/kuma,jgmize/kuma,jwhitlock/kuma,biswajitsahu/kuma,groovecoder/kuma,jezdez/kuma,ollie314/kuma,ronakkhunt/kuma,chirilo/kuma,SphinxKnight/kuma,nhenezi/kuma,davidyezsetz/kuma,davehunt/kuma,darkwing/kuma,nhenezi/kuma,utkbansal/kuma,cindyyu/kuma,surajssd/kuma,YOTOV-LIMITED/kuma,mozilla/kuma,groovecoder/kuma,MenZil/kuma,davidyezsetz/kuma,anaran/kuma,YOTOV-LIMITED/kuma,mozilla/kuma,mastizada/kuma,safwanrahman/kuma,whip112/Whip112,anaran/kuma,openjck/kuma,SphinxKnight/kuma,cindyyu/kuma,tximikel/kuma,biswajitsahu/kuma,surajssd/kuma,hoosteeno/kuma,ronakkhunt/kuma,carnell69/kuma,MenZil/kuma,yfdyh000/kuma,tximikel/kuma,YOTOV-LIMITED/kuma,YOTOV-LIMITED/kuma,davehunt/kuma,jezdez/kuma,safwanrahman/kuma,darkwing/kuma,jezdez/kuma,jgmize/kuma,SphinxKnight/kuma,SphinxKnight/kuma,mastizada/kuma,chirilo/kuma,darkwing/kuma,openjck/kuma,YOTOV-LIMITED/kuma,jwhitlock/kuma,mastizada/kuma,RanadeepPolavarapu/kuma,ronakkhunt/kuma,darkwing/kuma,jwhitlock/kuma,ronakkhunt/kuma,ollie314/kuma,varunkamra/kuma,a2sheppy/kuma,tximikel/kuma,hoosteeno/kuma,anaran/kuma,cindyyu/kuma,ollie314/kuma,cindyyu/kuma,whip112/Whip112,cindyyu/kuma,a2sheppy/kuma,robhudson/kuma,SphinxKnight/kuma,jezdez/kuma,carnell69/kuma,utkbansal/kuma,chirilo/kuma,bluemini/kuma,robhudson/kuma,Elchi3/kuma,openjck/kuma,scrollback/kuma,safwanrahman/kuma,yfdyh000/kuma,bluemini/kuma,scrollback/kuma,bluemini/kuma,cindyyu/kuma,whip112/Whip112,FrankBian/kuma,hoosteeno/kuma,FrankBian/kuma,davidyezsetz/kuma,MenZil/kuma,utkbansal/kuma,yfdyh000/kuma,carnell69/kuma,mozilla/kuma,nhenezi/kuma,anaran/kuma,tximikel/kuma,scrollback/kuma,whip112/Whip112,ollie314/kuma,bluemini/kuma,openjck/kuma,anaran/kuma,a2sheppy/kuma,varunkamra/kuma,Elchi3/kuma,Elchi3/kuma,surajssd/kuma,carnell69/kuma,safwanrahman/kuma,groovecoder/kuma,RanadeepPolavarapu/kuma,ronakkhunt/kuma,davehunt/kuma,darkwing/kuma,RanadeepPolavarapu/kuma,biswajitsahu/kuma,SphinxKnight/kuma,mastizada/kuma,RanadeepPolavarapu/kuma,biswajitsahu/kuma,jgmize/kuma,varunkamra/kuma,carnell69/kuma,jwhitlock/kuma,davidyezsetz/kuma,jgmize/kuma,ollie314/kuma,hoosteeno/kuma,davehunt/kuma,openjck/kuma,RanadeepPolavarapu/kuma,jezdez/kuma