1
0

view_routes.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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('/mkuser')
  9. async def debug(request):
  10. from db import db, User
  11. from tools import hash_password
  12. u = await db.create(User, username='cirno', password=hash_password('cirno'), ip_address='127.0.0.1')
  13. return web.json_response({'i':u.id})
  14. @routes.get('/mkuser2')
  15. async def debug(request):
  16. from db import db, User
  17. from tools import hash_password
  18. u = await db.create(User, username='cirno9', password=hash_password('cirno9'), ip_address='127.0.0.1')
  19. return web.json_response({'i':u.id})
  20. @routes.get('/mkvid')
  21. async def debug(request):
  22. from db import db, Video, User, VideoTag, Tag
  23. u = await db.get(User.select().where(User.username == 'cirno'))
  24. v = await db.create(Video, video='test', thumbnail='/static/thumbs/default.png', uploader=u)
  25. tag1 = await db.get_or_create(Tag, tag='test1')
  26. tag2 = await db.get_or_create(Tag, tag='test2')
  27. tag3 = await db.get_or_create(Tag, tag='test3')
  28. tag4 = await db.get_or_create(Tag, tag='test4')
  29. await db.create(VideoTag, video=v, tag=tag1[0])
  30. await db.create(VideoTag, video=v, tag=tag2[0])
  31. await db.create(VideoTag, video=v, tag=tag3[0])
  32. await db.create(VideoTag, video=v, tag=tag4[0])
  33. return web.json_response({'i':v.id})
  34. @routes.get('/')
  35. async def index(request):
  36. return web.HTTPFound('/random')
  37. @routes.get('/404')
  38. async def index(request):
  39. session = await get_session(request)
  40. return render_template('not_found',
  41. session=session)
  42. @routes.get('/random')
  43. async def random(request):
  44. session = await get_session(request)
  45. return render_template('random',
  46. session=session)
  47. @routes.get('/watch/{video_id:[0-9]+}')
  48. async def watch(request):
  49. session = await get_session(request)
  50. video_id = request.match_info['video_id']
  51. try:
  52. video = await find_video_by_id(video_id)
  53. except VideoError:
  54. return render_template('not_found',
  55. session=session)
  56. return render_template('video',
  57. session=session,
  58. video=video)
  59. @routes.get('/search')
  60. async def search(request):
  61. session = await get_session(request)
  62. return render_template('search',
  63. session=session)
  64. @routes.get('/login')
  65. async def login(request):
  66. session = await get_session(request)
  67. return render_template('login',
  68. session=session,
  69. hcaptcha_sitekey=config.HCAPTCHA_SITEKEY)
  70. @routes.get('/signup')
  71. async def signup(request):
  72. session = await get_session(request)
  73. return render_template('signup',
  74. session=session,
  75. hcaptcha_sitekey=config.HCAPTCHA_SITEKEY)
  76. @routes.get('/user/{user_id:[0-9]+}')
  77. async def user(request):
  78. session = await get_session(request)
  79. user_id = request.match_info['user_id']
  80. return render_template('user',
  81. session=session,
  82. user_id=user_id)
  83. @routes.get('/upload')
  84. async def upload(request):
  85. session = await get_session(request)
  86. return render_template('upload',
  87. session=session,
  88. hcaptcha_sitekey=config.HCAPTCHA_SITEKEY)