Browse Source

admin broadcast message feature

main
forest 2 months ago
parent
commit
1376525c9e
  1. 5
      capsulflask/__init__.py
  2. 7
      capsulflask/admin.py
  3. 15
      capsulflask/db.py
  4. 3
      capsulflask/db_model.py
  5. 4
      capsulflask/schema_migrations/24_down_broadcast_message.sql
  6. 7
      capsulflask/schema_migrations/24_up_broadcast_message.sql
  7. 13
      capsulflask/templates/admin.html
  8. 2
      capsulflask/templates/base.html

5
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

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

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

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

4
capsulflask/schema_migrations/24_down_broadcast_message.sql

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

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

13
capsulflask/templates/admin.html

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

2
capsulflask/templates/base.html

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

Loading…
Cancel
Save