Add notif page
This commit is contained in:
Binary file not shown.
141
routes/main.py
141
routes/main.py
@@ -1,6 +1,6 @@
|
||||
from flask import render_template, Blueprint, redirect, url_for, request, flash, Response, jsonify, session
|
||||
from flask_login import current_user, login_required
|
||||
from models import User, db, Room, RoomFile, RoomMemberPermission, SiteSettings, Event, Conversation, Message, MessageAttachment
|
||||
from models import User, db, Room, RoomFile, RoomMemberPermission, SiteSettings, Event, Conversation, Message, MessageAttachment, Notif
|
||||
from routes.auth import require_password_change
|
||||
import os
|
||||
from werkzeug.utils import secure_filename
|
||||
@@ -441,7 +441,144 @@ def init_routes(main_bp):
|
||||
@login_required
|
||||
@require_password_change
|
||||
def notifications():
|
||||
return render_template('notifications/notifications.html')
|
||||
# Get filter parameters
|
||||
notif_type = request.args.get('notif_type', '')
|
||||
date_range = request.args.get('date_range', '7d')
|
||||
page = request.args.get('page', 1, type=int)
|
||||
per_page = 10
|
||||
|
||||
# Calculate date range
|
||||
end_date = datetime.utcnow()
|
||||
if date_range == '24h':
|
||||
start_date = end_date - timedelta(days=1)
|
||||
elif date_range == '7d':
|
||||
start_date = end_date - timedelta(days=7)
|
||||
elif date_range == '30d':
|
||||
start_date = end_date - timedelta(days=30)
|
||||
else:
|
||||
start_date = None
|
||||
|
||||
# Build query
|
||||
query = Notif.query.filter_by(user_id=current_user.id)
|
||||
|
||||
if notif_type:
|
||||
query = query.filter_by(notif_type=notif_type)
|
||||
if start_date:
|
||||
query = query.filter(Notif.timestamp >= start_date)
|
||||
|
||||
# Get total count for pagination
|
||||
total_notifs = query.count()
|
||||
total_pages = (total_notifs + per_page - 1) // per_page
|
||||
|
||||
# Get paginated notifications
|
||||
notifications = query.order_by(Notif.timestamp.desc()).paginate(page=page, per_page=per_page)
|
||||
|
||||
return render_template('notifications/notifications.html',
|
||||
notifications=notifications.items,
|
||||
total_pages=total_pages,
|
||||
current_page=page)
|
||||
|
||||
@main_bp.route('/api/notifications')
|
||||
@login_required
|
||||
def get_notifications():
|
||||
# Get filter parameters
|
||||
notif_type = request.args.get('notif_type', '')
|
||||
date_range = request.args.get('date_range', '7d')
|
||||
page = request.args.get('page', 1, type=int)
|
||||
per_page = 10
|
||||
|
||||
# Calculate date range
|
||||
end_date = datetime.utcnow()
|
||||
if date_range == '24h':
|
||||
start_date = end_date - timedelta(days=1)
|
||||
elif date_range == '7d':
|
||||
start_date = end_date - timedelta(days=7)
|
||||
elif date_range == '30d':
|
||||
start_date = end_date - timedelta(days=30)
|
||||
else:
|
||||
start_date = None
|
||||
|
||||
# Build query
|
||||
query = Notif.query.filter_by(user_id=current_user.id)
|
||||
|
||||
if notif_type:
|
||||
query = query.filter_by(notif_type=notif_type)
|
||||
if start_date:
|
||||
query = query.filter(Notif.timestamp >= start_date)
|
||||
|
||||
# Get total count for pagination
|
||||
total_notifs = query.count()
|
||||
total_pages = (total_notifs + per_page - 1) // per_page
|
||||
|
||||
# Get paginated notifications
|
||||
notifications = query.order_by(Notif.timestamp.desc()).paginate(page=page, per_page=per_page)
|
||||
|
||||
return jsonify({
|
||||
'notifications': [{
|
||||
'id': notif.id,
|
||||
'notif_type': notif.notif_type,
|
||||
'timestamp': notif.timestamp.isoformat(),
|
||||
'read': notif.read,
|
||||
'details': notif.details,
|
||||
'sender': {
|
||||
'id': notif.sender.id,
|
||||
'username': notif.sender.username
|
||||
} if notif.sender else None
|
||||
} for notif in notifications.items],
|
||||
'total_pages': total_pages,
|
||||
'current_page': page
|
||||
})
|
||||
|
||||
@main_bp.route('/api/notifications/<int:notif_id>')
|
||||
@login_required
|
||||
def get_notification_details(notif_id):
|
||||
notif = Notif.query.get_or_404(notif_id)
|
||||
if notif.user_id != current_user.id:
|
||||
return jsonify({'error': 'Unauthorized'}), 403
|
||||
|
||||
return jsonify({
|
||||
'id': notif.id,
|
||||
'notif_type': notif.notif_type,
|
||||
'timestamp': notif.timestamp.isoformat(),
|
||||
'read': notif.read,
|
||||
'details': notif.details,
|
||||
'sender': {
|
||||
'id': notif.sender.id,
|
||||
'username': notif.sender.username
|
||||
} if notif.sender else None
|
||||
})
|
||||
|
||||
@main_bp.route('/api/notifications/<int:notif_id>/read', methods=['POST'])
|
||||
@login_required
|
||||
def mark_notification_read(notif_id):
|
||||
notif = Notif.query.get_or_404(notif_id)
|
||||
if notif.user_id != current_user.id:
|
||||
return jsonify({'error': 'Unauthorized'}), 403
|
||||
|
||||
notif.read = True
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({'success': True})
|
||||
|
||||
@main_bp.route('/api/notifications/mark-all-read', methods=['POST'])
|
||||
@login_required
|
||||
def mark_all_notifications_read():
|
||||
result = Notif.query.filter_by(user_id=current_user.id, read=False).update({'read': True})
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({'success': True, 'count': result})
|
||||
|
||||
@main_bp.route('/api/notifications/<int:notif_id>', methods=['DELETE'])
|
||||
@login_required
|
||||
def delete_notification(notif_id):
|
||||
notif = Notif.query.get_or_404(notif_id)
|
||||
if notif.user_id != current_user.id:
|
||||
return jsonify({'error': 'Unauthorized'}), 403
|
||||
|
||||
db.session.delete(notif)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({'success': True})
|
||||
|
||||
@main_bp.route('/settings')
|
||||
@login_required
|
||||
|
||||
Reference in New Issue
Block a user