diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/Readme.rst b/Readme.rst new file mode 100644 index 0000000..d18e732 --- /dev/null +++ b/Readme.rst @@ -0,0 +1,7 @@ +Run +* `pip3 install -r src/requirements.txt` +* `cd src` +* `FLASK_APP=spammer.py flask run` + +Docker +* `docker-compose up` diff --git a/docker-compose.yml b/docker-compose.yml index 2d61382..e4df720 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,4 +3,6 @@ version: "3" services: rzspam: build: . - image: docker.wiai.de/fswiai/rzspam:0.1 \ No newline at end of file + image: docker.wiai.de/fswiai/rzspam:0.1 + ports: + - 5000:5000 \ No newline at end of file diff --git a/src/spammer.py b/src/spammer.py index a57cb89..b39adc0 100644 --- a/src/spammer.py +++ b/src/spammer.py @@ -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")]) diff --git a/src/templates.py b/src/static/script.js similarity index 56% rename from src/templates.py rename to src/static/script.js index f6ed084..f348817 100644 --- a/src/templates.py +++ b/src/static/script.js @@ -1,9 +1,3 @@ -import json -BASE = """ - -
-choose an issue, fill the placeholders, enter your uni-mail, then hit send
- - -""" - -def format_page(body): - return BASE + str(body) + "\n" - -def format_index(issues): - html = "" - for issue in issues: - html+=f"\n" - index = INDEX.format(issues=html) - return format_page(index) \ No newline at end of file +} \ No newline at end of file diff --git a/src/static/style.css b/src/static/style.css new file mode 100644 index 0000000..ce507f3 --- /dev/null +++ b/src/static/style.css @@ -0,0 +1,3 @@ +body { + background-color: aqua; +} diff --git a/src/templates/base.html b/src/templates/base.html new file mode 100644 index 0000000..2b46671 --- /dev/null +++ b/src/templates/base.html @@ -0,0 +1,11 @@ + + + +choose an issue, fill the placeholders, enter your uni-mail, then hit send
+ + +{% endblock %} \ No newline at end of file