2014-02-19 06:30:21 +00:00
|
|
|
__version__ = '0.01'
|
|
|
|
|
2014-02-20 07:51:18 +00:00
|
|
|
from flask import Flask, g, request, session, render_template, send_file, abort
|
2014-02-19 06:30:21 +00:00
|
|
|
import jinja2
|
|
|
|
import os.path
|
2014-02-20 07:51:18 +00:00
|
|
|
import time
|
2014-02-19 06:30:21 +00:00
|
|
|
|
|
|
|
import config
|
2014-02-20 07:51:18 +00:00
|
|
|
import rophako.utils
|
2014-02-19 06:30:21 +00:00
|
|
|
|
2014-02-20 07:51:18 +00:00
|
|
|
app = Flask(__name__,
|
|
|
|
static_url_path="/.static",
|
|
|
|
)
|
2014-02-19 06:30:21 +00:00
|
|
|
app.DEBUG = config.DEBUG
|
2014-02-20 07:51:18 +00:00
|
|
|
app.secret_key = config.SECRET_KEY
|
2014-02-19 06:30:21 +00:00
|
|
|
|
|
|
|
# Load all the blueprints!
|
2014-02-20 07:51:18 +00:00
|
|
|
from rophako.modules.admin import mod as AdminModule
|
2014-02-19 06:30:21 +00:00
|
|
|
from rophako.modules.account import mod as AccountModule
|
2014-03-28 08:23:12 +00:00
|
|
|
from rophako.modules.blog import mod as BlogModule
|
2014-04-05 07:47:43 +00:00
|
|
|
from rophako.modules.photo import mod as PhotoModule
|
2014-04-06 22:45:43 +00:00
|
|
|
from rophako.modules.comment import mod as CommentModule
|
|
|
|
from rophako.modules.emoticons import mod as EmoticonsModule
|
2014-04-07 01:02:34 +00:00
|
|
|
from rophako.modules.contact import mod as ContactModule
|
2014-02-20 07:51:18 +00:00
|
|
|
app.register_blueprint(AdminModule)
|
2014-02-19 06:30:21 +00:00
|
|
|
app.register_blueprint(AccountModule)
|
2014-03-28 08:23:12 +00:00
|
|
|
app.register_blueprint(BlogModule)
|
2014-04-05 07:47:43 +00:00
|
|
|
app.register_blueprint(PhotoModule)
|
2014-04-06 22:45:43 +00:00
|
|
|
app.register_blueprint(CommentModule)
|
|
|
|
app.register_blueprint(EmoticonsModule)
|
2014-04-07 01:02:34 +00:00
|
|
|
app.register_blueprint(ContactModule)
|
2014-02-19 06:30:21 +00:00
|
|
|
|
|
|
|
# Custom Jinja handler to support custom- and default-template folders for
|
|
|
|
# rendering templates.
|
|
|
|
app.jinja_loader = jinja2.ChoiceLoader([
|
2014-04-09 22:06:01 +00:00
|
|
|
jinja2.FileSystemLoader(config.SITE_ROOT), # Site specific.
|
|
|
|
jinja2.FileSystemLoader("rophako/www"), # Default/fall-back
|
2014-02-19 06:30:21 +00:00
|
|
|
])
|
|
|
|
|
2014-02-20 07:51:18 +00:00
|
|
|
app.jinja_env.globals["csrf_token"] = rophako.utils.generate_csrf_token
|
2014-03-28 08:23:12 +00:00
|
|
|
app.jinja_env.globals["include_page"] = rophako.utils.include
|
2014-02-20 07:51:18 +00:00
|
|
|
|
2014-04-06 22:45:43 +00:00
|
|
|
# Preload the emoticon data.
|
|
|
|
import rophako.model.emoticons as Emoticons
|
|
|
|
Emoticons.load_theme()
|
|
|
|
|
2014-02-19 06:30:21 +00:00
|
|
|
|
|
|
|
@app.before_request
|
|
|
|
def before_request():
|
|
|
|
"""Called before all requests. Initialize global template variables."""
|
|
|
|
|
2014-02-20 07:51:18 +00:00
|
|
|
# CSRF protection.
|
|
|
|
if request.method == "POST":
|
|
|
|
token = session.pop("_csrf", None)
|
|
|
|
if not token or str(token) != str(request.form.get("token")):
|
|
|
|
abort(403)
|
|
|
|
|
2014-02-19 06:30:21 +00:00
|
|
|
# Default template vars.
|
|
|
|
g.info = {
|
2014-02-20 07:51:18 +00:00
|
|
|
"time": time.time(),
|
2014-02-19 06:30:21 +00:00
|
|
|
"app": {
|
|
|
|
"name": "Rophako",
|
|
|
|
"version": __version__,
|
|
|
|
"author": "Noah Petherbridge",
|
2014-04-05 07:47:43 +00:00
|
|
|
"photo_url": config.PHOTO_ROOT_PUBLIC,
|
2014-02-19 06:30:21 +00:00
|
|
|
},
|
2014-04-09 22:46:37 +00:00
|
|
|
"uri": request.path,
|
2014-02-20 07:51:18 +00:00
|
|
|
"session": {
|
|
|
|
"login": False, # Not logged in, until proven otherwise.
|
|
|
|
"username": "guest",
|
|
|
|
"uid": 0,
|
|
|
|
"name": "Guest",
|
|
|
|
"role": "user",
|
|
|
|
}
|
2014-02-19 06:30:21 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 07:51:18 +00:00
|
|
|
# Default session vars.
|
|
|
|
if not "login" in session:
|
|
|
|
session.update(g.info["session"])
|
|
|
|
|
|
|
|
# Refresh their login status from the DB.
|
|
|
|
if session["login"]:
|
|
|
|
import rophako.model.user as User
|
|
|
|
if not User.exists(uid=session["uid"]):
|
|
|
|
# Weird! Log them out.
|
|
|
|
from rophako.modules.account import logout
|
|
|
|
logout()
|
|
|
|
return
|
|
|
|
|
|
|
|
db = User.get_user(uid=session["uid"])
|
|
|
|
session["username"] = db["username"]
|
|
|
|
session["name"] = db["name"]
|
|
|
|
session["role"] = db["role"]
|
|
|
|
|
|
|
|
# Copy session params into g.info. The only people who should touch the
|
|
|
|
# session are the login/out pages.
|
|
|
|
for key in session:
|
|
|
|
g.info["session"][key] = session[key]
|
|
|
|
|
2014-02-19 06:30:21 +00:00
|
|
|
|
|
|
|
@app.context_processor
|
|
|
|
def after_request():
|
|
|
|
"""Called just before render_template. Inject g.info into the template vars."""
|
|
|
|
return g.info
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/<path:path>")
|
|
|
|
def catchall(path):
|
|
|
|
"""The catch-all path handler. If it exists in the www folders, it's sent,
|
|
|
|
otherwise we give the 404 error page."""
|
|
|
|
|
|
|
|
# Search for this file.
|
2014-04-09 22:06:01 +00:00
|
|
|
for root in [config.SITE_ROOT, "rophako/www"]:
|
2014-02-19 06:30:21 +00:00
|
|
|
abspath = os.path.abspath("{}/{}".format(root, path))
|
|
|
|
if os.path.isfile(abspath):
|
|
|
|
return send_file(abspath)
|
|
|
|
elif not "." in path and os.path.isfile(abspath + ".html"):
|
2014-03-28 08:23:12 +00:00
|
|
|
return rophako.utils.template(path + ".html")
|
2014-04-07 01:02:34 +00:00
|
|
|
elif not "." in path and os.path.isfile(abspath + "/index.html"):
|
|
|
|
return rophako.utils.template(path + "/index.html")
|
2014-02-19 06:30:21 +00:00
|
|
|
|
|
|
|
return not_found("404")
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def index():
|
|
|
|
return catchall("index")
|
|
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(404)
|
|
|
|
def not_found(error):
|
|
|
|
return render_template('errors/404.html', **g.info), 404
|
|
|
|
|
2014-02-20 07:51:18 +00:00
|
|
|
|
2014-02-19 06:30:21 +00:00
|
|
|
# Domain specific endpoints.
|
|
|
|
if config.SITE_NAME == "kirsle.net":
|
|
|
|
import rophako.modules.kirsle_legacy
|