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.
 
 
 
 
 

89 lines
2.4 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. catch_exceptions = make_error_catcher(app, "error.mako")
  22. route_restricted = make_route_restricter(
  23. auth, lambda: redirect(url_for("index"), 303))
  24. MakoTemplates(app)
  25. set_up_asset_versioning(app)
  26. config.install(app)
  27. calefaction.enable_logging()
  28. @app.before_request
  29. def prepare_request():
  30. g.auth = auth
  31. g.config = config
  32. g.eve = eve
  33. g.version = calefaction.__version__
  34. app.before_request(Database.pre_hook)
  35. app.teardown_appcontext(Database.post_hook)
  36. @app.route("/")
  37. @catch_exceptions
  38. def index():
  39. success, _ = try_func(auth.is_authenticated)
  40. if success:
  41. return render_template("home.mako")
  42. return render_template("landing.mako")
  43. @app.route("/login", methods=["GET", "POST"])
  44. @catch_exceptions
  45. def login():
  46. code = request.args.get("code")
  47. state = request.args.get("state")
  48. success, caught = try_func(lambda: auth.handle_login(code, state))
  49. if success:
  50. flash(Messages.LOGGED_IN, "success")
  51. elif getattr(g, "_session_expired", False):
  52. flash(Messages.SESSION_EXPIRED, "error")
  53. elif not caught:
  54. flash(Messages.LOGIN_FAILED, "error")
  55. return redirect(url_for("index"), 303)
  56. @app.route("/logout", methods=["GET", "POST"])
  57. @catch_exceptions
  58. def logout():
  59. if request.method == "GET":
  60. return render_template("logout.mako")
  61. auth.handle_logout()
  62. flash(Messages.LOGGED_OUT, "success")
  63. return redirect(url_for("index"), 303)
  64. @app.route("/settings/style/<style>", methods=["POST"])
  65. @catch_exceptions
  66. @route_restricted
  67. def set_style(style):
  68. if not auth.set_character_style(style):
  69. abort(404)
  70. return "", 204
  71. if __name__ == "__main__":
  72. app.run(debug=True, port=8080)