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.
 
 
 
 
 

122 lines
3.4 KiB

  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. app.register_blueprint(AdminModule)
  17. app.register_blueprint(AccountModule)
  18. # Custom Jinja handler to support custom- and default-template folders for
  19. # rendering templates.
  20. app.jinja_loader = jinja2.ChoiceLoader([
  21. jinja2.FileSystemLoader("site/www"), # Site specific.
  22. jinja2.FileSystemLoader("rophako/www"), # Default
  23. ])
  24. app.jinja_env.globals["csrf_token"] = rophako.utils.generate_csrf_token
  25. @app.before_request
  26. def before_request():
  27. """Called before all requests. Initialize global template variables."""
  28. # CSRF protection.
  29. if request.method == "POST":
  30. token = session.pop("_csrf", None)
  31. if not token or str(token) != str(request.form.get("token")):
  32. abort(403)
  33. # Default template vars.
  34. g.info = {
  35. "time": time.time(),
  36. "app": {
  37. "name": "Rophako",
  38. "version": __version__,
  39. "author": "Noah Petherbridge",
  40. },
  41. "uri": request.path.split("/")[1:],
  42. "session": {
  43. "login": False, # Not logged in, until proven otherwise.
  44. "username": "guest",
  45. "uid": 0,
  46. "name": "Guest",
  47. "role": "user",
  48. }
  49. }
  50. # Default session vars.
  51. if not "login" in session:
  52. session.update(g.info["session"])
  53. # Refresh their login status from the DB.
  54. if session["login"]:
  55. import rophako.model.user as User
  56. if not User.exists(uid=session["uid"]):
  57. # Weird! Log them out.
  58. from rophako.modules.account import logout
  59. logout()
  60. return
  61. db = User.get_user(uid=session["uid"])
  62. session["username"] = db["username"]
  63. session["name"] = db["name"]
  64. session["role"] = db["role"]
  65. # Copy session params into g.info. The only people who should touch the
  66. # session are the login/out pages.
  67. for key in session:
  68. g.info["session"][key] = session[key]
  69. @app.context_processor
  70. def after_request():
  71. """Called just before render_template. Inject g.info into the template vars."""
  72. g.info["time_elapsed"] = "%.03f" % (time.time() - g.info["time"])
  73. return g.info
  74. @app.route("/<path:path>")
  75. def catchall(path):
  76. """The catch-all path handler. If it exists in the www folders, it's sent,
  77. otherwise we give the 404 error page."""
  78. # Search for this file.
  79. for root in ["site/www", "rophako/www"]:
  80. abspath = os.path.abspath("{}/{}".format(root, path))
  81. if os.path.isfile(abspath):
  82. return send_file(abspath)
  83. elif not "." in path and os.path.isfile(abspath + ".html"):
  84. return render_template(path + ".html")
  85. return not_found("404")
  86. @app.route("/")
  87. def index():
  88. print "INDEX PAGE"
  89. return catchall("index")
  90. @app.errorhandler(404)
  91. def not_found(error):
  92. print "NOT FOUND"
  93. return render_template('errors/404.html', **g.info), 404
  94. # Domain specific endpoints.
  95. if config.SITE_NAME == "kirsle.net":
  96. import rophako.modules.kirsle_legacy