notyfikacje
This commit is contained in:
+76
-3
@@ -14,7 +14,7 @@ from flask_login import current_user, login_required, login_user, logout_user
|
||||
from auth import init_login_manager
|
||||
from config import load_secret_key, sensors_api_url, users_database_uri
|
||||
from sensors_client import SensorsAPIError, SensorsClient
|
||||
from user_models import Note, NoteItem, User, user_db
|
||||
from user_models import Note, NoteItem, Notification, User, user_db
|
||||
|
||||
|
||||
def create_app():
|
||||
@@ -53,8 +53,16 @@ def create_app():
|
||||
return jsonify(data), status
|
||||
|
||||
@app.context_processor
|
||||
def inject_service_urls():
|
||||
return {"main_api_url": request.url_root.rstrip("/") + "/api"}
|
||||
def inject_globals():
|
||||
unread_notifications = 0
|
||||
if current_user.is_authenticated:
|
||||
unread_notifications = Notification.query.filter_by(
|
||||
user_id=current_user.id, is_read=False
|
||||
).count()
|
||||
return {
|
||||
"main_api_url": request.url_root.rstrip("/") + "/api",
|
||||
"unread_notifications": unread_notifications,
|
||||
}
|
||||
|
||||
# --- Pages ---
|
||||
|
||||
@@ -103,6 +111,40 @@ def create_app():
|
||||
def api_docs():
|
||||
return render_template("docs.html", user=current_user)
|
||||
|
||||
# --- Notifications (stored in main_app.db) ---
|
||||
|
||||
@app.route("/notifications")
|
||||
@login_required
|
||||
def notifications_page():
|
||||
notifications = (
|
||||
Notification.query.filter_by(user_id=current_user.id)
|
||||
.order_by(
|
||||
Notification.is_read.asc(),
|
||||
Notification.created_at.desc(),
|
||||
Notification.id.desc(),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
return render_template(
|
||||
"notifications.html", user=current_user, notifications=notifications
|
||||
)
|
||||
|
||||
@app.route("/notifications/<int:notification_id>/read")
|
||||
@login_required
|
||||
def notification_read(notification_id):
|
||||
notification = Notification.query.filter_by(
|
||||
id=notification_id, user_id=current_user.id
|
||||
).first_or_404()
|
||||
if not notification.is_read:
|
||||
notification.mark_read()
|
||||
user_db.session.commit()
|
||||
return redirect(
|
||||
url_for(
|
||||
"notifications_page",
|
||||
_anchor=f"notification-{notification.id}",
|
||||
)
|
||||
)
|
||||
|
||||
# --- Notes (stored in main_app.db) ---
|
||||
|
||||
def _note_or_404(note_id):
|
||||
@@ -287,6 +329,37 @@ def create_app():
|
||||
user_db.session.commit()
|
||||
click.echo(f"Created administrator '{username}'.")
|
||||
|
||||
@app.cli.command("create-notification")
|
||||
@click.option("--username", default=None, help="Recipient username")
|
||||
@click.option("--all", "all_users", is_flag=True, help="Send to all users")
|
||||
@click.option("--message", default=None, help="Notification text")
|
||||
def create_notification(username, all_users, message):
|
||||
"""Create a notification for one user or for all users."""
|
||||
if all_users and username:
|
||||
raise click.ClickException("Use either --username or --all, not both.")
|
||||
if not all_users and not username:
|
||||
raise click.ClickException("Provide --username or --all.")
|
||||
|
||||
message = (message or "").strip()
|
||||
if not message:
|
||||
message = click.prompt("Message").strip()
|
||||
if not message:
|
||||
raise click.ClickException("Message cannot be empty.")
|
||||
message = message[:500]
|
||||
|
||||
if all_users:
|
||||
users = User.query.all()
|
||||
else:
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if not user:
|
||||
raise click.ClickException(f"User '{username}' not found.")
|
||||
users = [user]
|
||||
|
||||
for user in users:
|
||||
user_db.session.add(Notification(user_id=user.id, message=message))
|
||||
user_db.session.commit()
|
||||
click.echo(f"Created {len(users)} notification(s).")
|
||||
|
||||
return app
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user