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.
 
 
 
 
 

91 regels
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 not caught:
  52. flash(Messages.LOGIN_FAILED, "error")
  53. return redirect(url_for("index"), 303)
  54. @app.route("/logout", methods=["GET", "POST"])
  55. @catch_exceptions
  56. def logout():
  57. if request.method == "GET":
  58. return render_template("logout.mako")
  59. auth.handle_logout()
  60. flash(Messages.LOGGED_OUT, "success")
  61. return redirect(url_for("index"), 303)
  62. @app.route("/settings/style/<style>", methods=["POST"])
  63. @catch_exceptions
  64. @route_restricted
  65. def set_style(style):
  66. if not auth.set_character_style(style):
  67. abort(404)
  68. return "", 204
  69. @app.errorhandler(404)
  70. def page_not_found(err):
  71. return render_template("404.mako"), 404
  72. if __name__ == "__main__":
  73. app.run(debug=True, port=8080)