From 7005888d349891c6ed1b32bd0e719173bf451f0a Mon Sep 17 00:00:00 2001 From: nathaniel smith Date: Sun, 2 Aug 2015 19:02:39 -0700 Subject: [PATCH] helpdesk --- tildetown-py/{cgi.py => app.py} | 52 ++++++++++++++++++-- tildetown-py/templates/guestbook.html | 2 +- tildetown-py/templates/helpdesk.html | 68 +++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 5 deletions(-) rename tildetown-py/{cgi.py => app.py} (62%) create mode 100644 tildetown-py/templates/helpdesk.html diff --git a/tildetown-py/cgi.py b/tildetown-py/app.py similarity index 62% rename from tildetown-py/cgi.py rename to tildetown-py/app.py index 907f1a2..a4f8f2f 100644 --- a/tildetown-py/cgi.py +++ b/tildetown-py/app.py @@ -5,10 +5,14 @@ import random import time from sys import argv from tempfile import mkdtemp, mkstemp + +from pyhocon import ConfigFactory +import requests from flask import Flask, render_template, request, redirect + from stats import get_data -## disgusting hack for python 3.4 +## disgusting hack for python 3.4.0 import pkgutil orig_get_loader = pkgutil.get_loader def get_loader(name): @@ -17,12 +21,17 @@ def get_loader(name): except AttributeError: pass pkgutil.get_loader = get_loader -########### +########## SITE_NAME = 'tilde.town' app = Flask('~cgi') +@lru_cache(maxsize=32) +def cfg(k): + conf = ConfigFactory.parse_file('cfg.conf') + return conf[k] + @lru_cache(maxsize=32) def site_context(): return get_data() @@ -73,8 +82,43 @@ def post_guestbook(): return redirect("/guestbook") @app.route('/helpdesk', methods=['GET']) -def helpdesk(): - return "HELPDESK UNDER CONSTRUCTION" +def get_helpdesk(): + status = request.args.get('status', 'unsubmitted') + desc = request.args.get('desc', '') + context = { + 'status': status, + 'desc': desc, + } + context.update(site_context()) + return render_template('helpdesk.html', **context) + +def send_email(data): + name = data.get('name', 'anonymous') + email = data['email'] + request_type = data['type'] + desc = request.form['desc'] + response = requests.post( + cfg('mailgun_url'), + auth=("api", cfg('mailgun_key')), + data={"from": "root@tilde.town", + "to": cfg('trello'), + "subject": "{} from {} <{}>".format(request_type, name, email), + "text": desc}) + + return response.status_code == 200 + +@app.route('/helpdesk', methods=['POST']) +def post_helpdesk(): + desc = request.form['desc'] + captcha = request.form['hmm'] + + if captcha == 'scrop': + status = "success" if send_email(request.form) else "fail" + else: + status = "fail" + + # should we bother restoring other fields beside desc? + return redirect('/helpdesk?status={}&desc={}'.format(status, desc)) if __name__ == '__main__': if len(argv) == 2: diff --git a/tildetown-py/templates/guestbook.html b/tildetown-py/templates/guestbook.html index 890b002..e1f9f92 100644 --- a/tildetown-py/templates/guestbook.html +++ b/tildetown-py/templates/guestbook.html @@ -2,7 +2,7 @@ {{site_name}} guestbook - +

{{site_name}} guestbook

diff --git a/tildetown-py/templates/helpdesk.html b/tildetown-py/templates/helpdesk.html new file mode 100644 index 0000000..8740960 --- /dev/null +++ b/tildetown-py/templates/helpdesk.html @@ -0,0 +1,68 @@ + + + + {{ site_name }} help desk + + + +

{{ site_name }} help desk

+

+ hi! this form exists to make it a little easier for {{ site_name }} admins + to help people out with system issues like logging in, installing + packages, and general questions about how to do stuff. +

+ +

+ in using this system, recall that there are only a handful of admins and + you'll get a response (by email) as they are able to help out. +

+ +

+ thanks! +

+ + {% if status == 'fail' %} +

+ oops, sorry! that didn't work. either something is wrong on the server or + there was a problem with the form..try again? +

+ {% endif %} + + {% if status == 'success' %} +

+ hooray, your request has been sent. +

+ {% endif %} + +
+

+ name: +

+

+ email: +

+

+ type of issue: + +

+

+ describe your issue (in 500 characters or less):
+ +

+

+ are you a robot?
+ yeah
+ nope
+

+

+ +

+
+ +