admin broadcast message feature

This commit is contained in:
forest 2022-04-11 14:42:47 -05:00
parent 83b8d67a86
commit 1376525c9e
8 changed files with 51 additions and 5 deletions

View File

@ -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

View File

@ -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")

View File

@ -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(

View File

@ -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()

View File

@ -0,0 +1,4 @@
DROP TABLE broadcast_message;
UPDATE schemaversion SET version = 23;

View File

@ -0,0 +1,7 @@
CREATE TABLE broadcast_message (
message TEXT NOT NULL
);
UPDATE schemaversion SET version = 24;

View File

@ -152,6 +152,19 @@
<hr/>
{% endif %}
<div class="third-margin">
<div class="row">
<h1>📰 Sitewide Banner: Post a temporary message across the site 📰</h1>
</div>
<div class="row">
<form method="post" class="megaphone">
<input type="hidden" name="action" value="set_broadcast_message"></input>
<input type="hidden" name="csrf-token" value="{{ csrf_token }}"/>
<input type="text" name="message" placeholder="blahblahblah..." />
<input type="submit" value="SET"/>
</form>
</div>
<div class="third-margin">
<div class="row">
<h1>📢 Admin Megaphone: Email All Users who have Active Capsuls 📢</h1>

View File

@ -30,7 +30,7 @@
<a href="/changelog">Changelog</a>
{% if session["account"] %}
<a href="/console">Capsuls</a>
<a href="/console/">Capsuls</a>
<a href="/console/ssh">SSH Public Keys</a>
<a href="/console/account-balance">Account Balance</a>
{% endif %}