PageRenderTime 29ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/weiqi/handler/index.py

https://gitlab.com/sphaso/weiqi.gs
Python | 79 lines | 55 code | 9 blank | 15 comment | 0 complexity | 365791d081ab9a6c2bc920afc2d63949 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 tornado.web import HTTPError
  17. from sqlalchemy.orm import undefer
  18. from weiqi import settings
  19. from weiqi.handler.base import BaseHandler
  20. from weiqi.models import User, Game
  21. from weiqi.identicon import generate_identicon
  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_header('Cache-control', 'no-cache, no-store, must-revalidate')
  31. self.set_header('Pragma', 'no-cache')
  32. self.set_header('Expires', '0')
  33. self.render('index.html', settings=conf)
  34. class PingHandler(BaseHandler):
  35. def get(self):
  36. self.write('pong')
  37. class AvatarHandler(BaseHandler):
  38. def get(self, user_id):
  39. want_large = self.get_argument('size', '') == 'large'
  40. user = self.db.query(User.avatar, User.avatar_large, User.display).filter_by(id=user_id).first()
  41. if user:
  42. avatar = user[1] if want_large else user[0]
  43. display = user[2]
  44. else:
  45. avatar, display = None, ''
  46. if not avatar:
  47. data = '{}-{}'.format(user_id, display).strip('-')
  48. size = 256 if want_large else 64
  49. avatar = generate_identicon(data.encode(), size=size).getvalue()
  50. self.set_header('Content-Type', 'image/png')
  51. self.set_header('Cache-control', 'max-age=' + str(3600*24))
  52. self.write(avatar)
  53. class SgfHandler(BaseHandler):
  54. def get(self, game_id):
  55. game = self.db.query(Game).options(undefer('board')).get(game_id)
  56. if not game:
  57. raise HTTPError(404)
  58. filename = '%s-%s-%s.sgf' % (game.created_at.date().isoformat(), game.white_display, game.black_display)
  59. self.set_header('Content-Type', 'application/x-go-sgf; charset=utf-8')
  60. self.set_header('Content-Disposition', 'attachment; filename="%s"' % filename)
  61. self.write(game_to_sgf(game))