# -*- coding: utf-8 -*- from flask import g, session, request, render_template, flash, redirect, url_for from functools import wraps import codecs import uuid import datetime import time import re import importlib import smtplib import markdown from rophako.log import logger from config import * def login_required(f): """Wrapper for pages that require a logged-in user.""" @wraps(f) def decorated_function(*args, **kwargs): if not g.info["session"]["login"]: session["redirect_url"] = request.url flash("You must be logged in to do that!") return redirect(url_for("account.login")) return f(*args, **kwargs) return decorated_function def admin_required(f): """Wrapper for admin-only pages. Implies login_required.""" @wraps(f) def decorated_function(*args, **kwargs): if not g.info["session"]["login"]: # Not even logged in? session["redirect_url"] = request.url flash("You must be logged in to do that!") return redirect(url_for("account.login")) if g.info["session"]["role"] != "admin": logger.warning("User tried to access an Admin page, but wasn't allowed!") return redirect(url_for("index")) return f(*args, **kwargs) return decorated_function def template(name, **kwargs): """Render a template to the browser.""" html = render_template(name, **kwargs) # Get the elapsed time for the request. time_elapsed = "%.03f" % (time.time() - g.info["time"]) html = re.sub(r'\%time_elapsed\%', time_elapsed, html) return html def markdown_template(path): """Render a Markdown page to the browser.""" # The path is the absolute path to the Markdown file, so open it directly. fh = codecs.open(path, "r", "utf-8") body = fh.read() fh.close() # Extract a title from the first line. first = body.split("\n")[0] if first.startswith("#"): first = first[1:].strip() rendered = render_markdown(body) return template("markdown.inc.html", title=first, markdown=rendered, ) def render_markdown(body, html_escape=True): """Render a block of Markdown text. This will default to escaping literal HTML characters. Set `html_escape=False` to trust HTML.""" args = dict( lazy_ol=False, # If a numbered list starts at e.g. 4, show the