PCv5/app/routes/posts/redirect.py

30 lines
918 B
Python
Raw Normal View History

2023-09-05 22:23:06 +02:00
from app import app
2023-09-05 20:25:50 +02:00
from app.models.comment import Comment
from app.models.thread import Thread
from app.models.program import Program
2023-09-05 22:23:06 +02:00
from flask import redirect, url_for
2023-09-05 20:25:50 +02:00
@app.route('/post/<int:postid>', methods=['GET', 'POST'])
def redirect_post(postid):
2023-09-05 21:09:54 +02:00
c = Comment.query.get_or_404(postid)
2023-09-05 20:25:50 +02:00
owner = c.thread.owner_post
# Get the comments for the thread
comments = Comment.query.where(
Comment.thread_id == c.thread.id,
Comment.date_created <= c.date_created
).order_by(
Comment.date_created.asc()
).paginate(per_page=Thread.COMMENTS_PER_PAGE, error_out=False)
2023-09-05 21:09:54 +02:00
if owner.type == 'topic':
2023-09-05 20:25:50 +02:00
# Is a topic
2023-09-05 21:09:54 +02:00
url = url_for('forum_topic', f=owner.forum, page=(owner, comments.pages), _anchor=str(c.id))
2023-09-05 20:25:50 +02:00
else:
# Is a program
url = url_for('program_view', page=(owner, comments.pages), _anchor=str(c.id))
2023-09-05 20:25:50 +02:00
return redirect(url, 301)