A corporation manager and dashboard for EVE Online
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

95 řádky
2.5 KiB

  1. #! /usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from pathlib import Path
  4. from flask import Flask, abort, flash, g, redirect, request, url_for
  5. from flask_mako import MakoTemplates, render_template
  6. import calefaction
  7. from calefaction.auth import AuthManager
  8. from calefaction.config import Config
  9. from calefaction.database import Database
  10. from calefaction.eve import EVE
  11. from calefaction.messages import Messages
  12. from calefaction.util import (
  13. try_func, make_error_catcher, make_route_restricter,
  14. set_up_asset_versioning)
  15. app = Flask(__name__)
  16. basepath = Path(__file__).resolve().parent
  17. config = Config(basepath / "config")
  18. Database.path = str(basepath / "data" / "db.sqlite3")
  19. eve = EVE(config)
  20. auth = AuthManager(config, eve)
  21. app.catch_exceptions = make_error_catcher(app, "error.mako")
  22. app.route_restricted = make_route_restricter(
  23. auth, lambda: redirect(url_for("index"), 303))
  24. MakoTemplates(app)
  25. set_up_asset_versioning(app)
  26. calefaction.enable_logging()
  27. config.install(app)
  28. @app.before_request
  29. def prepare_request():
  30. g.auth = auth
  31. g.config = config
  32. g.eve = eve
  33. g.modules = config.modules
  34. g.version = calefaction.__version__
  35. app.before_request(Database.pre_hook)
  36. app.teardown_appcontext(Database.post_hook)
  37. @app.route("/")
  38. @app.catch_exceptions
  39. def index():
  40. success, _ = try_func(auth.is_authenticated)
  41. if success:
  42. module = config.get("modules.home")
  43. if module:
  44. return config.modules[module].home()
  45. return render_template("default_home.mako")
  46. return render_template("landing.mako")
  47. @app.route("/login", methods=["GET", "POST"])
  48. @app.catch_exceptions
  49. def login():
  50. code = request.args.get("code")
  51. state = request.args.get("state")
  52. success, caught = try_func(lambda: auth.handle_login(code, state))
  53. if success:
  54. flash(Messages.LOGGED_IN, "success")
  55. elif not caught:
  56. flash(Messages.LOGIN_FAILED, "error")
  57. return redirect(url_for("index"), 303)
  58. @app.route("/logout", methods=["GET", "POST"])
  59. @app.catch_exceptions
  60. def logout():
  61. if request.method == "GET":
  62. return render_template("logout.mako")
  63. auth.handle_logout()
  64. flash(Messages.LOGGED_OUT, "success")
  65. return redirect(url_for("index"), 303)
  66. @app.route("/settings/style/<style>", methods=["POST"])
  67. @app.catch_exceptions
  68. @app.route_restricted
  69. def set_style(style):
  70. if not auth.set_character_style(style):
  71. abort(404)
  72. return "", 204
  73. @app.errorhandler(404)
  74. def page_not_found(err):
  75. return render_template("404.mako"), 404
  76. if __name__ == "__main__":
  77. app.run(debug=True, port=8080)