A Python content management system designed for kirsle.net featuring a blog, comments and photo albums. https://rophako.kirsle.net/
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.

__init__.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. __version__ = '0.01'
  2. from flask import Flask, g, request, session, render_template, send_file, abort
  3. import jinja2
  4. import os.path
  5. import time
  6. import config
  7. import rophako.utils
  8. app = Flask(__name__,
  9. static_url_path="/.static",
  10. )
  11. app.DEBUG = config.DEBUG
  12. app.secret_key = config.SECRET_KEY
  13. # Load all the blueprints!
  14. from rophako.modules.admin import mod as AdminModule
  15. from rophako.modules.account import mod as AccountModule
  16. from rophako.modules.blog import mod as BlogModule
  17. from rophako.modules.photo import mod as PhotoModule
  18. from rophako.modules.comment import mod as CommentModule
  19. from rophako.modules.emoticons import mod as EmoticonsModule
  20. from rophako.modules.contact import mod as ContactModule
  21. app.register_blueprint(AdminModule)
  22. app.register_blueprint(AccountModule)
  23. app.register_blueprint(BlogModule)
  24. app.register_blueprint(PhotoModule)
  25. app.register_blueprint(CommentModule)
  26. app.register_blueprint(EmoticonsModule)
  27. app.register_blueprint(ContactModule)
  28. # Custom Jinja handler to support custom- and default-template folders for
  29. # rendering templates.
  30. app.jinja_loader = jinja2.ChoiceLoader([
  31. jinja2.FileSystemLoader(config.SITE_ROOT), # Site specific.
  32. jinja2.FileSystemLoader("rophako/www"), # Default/fall-back
  33. ])
  34. app.jinja_env.globals["csrf_token"] = rophako.utils.generate_csrf_token
  35. app.jinja_env.globals["include_page"] = rophako.utils.include
  36. # Preload the emoticon data.
  37. import rophako.model.emoticons as Emoticons
  38. Emoticons.load_theme()
  39. @app.before_request
  40. def before_request():
  41. """Called before all requests. Initialize global template variables."""
  42. # CSRF protection.
  43. if request.method == "POST":
  44. token = session.pop("_csrf", None)
  45. if not token or str(token) != str(request.form.get("token")):
  46. abort(403)
  47. # Default template vars.
  48. g.info = {
  49. "time": time.time(),
  50. "app": {
  51. "name": "Rophako",
  52. "version": __version__,
  53. "author": "Noah Petherbridge",
  54. "photo_url": config.PHOTO_ROOT_PUBLIC,
  55. },
  56. "uri": request.path.split("/")[1:],
  57. "session": {
  58. "login": False, # Not logged in, until proven otherwise.
  59. "username": "guest",
  60. "uid": 0,
  61. "name": "Guest",
  62. "role": "user",
  63. }
  64. }
  65. # Default session vars.
  66. if not "login" in session:
  67. session.update(g.info["session"])
  68. # Refresh their login status from the DB.
  69. if session["login"]:
  70. import rophako.model.user as User
  71. if not User.exists(uid=session["uid"]):
  72. # Weird! Log them out.
  73. from rophako.modules.account import logout
  74. logout()
  75. return
  76. db = User.get_user(uid=session["uid"])
  77. session["username"] = db["username"]
  78. session["name"] = db["name"]
  79. session["role"] = db["role"]
  80. # Copy session params into g.info. The only people who should touch the
  81. # session are the login/out pages.
  82. for key in session:
  83. g.info["session"][key] = session[key]
  84. @app.context_processor
  85. def after_request():
  86. """Called just before render_template. Inject g.info into the template vars."""
  87. return g.info
  88. @app.route("/<path:path>")
  89. def catchall(path):
  90. """The catch-all path handler. If it exists in the www folders, it's sent,
  91. otherwise we give the 404 error page."""
  92. # Search for this file.
  93. for root in [config.SITE_ROOT, "rophako/www"]:
  94. abspath = os.path.abspath("{}/{}".format(root, path))
  95. if os.path.isfile(abspath):
  96. return send_file(abspath)
  97. elif not "." in path and os.path.isfile(abspath + ".html"):
  98. return rophako.utils.template(path + ".html")
  99. elif not "." in path and os.path.isfile(abspath + "/index.html"):
  100. return rophako.utils.template(path + "/index.html")
  101. return not_found("404")
  102. @app.route("/")
  103. def index():
  104. return catchall("index")
  105. @app.errorhandler(404)
  106. def not_found(error):
  107. return render_template('errors/404.html', **g.info), 404
  108. # Domain specific endpoints.
  109. if config.SITE_NAME == "kirsle.net":
  110. import rophako.modules.kirsle_legacy