Skip to content

Commit

Permalink
Merge pull request #43 from TheRomanVolkov/messages
Browse files Browse the repository at this point in the history
Add routes:
  • Loading branch information
JustRomanVolkov committed Feb 15, 2024
2 parents 4258bb1 + 213ef0b commit 0a65a94
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions app/main/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]

0 comments on commit 0a65a94

Please sign in to comment.