From 213ef0b1a7a5bb48f80f292556de032c26a1b45f Mon Sep 17 00:00:00 2001 From: R2 Date: Thu, 15 Feb 2024 15:05:14 +0700 Subject: [PATCH] Add routes: /messages /notifications --- app/main/routes.py | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/app/main/routes.py b/app/main/routes.py index 0567273..d24a38a 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -16,8 +16,8 @@ from app import db from app.main import bp -from app.main.forms import EditProfileForm, EmptyForm, PostForm, SearchForm -from app.models import User, Post +from app.main.forms import EditProfileForm, EmptyForm, PostForm, SearchForm, MessageForm +from app.models import User, Post, Message, Notification @bp.before_request @@ -281,3 +281,37 @@ def search(): # Отображаем страницу поиска с результатами return render_template('search.html', title=_('Search'), posts=posts, next_url=next_url, prev_url=prev_url) + + +@bp.route('/messages') +@login_required +def messages(): + current_user.last_message_read_time = datetime.now(timezone.utc) + current_user.add_notification('unread_message_count', 0) + db.session.commit() + page = request.args.get('page', 1, type=int) + query = current_user.messages_received.select().order_by( + Message.timestamp.desc()) + messages = db.paginate(query, page=page, + per_page=current_app.config['POSTS_PER_PAGE'], + error_out=False) + next_url = url_for('main.messages', page=messages.next_num) \ + if messages.has_next else None + prev_url = url_for('main.messages', page=messages.prev_num) \ + if messages.has_prev else None + return render_template('messages.html', messages=messages.items, + next_url=next_url, prev_url=prev_url) + + +@bp.route('/notifications') +@login_required +def notifications(): + since = request.args.get('since', 0.0, type=float) + query = current_user.notifications.select().where( + Notification.timestamp > since).order_by(Notification.timestamp.asc()) + notifications = db.session.scalars(query) + return [{ + 'name': n.name, + 'data': n.get_data(), + 'timestamp': n.timestamp + } for n in notifications]