Moved html files out out python templates to html files

master
noah metz 2022-11-29 00:51:03 -07:00
parent 6cede40a29
commit 0e2e2c97b5
11 changed files with 124 additions and 156 deletions

1
.gitignore vendored

@ -1,3 +1,4 @@
.ldap_creds
.kadmin_creds
venv/
.venv

@ -359,113 +359,6 @@ def get_default_kadmin_creds():
pw = ""
return (princ, pw)
css_style = """
<link rel="preload" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/apple_-webfont.woff2" as="font" type="font/woff2" crossorigin />
<link rel="preload" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/apple_-webfont.woff" as="font" type="font/woff" crossorigin />
<link rel="stylesheet" type="text/css" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/stylesheet.css"/>
<link rel="stylesheet" type="text/css" href="/static/metznet.css" />
"""
torrents_js = """
<script type=text/javascript src="/static/torrents.js"></script>
"""
newline='\n'
class HTMLElement:
def __str__(self):
return self.html
class InputField(HTMLElement):
def __init__(self, label, name, input_type, additional=""):
self.html = f"""
<div>
{label}
</div>
<div>
<input name="{name}" type="{input_type}" {additional} />
</div>
"""
class WebGridBox(HTMLElement):
def __init__(self, content, cols=None, rows=None, text_align="center"):
col_css = None
if cols is not None:
col_css = f"grid-column: span {cols}; "
row_css = None
if rows is not None:
row_css = f"grid-row: span {rows}; "
style_html = f"style=\"{col_css or ''}{row_css or ''}text-align: {text_align};\""
self.html = f"""
<div {style_html}>
{content}
</div>
"""
class WebGrid(HTMLElement):
def __init__(self, content: List[Any], grid_template_columns, grid_template_rows=None, cls="grid"):
col_css = f"grid-template-columns: {grid_template_columns};"
row_css = ""
if grid_template_rows is not None:
row_css = f"grid-template-rows: {grid_template_rows};"
self.html = f"""
<div class="grid" style="{col_css} {row_css}">
{newline.join([str(entry) for entry in content])}
</div>
"""
class WebForm(HTMLElement):
def __init__(self, action, name, fields: List[InputField], button_label=None):
self.html = f"""
<div class="form">
<form action="{action}" method="post" enctype="multipart/form-data">
<div class="form-fields">
<div style="grid-column: span 2; grid-row: span 2; text-align: center">
<header>{name}</header>
</div>
{newline.join([str(field) for field in fields])}
<div>
<button>{button_label}</button>
</div>
</div>
</form>
</div>
"""
resize_script = """
<script src="/static/resize.js"></script>
"""
class WebPage(HTMLElement):
def __init__(self, bodies: List[Any] = [], headers: List [Any] = []):
self.html = f"""
<!DOCTYPE html>
<html>
<head>
{resize_script}
{css_style}
{newline.join([str(header) for header in headers])}
</head>
<body>
{newline.join([str(body) for body in bodies])}
</body>
</html>
"""
class WebDiv(HTMLElement):
def __init__(self, content=[], cls=None, id=None):
class_css = ""
if cls is not None:
class_css = f"class=\"{cls}\""
id_html = ""
if id is not None:
id_html = f"id=\"{id}\""
self.html = f"""
<div {class_css} {id_html}>
{newline.join([str(element) for element in content])}
</div>
"""
def metznet_webui(args):
from bottle import get, post, run, request, static_file
from os import getcwd
@ -489,27 +382,15 @@ def metznet_webui(args):
@get("/static/<filename>")
def static_files(filename):
return static_file(filename, root=f"{getcwd()}/src/")
return static_file(filename, root=f"{getcwd()}/src/static")
@get("/")
def root_page():
services = {
"Plex: Watch TV and Movies": "https://plex.metznet.ca",
"Torrents: Download Files": "/static/torrents.html",
"Raid: Access Files": "/raid",
"Mastodon: Social Media Site": "https://mastodon.metznet.ca",
"Nextcloud: Files and E-Mail": "https://cloud.metznet.ca",
"Password Change": "/changepw",
"Public Key Change": "/changekey",
}
service_boxes = []
for service in services:
service_boxes.append(WebGridBox(f"<a href=\"{services[service]}\">{service}</a>", text_align="left"))
boxes = [WebGridBox("<header>MetzNet<br>Service List</header>", 1, 2)] + service_boxes
return WebPage([WebGrid(boxes, "500px", f"repeat({len(services) + 2}, 50px)")]).html
@post("/changekey")
def do_changekey():
@get("/<filename>")
def root_files(filename="index.html"):
return static_file(filename, root=f"{getcwd()}/src")
@post("/changekey.html")
def post_changekey():
change = KeyChange()
change.uid = request.forms.get("username")
change.old_pw = request.forms.get("password")
@ -520,43 +401,20 @@ def metznet_webui(args):
pubkey_bytes.append(string)
change.pubkeys = pubkey_bytes
if changekey_account(change):
return WebPage(["<p>Success!</p>"]).html
return static_file("success.html", root=f"{getcwd()}/src")
else:
return WebPage(["<p>Fail :(</p>"]).html
@get("/changekey")
def changekey_form():
return WebPage([WebForm("/changekey",
"MetzNet<br>Key Change",
[
InputField("Username:", "username", "text"),
InputField("Password:", "password", "password"),
InputField("Public Keys:", "pubkeys", "file", "multiple")
],
"Change Keys")]).html
@post("/changepw")
def do_changepw():
return static_file("fail.html", root=f"{getcwd()}/src")
@post("/changepw.html")
def post_changepw():
change = PWChange()
change.uid = request.forms.get("username")
change.old_pw = request.forms.get("password")
change.new_pw = request.forms.get("new_password")
if changepw_account(change):
return WebPage(["<p>Success!</p>"]).html
return static_file("success.html", root=f"{getcwd()}/src")
else:
return WebPage(["<p>Fail :(</p>"]).html
@get("/changepw")
def changepw_form():
return WebPage([WebForm("/changepw",
"MetzNet<br>Password Change",
[
InputField("Username:", "username", "text"),
InputField("Password:", "password", "password"),
InputField("New Password:", "new_password", "password"),
],
"Change Password")]).html
return static_file("fail.html", root=f"{getcwd()}/src")
run(host='0.0.0.0', port=8024)

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<script src="/static/resize.js"></script>
<link rel="preload" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/apple_-webfont.woff2" as="font" type="font/woff2" crossorigin />
<link rel="preload" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/apple_-webfont.woff" as="font" type="font/woff" crossorigin />
<link rel="stylesheet" type="text/css" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/stylesheet.css"/>
<link rel="stylesheet" type="text/css" href="/static/metznet.css" />
</head>
<body>
<div class="form">
<form action="/changekey.html" method="post" enctype="multipart/form-data">
<div class="form-fields">
<div style="grid-column: span 2; grid-row: span 2; text-align: center">
<header>MetzNet<br>Key Change</header>
</div>
<div>Username:</div><div><input name="username" type="text"/></div>
<div>Password:</div><div><input name="password" type="password"/></div>
<div>Public Keys:</div><div><input name="pubkeys" type="file" multiple/></div>
<div><button>Change Keys</button></div>
</div>
</form>
</div>
</body>
</html>

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<script src="/static/resize.js"></script>
<link rel="preload" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/apple_-webfont.woff2" as="font" type="font/woff2" crossorigin />
<link rel="preload" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/apple_-webfont.woff" as="font" type="font/woff" crossorigin />
<link rel="stylesheet" type="text/css" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/stylesheet.css"/>
<link rel="stylesheet" type="text/css" href="/static/metznet.css" />
</head>
<body>
<div class="form">
<form action="/changepw.html" method="post" enctype="multipart/form-data">
<div class="form-fields">
<div style="grid-column: span 2; grid-row: span 2; text-align: center">
<header>MetzNet<br>Password Change</header>
</div>
<div>Username:</div><div><input name="username" type="text"/></div>
<div>Password:</div><div><input name="password" type="password"/></div>
<div>New Password:</div><div><input name="new_password" type="password" /></div>
<div><button>Change Password</button></div>
</div>
</form>
</div>
</body>
</html>

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<script src="/static/resize.js"></script>
<link rel="preload" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/apple_-webfont.woff2" as="font" type="font/woff2" crossorigin />
<link rel="preload" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/apple_-webfont.woff" as="font" type="font/woff" crossorigin />
<link rel="stylesheet" type="text/css" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/stylesheet.css"/>
<link rel="stylesheet" type="text/css" href="/static/metznet.css" />
</head>
<body>
<p>Fail</p>
</body>
</html>

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<script src="/static/resize.js"></script>
<link rel="preload" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/apple_-webfont.woff2" as="font" type="font/woff2" crossorigin />
<link rel="preload" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/apple_-webfont.woff" as="font" type="font/woff" crossorigin />
<link rel="stylesheet" type="text/css" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/stylesheet.css"/>
<link rel="stylesheet" type="text/css" href="/static/metznet.css" />
</head>
<body>
<div class="grid" style="grid-template-columns: 500px; grid-template-rows: repeat(9, 50px);">
<div style="grid-column: span 1; grid-row: span 2; text-align: center;">
<header>MetzNet<br>Service List</header>
</div>
<div><a href="https://plex.metznet.ca">Plex: Watch TV and Movies</a></div>
<div><a href="torrents.html">Torrents: Download Files</a></div>
<div><a href="/raid">Raid: Access Files</a></div>
<div><a href="https://mastodon.metznet.ca">Mastodon: Social Media Site</a></div>
<div><a href="https://cloud.metznet.ca">Nextcloud: Files and E-Mail</a></div>
<div><a href="/changepw.html">Password Change</a></div>
<div><a href="/changekey.html">Public Key Change</a></div>
</div>
</body>
</html>

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<script src="/static/resize.js"></script>
<link rel="preload" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/apple_-webfont.woff2" as="font" type="font/woff2" crossorigin />
<link rel="preload" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/apple_-webfont.woff" as="font" type="font/woff" crossorigin />
<link rel="stylesheet" type="text/css" href="https://s3.metznet.ca/static.metznet.ca/fonts/appleii/stylesheet.css"/>
<link rel="stylesheet" type="text/css" href="/static/metznet.css" />
</head>
<body>
<p>Success</p>
</body>
</html>