From 3e8acd28ac69de16cdcad6e0734029821daaa218 Mon Sep 17 00:00:00 2001 From: Lynne Date: Wed, 18 Sep 2019 21:18:01 +1000 Subject: [PATCH] moved bot_edit to its own file --- app/pages/bot/edit.py | 58 +++++++++++++++++++++++++++++++++++++++++++ app/webui.py | 58 +++---------------------------------------- 2 files changed, 61 insertions(+), 55 deletions(-) create mode 100644 app/pages/bot/edit.py diff --git a/app/pages/bot/edit.py b/app/pages/bot/edit.py new file mode 100644 index 0000000..bbbd499 --- /dev/null +++ b/app/pages/bot/edit.py @@ -0,0 +1,58 @@ +from flask import session, request, redirect, render_template +import MySQLdb + +def bot_edit(id, mysql): + if request.method == "GET": + dc = mysql.connection.cursor(MySQLdb.cursors.DictCursor) + dc.execute("SELECT * FROM bots WHERE handle = %s", (id,)) + return render_template("bot/edit.html", bot = dc.fetchone(), error = session.pop('error', None), success = session.pop('success', None)) + else: + # update stored settings + replies_enabled = 'replies' in request.form + learn_from_cw = 'cw-learning' in request.form + + if request.form['fake-mention-style'] not in ['full', 'brief']: + session['error'] = "Invalid setting for fake mention style." + return redirect("/bot/edit/{}".format(id), 303) + + if request.form['fake-mentions'] not in ['always', 'middle', 'never']: + session['error'] = "Invalid setting for fake mentions." + return redirect("/bot/edit/{}".format(id), 303) + + if request.form['privacy'] not in ['public', 'unlisted', 'private']: + session['error'] = "Invalid setting for post privacy." + return redirect("/bot/edit/{}".format(id), 303) + + if int(request.form['length']) < 100 or int(request.form['length']) > 5000: + session['error'] = "Invalid setting for maximum post length." + return redirect("/bot/edit/{}".format(id), 303) + + if int(request.form['freq']) < 15 or int(request.form['freq']) > 240 or int(request.form['freq']) % 5: + session['error'] = "Invalid setting for post frequency." + return redirect("/bot/edit/{}".format(id), 303) + + if len(request.form['cw']) > 128: + session['error'] = "Content warning cannot exceed 128 characters." + return redirect("/bot/edit/{}".format(id), 303) + + c = mysql.connection.cursor() + try: + c.execute("UPDATE bots SET replies_enabled = %s, post_frequency = %s, content_warning = %s, length = %s, fake_mentions = %s, fake_mentions_full = %s, post_privacy = %s, learn_from_cw = %s WHERE handle = %s", ( + replies_enabled, + request.form['freq'], + request.form['cw'] if request.form['cw'] != "" else None, + request.form['length'], + request.form['fake-mentions'], + request.form['fake-mention-style'] == 'full', + request.form['privacy'], + learn_from_cw, + id + )) + mysql.connection.commit() + c.close() + except: + session['error'] = "Couldn't save your settings." + return redirect("/bot/edit/{}".format(id), 303) + + session['success'] = True + return redirect("/bot/edit/{}".format(id), 303) diff --git a/app/webui.py b/app/webui.py index 4b45df6..8c57153 100644 --- a/app/webui.py +++ b/app/webui.py @@ -11,6 +11,7 @@ import json, hashlib, re import functions from pages.home import home from pages.settings import settings +from pages.bot.edit import bot_edit cfg = json.load(open("config.json")) @@ -59,61 +60,8 @@ def render_settings(): return settings(mysql) @app.route("/bot/edit/", methods = ['GET', 'POST']) -def bot_edit(id): - if request.method == "GET": - dc = mysql.connection.cursor(MySQLdb.cursors.DictCursor) - dc.execute("SELECT * FROM bots WHERE handle = %s", (id,)) - return render_template("bot/edit.html", bot = dc.fetchone(), error = session.pop('error', None), success = session.pop('success', None)) - else: - # update stored settings - replies_enabled = 'replies' in request.form - learn_from_cw = 'cw-learning' in request.form - - if request.form['fake-mention-style'] not in ['full', 'brief']: - session['error'] = "Invalid setting for fake mention style." - return redirect("/bot/edit/{}".format(id), 303) - - if request.form['fake-mentions'] not in ['always', 'middle', 'never']: - session['error'] = "Invalid setting for fake mentions." - return redirect("/bot/edit/{}".format(id), 303) - - if request.form['privacy'] not in ['public', 'unlisted', 'private']: - session['error'] = "Invalid setting for post privacy." - return redirect("/bot/edit/{}".format(id), 303) - - if int(request.form['length']) < 100 or int(request.form['length']) > 5000: - session['error'] = "Invalid setting for maximum post length." - return redirect("/bot/edit/{}".format(id), 303) - - if int(request.form['freq']) < 15 or int(request.form['freq']) > 240 or int(request.form['freq']) % 5: - session['error'] = "Invalid setting for post frequency." - return redirect("/bot/edit/{}".format(id), 303) - - if len(request.form['cw']) > 128: - session['error'] = "Content warning cannot exceed 128 characters." - return redirect("/bot/edit/{}".format(id), 303) - - c = mysql.connection.cursor() - try: - c.execute("UPDATE bots SET replies_enabled = %s, post_frequency = %s, content_warning = %s, length = %s, fake_mentions = %s, fake_mentions_full = %s, post_privacy = %s, learn_from_cw = %s WHERE handle = %s", ( - replies_enabled, - request.form['freq'], - request.form['cw'] if request.form['cw'] != "" else None, - request.form['length'], - request.form['fake-mentions'], - request.form['fake-mention-style'] == 'full', - request.form['privacy'], - learn_from_cw, - id - )) - mysql.connection.commit() - c.close() - except: - session['error'] = "Couldn't save your settings." - return redirect("/bot/edit/{}".format(id), 303) - - session['success'] = True - return redirect("/bot/edit/{}".format(id), 303) +def render_bot_edit(id): + return bot_edit(id, mysql) @app.route("/bot/delete/", methods=['GET', 'POST']) def bot_delete(id):