A corporation manager and dashboard for EVE Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

103 lines
3.0 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. """Set up the Flask global context variable with important objects."""
  31. g.auth = auth
  32. g.config = config
  33. g.eve = eve
  34. g.modules = config.modules
  35. g.version = calefaction.__version__
  36. app.before_request(Database.pre_hook)
  37. app.teardown_appcontext(Database.post_hook)
  38. @app.route("/")
  39. @app.catch_exceptions
  40. def index():
  41. """Render and return the index page.
  42. This is a informational landing page for non-logged-in users, and the corp
  43. homepage for those who are logged in.
  44. """
  45. success, _ = try_func(auth.is_authenticated)
  46. if success:
  47. module = config.get("modules.home")
  48. if module:
  49. return config.modules[module].home()
  50. return render_template("default_home.mako")
  51. return render_template("landing.mako")
  52. @app.route("/login", methods=["GET", "POST"])
  53. @app.catch_exceptions
  54. def login():
  55. """Handle the last step of a SSO login request."""
  56. code = request.values.get("code")
  57. state = request.values.get("state")
  58. success, caught = try_func(lambda: auth.handle_login(code, state))
  59. if success:
  60. flash(Messages.LOGGED_IN, "success")
  61. elif not caught:
  62. flash(Messages.LOGIN_FAILED, "error")
  63. return redirect(url_for("index"), 303)
  64. @app.route("/logout", methods=["GET", "POST"])
  65. @app.catch_exceptions
  66. def logout():
  67. """Log the user out (POST), or ask them to confirm a log out (GET)."""
  68. if request.method == "GET":
  69. return render_template("logout.mako")
  70. auth.handle_logout()
  71. flash(Messages.LOGGED_OUT, "success")
  72. return redirect(url_for("index"), 303)
  73. @app.route("/settings/style/<style>", methods=["POST"])
  74. @app.catch_exceptions
  75. @app.route_restricted
  76. def set_style(style):
  77. """Set the user's style preference."""
  78. if not auth.set_character_style(style):
  79. abort(404)
  80. return "", 204
  81. @app.errorhandler(404)
  82. @app.catch_exceptions
  83. def page_not_found(err):
  84. """Render and return the 404 error template."""
  85. return render_template("404.mako"), 404