From 9a1bc8be03489d5f7e1042998979b473b271bb7a Mon Sep 17 00:00:00 2001 From: Krateng Date: Mon, 17 Aug 2020 17:14:38 +0200 Subject: [PATCH] Fixed error page --- maloja/server.py | 83 +++++++++++++++-------------- maloja/web/jinja/error.jinja | 18 +++++++ maloja/web/pyhp/errors/generic.pyhp | 28 ---------- 3 files changed, 60 insertions(+), 69 deletions(-) create mode 100644 maloja/web/jinja/error.jinja delete mode 100644 maloja/web/pyhp/errors/generic.pyhp diff --git a/maloja/server.py b/maloja/server.py index e00ab47..d8707a8 100755 --- a/maloja/server.py +++ b/maloja/server.py @@ -4,7 +4,7 @@ from .globalconf import datadir, DATA_DIR # server stuff -from bottle import Bottle, route, get, post, error, run, template, static_file, request, response, FormsDict, redirect, template, HTTPResponse, BaseRequest +from bottle import Bottle, route, get, post, error, run, template, static_file, request, response, FormsDict, redirect, template, HTTPResponse, BaseRequest, abort import waitress # templating from jinja2 import Environment, PackageLoader, select_autoescape @@ -94,10 +94,9 @@ def mainpage(): def customerror(error): code = int(str(error).split(",")[0][1:]) - if os.path.exists(pthjoin(WEBFOLDER,"errors",str(code) + ".pyhp")): - return pyhpfile(pthjoin(WEBFOLDER,"errors",str(code) + ".pyhp"),{"errorcode":code}) - else: - return pyhpfile(pthjoin(WEBFOLDER,"errors","generic.pyhp"),{"errorcode":code}) + template = jinjaenv.get_template('error.jinja') + res = template.render(errorcode=code) + return res @@ -222,7 +221,7 @@ jinjaenv = Environment( jinjaenv.globals.update(JINJA_CONTEXT) -@webserver.route("/") +@webserver.route("/") @auth.authenticated def static_html_private(name): return static_html(name) @@ -291,48 +290,50 @@ def static_html(name): # if not, use the old way else: + try: + with open(pthjoin(WEBFOLDER,name + ".html")) as htmlfile: + html = htmlfile.read() - with open(pthjoin(WEBFOLDER,name + ".html")) as htmlfile: - html = htmlfile.read() - - # apply global substitutions - with open(pthjoin(WEBFOLDER,"common/footer.html")) as footerfile: - footerhtml = footerfile.read() - with open(pthjoin(WEBFOLDER,"common/header.html")) as headerfile: - headerhtml = headerfile.read() - html = html.replace("",footerhtml + "").replace("",headerhtml + "") + # apply global substitutions + with open(pthjoin(WEBFOLDER,"common/footer.html")) as footerfile: + footerhtml = footerfile.read() + with open(pthjoin(WEBFOLDER,"common/header.html")) as headerfile: + headerhtml = headerfile.read() + html = html.replace("",footerhtml + "").replace("",headerhtml + "") - # If a python file exists, it provides the replacement dict for the html file - if os.path.exists(pthjoin(WEBFOLDER,name + ".py")): - #txt_keys = SourceFileLoader(name,"web/" + name + ".py").load_module().replacedict(keys,DATABASE_PORT) - try: - module = importlib.import_module(".web." + name,package="maloja") - txt_keys,resources = module.instructions(keys) - except Exception as e: - log("Error in website generation: " + str(sys.exc_info()),module="error") - raise + # If a python file exists, it provides the replacement dict for the html file + if os.path.exists(pthjoin(WEBFOLDER,name + ".py")): + #txt_keys = SourceFileLoader(name,"web/" + name + ".py").load_module().replacedict(keys,DATABASE_PORT) + try: + module = importlib.import_module(".web." + name,package="maloja") + txt_keys,resources = module.instructions(keys) + except Exception as e: + log("Error in website generation: " + str(sys.exc_info()),module="error") + raise - # add headers for server push - for resource in resources: - if all(ord(c) < 128 for c in resource["file"]): - # we can only put ascii stuff in the http header - linkheaders.append("<" + resource["file"] + ">; rel=preload; as=" + resource["type"]) + # add headers for server push + for resource in resources: + if all(ord(c) < 128 for c in resource["file"]): + # we can only put ascii stuff in the http header + linkheaders.append("<" + resource["file"] + ">; rel=preload; as=" + resource["type"]) - # apply key substitutions - for k in txt_keys: - if isinstance(txt_keys[k],list): - # if list, we replace each occurence with the next item - for element in txt_keys[k]: - html = html.replace(k,element,1) - else: - html = html.replace(k,txt_keys[k]) + # apply key substitutions + for k in txt_keys: + if isinstance(txt_keys[k],list): + # if list, we replace each occurence with the next item + for element in txt_keys[k]: + html = html.replace(k,element,1) + else: + html = html.replace(k,txt_keys[k]) - response.set_header("Link",",".join(linkheaders)) - log("Generated page {name} in {time:.5f}s (Python+HTML)".format(name=name,time=clock.stop()),module="debug") - return html - #return static_file("web/" + name + ".html",root="") + response.set_header("Link",",".join(linkheaders)) + log("Generated page {name} in {time:.5f}s (Python+HTML)".format(name=name,time=clock.stop()),module="debug") + return html + + except: + abort(404, "Page does not exist") # Shortlinks diff --git a/maloja/web/jinja/error.jinja b/maloja/web/jinja/error.jinja new file mode 100644 index 0000000..5ce08a2 --- /dev/null +++ b/maloja/web/jinja/error.jinja @@ -0,0 +1,18 @@ +{% extends "admin.jinja" %} + +{% block content %} + + + + + +
+
+
+

Error {{ errorcode }}


+ + +

That did not work. Don't ask me why.

+
+ +{% endblock %} diff --git a/maloja/web/pyhp/errors/generic.pyhp b/maloja/web/pyhp/errors/generic.pyhp deleted file mode 100644 index aafa2e1..0000000 --- a/maloja/web/pyhp/errors/generic.pyhp +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - Maloja - Error <pyhp echo="errorcode" /> - - - - - - - - - -
-
-
-

Error


- - -

That did not work. Don't ask me why.

-
- - - - -