add templates and static files

This commit is contained in:
Clemens Klug 2018-10-17 18:13:11 +02:00
parent 9ec7d6fe8c
commit 21a046f537
8 changed files with 59 additions and 50 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__/

7
Readme.rst Normal file
View File

@ -0,0 +1,7 @@
Run
* `pip3 install -r src/requirements.txt`
* `cd src`
* `FLASK_APP=spammer.py flask run`
Docker
* `docker-compose up`

View File

@ -3,4 +3,6 @@ version: "3"
services:
rzspam:
build: .
image: docker.wiai.de/fswiai/rzspam:0.1
image: docker.wiai.de/fswiai/rzspam:0.1
ports:
- 5000:5000

View File

@ -2,11 +2,9 @@ import json
from collections import namedtuple
from string import Formatter
from flask import Flask
from flask import request
from flask import Flask, render_template, request
from flask_mail import Mail, Message
import templates
Placeholder = namedtuple("Placeholder", ["name", "type", "desc", "default"])
@ -22,20 +20,22 @@ def load_config(conf="config.json"):
default=p_h.get("default"))
placeholders.append(p)
config.pop("placeholders")
flat = {}
flat = []
for org in config:
target = config[org]
templates = []
for issue_name in target["templates"]:
text = target["templates"][issue_name]
fields = [name for _, name, _, _ in Formatter().parse(text)]
flat[f"{org}: {issue_name}"] = {
name = f"{org}: {issue_name}"
value = json.dumps({
"org": org,
"name": issue_name,
"mail": config[org]["mail"],
"text": text,
"placeholders": [i._asdict() for i in placeholders if i.name in fields]
}
})
flat.append({"name": name, "value": value})
return flat
MAIL_SERVER = "smtp.uni-bamberg.de"
@ -56,7 +56,7 @@ issues = load_config()
@app.route("/")
def index():
return templates.format_index(issues)
return render_template("index.html", issues=issues)
@app.route("/send", methods=["POST"])
def send():
@ -73,7 +73,8 @@ def send():
recipients = [request.form["target"]]
msg = Message("Störungsmeldung", body=text, sender=sender, recipients=recipients)
print(msg)
return str(mail.send(msg))
if not mail.send(msg) is None:
return "Success!"
print([(field,field in request.form) for field in fields])
return "2"
print([(field,field in request.form) for field in ("text", "sender", "target")])

View File

@ -1,9 +1,3 @@
import json
BASE = """<!doctype html>
<html>
<head>
<title>spammer</title>
<script>
function selection() {
var x = document.getElementById("issue").value;
var data = JSON.parse(x)
@ -39,38 +33,4 @@ function selection() {
z.appendChild(x);
placeholders.appendChild(z);
}
}
</script>
</head>
<body>
"""
INDEX = """<h1>Welcome</h1>
<p>choose an issue, fill the placeholders, enter your uni-mail, then hit send</p>
<form action="send" method="post">
<p>
<select name="issue" required onchange="selection()" id="issue">
<option selected disabled="true">---</option>
{issues}
</select>
</p>
<p><textarea id="text" readonly required name="text"></textarea></p>
<p>
<label for="sender">absender:</label>
<input type="email" required="true" name="sender" id="sender" placeholder="sender email" readonly>
</p>
<div id="placeholders" ></div>
<input type="submit">
</form>
"""
def format_page(body):
return BASE + str(body) + "</body>\n</html>"
def format_index(issues):
html = ""
for issue in issues:
html+=f"<option value='{json.dumps(issues[issue])}'>{issue}</option>\n"
index = INDEX.format(issues=html)
return format_page(index)
}

3
src/static/style.css Normal file
View File

@ -0,0 +1,3 @@
body {
background-color: aqua;
}

11
src/templates/base.html Normal file
View File

@ -0,0 +1,11 @@
<!doctype html>
<html>
<head>
<title>spammer</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
<script type="application/javascript" src="{{url_for('static', filename='script.js') }}"></script>
</head>
<body>
{% block body %} {% endblock %}
</body>
</html>

24
src/templates/index.html Normal file
View File

@ -0,0 +1,24 @@
{% extends "base.html" %}
{% block body %}
<h1>Welcome</h1>
<p>choose an issue, fill the placeholders, enter your uni-mail, then hit send</p>
<form action="send" method="post">
<p>
<select name="issue" required onchange="selection()" id="issue">
<option selected disabled="true">---</option>
{% for issue in issues %}
<option value="{{issue.value}}">{{ issue.name }}</option>
{% endfor %}
</select>
</p>
<p><textarea id="text" readonly required name="text"></textarea></p>
<p>
<label for="sender">absender:</label>
<input type="email" required="true" name="sender" id="sender" placeholder="sender email" readonly>
</p>
<div id="placeholders" ></div>
<input type="submit">
</form>
{% endblock %}