view_routes.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from aiohttp import web
  2. from aiohttp_session import get_session
  3. from templates import render_template
  4. from video import VideoError, find_video_by_id
  5. from user import UserError, find_user_by_id, get_avatar
  6. from config import config
  7. routes = web.RouteTableDef()
  8. @routes.get('/')
  9. async def index(request):
  10. return web.HTTPFound('/random')
  11. @routes.get('/404')
  12. async def index(request):
  13. session = await get_session(request)
  14. return render_template('not_found',
  15. session=session)
  16. @routes.get('/random')
  17. async def random(request):
  18. session = await get_session(request)
  19. return render_template('random',
  20. session=session)
  21. @routes.get('/watch/{video_id:[0-9]+}')
  22. async def watch(request):
  23. session = await get_session(request)
  24. video_id = request.match_info['video_id']
  25. try:
  26. video = await find_video_by_id(video_id)
  27. except VideoError:
  28. return render_template('not_found',
  29. session=session)
  30. return render_template('video',
  31. session=session,
  32. video=video)
  33. @routes.get('/search')
  34. async def search(request):
  35. session = await get_session(request)
  36. return render_template('search',
  37. session=session)
  38. @routes.get('/login')
  39. async def login(request):
  40. session = await get_session(request)
  41. return render_template('login',
  42. session=session,
  43. hcaptcha_sitekey=config.HCAPTCHA_SITEKEY)
  44. @routes.get('/signup')
  45. async def signup(request):
  46. session = await get_session(request)
  47. return render_template('signup',
  48. session=session,
  49. hcaptcha_sitekey=config.HCAPTCHA_SITEKEY)
  50. @routes.get('/user/{user_id:[0-9]+}')
  51. async def user(request):
  52. session = await get_session(request)
  53. user_id = request.match_info['user_id']
  54. return render_template('user',
  55. session=session,
  56. user_id=user_id)
  57. @routes.get('/upload')
  58. async def upload(request):
  59. session = await get_session(request)
  60. return render_template('upload',
  61. session=session,
  62. hcaptcha_sitekey=config.HCAPTCHA_SITEKEY)