PageRenderTime 30ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/weiqi/handler/index.py

https://gitlab.com/Xylol/weiqi.gs
Python | 84 lines | 65 code | 4 blank | 15 comment | 1 complexity | 94bd43c0cf3c139eb8a1f9d99cc9e318 MD5 | raw file
  1. # weiqi.gs
  2. # Copyright (C) 2016 Michael Bitzi
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from sqlalchemy.orm import undefer
  17. from tornado.web import HTTPError
  18. from weiqi import settings
  19. from weiqi.handler.base import BaseHandler
  20. from weiqi.identicon import generate_identicon
  21. from weiqi.models import User, Game
  22. from weiqi.sgf import game_to_sgf
  23. class IndexHandler(BaseHandler):
  24. def get(self):
  25. conf = {
  26. 'RECAPTCHA_PUBLIC_KEY': settings.RECAPTCHA['public'],
  27. 'DEFAULT_KOMI': settings.DEFAULT_KOMI,
  28. 'HANDICAP_KOMI': settings.HANDICAP_KOMI,
  29. }
  30. self.set_secure_cookie(settings.COOKIE_NAME, self.get_secure_cookie(settings.COOKIE_NAME) or '')
  31. self.set_header('Cache-control', 'no-cache, no-store, must-revalidate')
  32. self.set_header('Pragma', 'no-cache')
  33. self.set_header('Expires', '0')
  34. self.render('index.html', settings=conf)
  35. class PingHandler(BaseHandler):
  36. def get(self):
  37. self.write('pong')
  38. class AvatarHandler(BaseHandler):
  39. def get(self, user_id):
  40. want_large = self.get_argument('size', '') == 'large'
  41. user = self.db.query(User.avatar, User.avatar_large, User.display).filter_by(id=user_id).first()
  42. if user:
  43. avatar = user[1] if want_large else user[0]
  44. display = user[2]
  45. else:
  46. avatar, display = None, ''
  47. if not avatar:
  48. data = '{}-{}'.format(user_id, display).strip('-')
  49. size = 256 if want_large else 64
  50. avatar = generate_identicon(data.encode(), size=size).getvalue()
  51. self.set_header('Content-Type', 'image/png')
  52. self.set_header('Cache-control', 'max-age=' + str(3600*24))
  53. self.write(avatar)
  54. class SgfHandler(BaseHandler):
  55. def get(self, game_id):
  56. game = self.db.query(Game).options(undefer('board')).get(game_id)
  57. if not game:
  58. raise HTTPError(404)
  59. filename = '%s-%s-%s.sgf' % (game.created_at.date().isoformat(), game.white_display, game.black_display)
  60. self.set_header('Content-Type', 'application/x-go-sgf; charset=utf-8')
  61. self.set_header('Content-Disposition', 'attachment; filename="%s"' % filename)
  62. self.enable_cors()
  63. self.write(game_to_sgf(game))