Check if user is banned, log them out and then flash them with a funny message which is handled by error modals

This commit is contained in:
csd4ni3l
2025-08-14 13:49:33 +02:00
parent 3db4310a1d
commit 1c9bb38848
2 changed files with 48 additions and 2 deletions

16
main.py
View File

@@ -1,4 +1,4 @@
from flask import Flask, redirect, url_for, render_template, request, Response, send_from_directory, g
from flask import Flask, redirect, url_for, render_template, request, Response, send_from_directory, g, flash
from dotenv import load_dotenv
from constants import RICKROLL_LINK, UPLOAD_DIR, MINIMUM_COSINE_SIMILARITY, MINIMUM_OCR_SIMILARITY, DATABASE_FILE
from PIL import Image
@@ -82,6 +82,20 @@ def user_loader(user_id):
def unauthorized_handler():
return redirect(url_for("login"))
@app.before_request
def check_banned():
username = flask_login.current_user.id
cur = get_db().cursor()
cur.execute("SELECT banned FROM Users WHERE username = ?", (username))
row = cur.fetchone()
cur.close()
if row is None or row[0]:
flash("Imagine forgetting to touch grass so you get banned from my app. Such a discord moderator you are. You have no life. Just go outside.")
flask_login.logout_user()
return redirect("/")
def resize_image_file(path, max_side=256, fmt="JPEG"):
img = Image.open(path)
scale = max_side / max(img.size)

View File

@@ -18,5 +18,37 @@
For more information, please visit <a href="/info" class="alert-link">the unofficial safety guide</a>!
</div>
{% block body %}{% endblock %}
<div class="modal fade" id="errorModal" tabindex="-1" aria-labelledby="errorModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header bg-danger text-white">
<h5 class="modal-title" id="errorModalLabel">Error</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body" id="errorModalBody">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
{% if category == 'error' %}
const modalBody = document.getElementById("errorModalBody");
modalBody.textContent = "{{ message|escapejs }}";
const errorModal = new bootstrap.Modal(document.getElementById('errorModal'));
errorModal.show();
{% endif %}
{% endfor %}
{% endif %}
{% endwith %}
});
</script>
</body>
</html>