From 1376525c9e71fa15d6b113a0c15ad68bc6b0be33 Mon Sep 17 00:00:00 2001 From: forest Date: Mon, 11 Apr 2022 14:42:47 -0500 Subject: [PATCH] admin broadcast message feature --- capsulflask/__init__.py | 5 +++++ capsulflask/admin.py | 7 +++++++ capsulflask/db.py | 15 +++++++++++---- capsulflask/db_model.py | 3 +++ .../24_down_broadcast_message.sql | 4 ++++ .../schema_migrations/24_up_broadcast_message.sql | 7 +++++++ capsulflask/templates/admin.html | 13 +++++++++++++ capsulflask/templates/base.html | 2 +- 8 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 capsulflask/schema_migrations/24_down_broadcast_message.sql create mode 100644 capsulflask/schema_migrations/24_up_broadcast_message.sql diff --git a/capsulflask/__init__.py b/capsulflask/__init__.py index 28ab7d6..d3121c5 100644 --- a/capsulflask/__init__.py +++ b/capsulflask/__init__.py @@ -14,6 +14,7 @@ from flask_mail import Mail, Message from flask import render_template from flask import url_for from flask import current_app +from flask import flash from apscheduler.schedulers.background import BackgroundScheduler @@ -207,6 +208,10 @@ def security_headers(response): if 'Content-Security-Policy' not in response.headers: response.headers['Content-Security-Policy'] = "default-src 'self'" response.headers['X-Content-Type-Options'] = 'nosniff' + + if current_app.config['BROADCAST_BANNER_MESSAGE'] is not None and current_app.config['BROADCAST_BANNER_MESSAGE'] != "": + flash(current_app.config['BROADCAST_BANNER_MESSAGE']) + return response diff --git a/capsulflask/admin.py b/capsulflask/admin.py index e08511c..bb575d3 100644 --- a/capsulflask/admin.py +++ b/capsulflask/admin.py @@ -60,6 +60,13 @@ def index(): current_app.logger.info(f"sending email is done.") return redirect(f"{url_for('admin.index')}") + elif request.form['action'] == "set_broadcast_message": + get_model().set_broadcast_message(request.form['message']) + current_app.config['BROADCAST_BANNER_MESSAGE'] = request.form['message'] + session.pop('_flashes', None) + + return redirect(f"{url_for('admin.index')}") + elif request.form['action'] == "start_or_stop": if 'id' not in request.form: return abort(400, "id is required") diff --git a/capsulflask/db.py b/capsulflask/db.py index 01fe76d..fd0e814 100644 --- a/capsulflask/db.py +++ b/capsulflask/db.py @@ -50,13 +50,13 @@ def init_app(app, is_running_server): hasSchemaVersionTable = False actionWasTaken = False schemaVersion = 0 - desiredSchemaVersion = 23 + desiredSchemaVersion = 24 cursor = connection.cursor() cursor.execute(""" - SELECT table_name, table_schema FROM information_schema.tables WHERE table_schema = '{}' - """.format(app.config['DATABASE_SCHEMA'])) + SELECT table_name, table_schema FROM information_schema.tables WHERE table_schema = %s + """, (app.config['DATABASE_SCHEMA'], )) rows = cursor.fetchall() for row in rows: @@ -110,8 +110,15 @@ def init_app(app, is_running_server): )) exit(1) - cursor.close() + cursor.execute("SELECT message FROM broadcast_message") + rows = cursor.fetchall() + if len(rows) > 0: + app.config['BROADCAST_BANNER_MESSAGE'] = rows[0][0] + else: + app.config['BROADCAST_BANNER_MESSAGE'] = None + cursor.close() + app.config['PSYCOPG2_CONNECTION_POOL'].putconn(connection) app.logger.info("{} current schemaVersion: \"{}\"".format( diff --git a/capsulflask/db_model.py b/capsulflask/db_model.py index 2d43cc5..012c3f7 100644 --- a/capsulflask/db_model.py +++ b/capsulflask/db_model.py @@ -509,3 +509,6 @@ class DBModel: + def set_broadcast_message(self, message): + self.cursor.execute("DELETE FROM broadcast_message; INSERT INTO broadcast_message (message) VALUES (%s)", (message, )) + self.connection.commit() diff --git a/capsulflask/schema_migrations/24_down_broadcast_message.sql b/capsulflask/schema_migrations/24_down_broadcast_message.sql new file mode 100644 index 0000000..9d309b0 --- /dev/null +++ b/capsulflask/schema_migrations/24_down_broadcast_message.sql @@ -0,0 +1,4 @@ + +DROP TABLE broadcast_message; + +UPDATE schemaversion SET version = 23; \ No newline at end of file diff --git a/capsulflask/schema_migrations/24_up_broadcast_message.sql b/capsulflask/schema_migrations/24_up_broadcast_message.sql new file mode 100644 index 0000000..cbf1342 --- /dev/null +++ b/capsulflask/schema_migrations/24_up_broadcast_message.sql @@ -0,0 +1,7 @@ + + +CREATE TABLE broadcast_message ( + message TEXT NOT NULL +); + +UPDATE schemaversion SET version = 24; \ No newline at end of file diff --git a/capsulflask/templates/admin.html b/capsulflask/templates/admin.html index f065402..811dd27 100644 --- a/capsulflask/templates/admin.html +++ b/capsulflask/templates/admin.html @@ -152,6 +152,19 @@
{% endif %} +
+
+

📰 Sitewide Banner: Post a temporary message across the site 📰

+
+
+
+ + + + +
+
+

📢 Admin Megaphone: Email All Users who have Active Capsuls 📢

diff --git a/capsulflask/templates/base.html b/capsulflask/templates/base.html index c7564bd..036b402 100644 --- a/capsulflask/templates/base.html +++ b/capsulflask/templates/base.html @@ -30,7 +30,7 @@ Changelog {% if session["account"] %} - Capsuls + Capsuls SSH Public Keys Account Balance {% endif %}