PCv5/app/routes/posts/redirect.py

30 lines
918 B
Python

from app import app
from app.models.comment import Comment
from app.models.thread import Thread
from app.models.program import Program
from flask import redirect, url_for
@app.route('/post/<int:postid>', methods=['GET', 'POST'])
def redirect_post(postid):
c = Comment.query.get_or_404(postid)
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)
if owner.type == 'topic':
# Is a topic
url = url_for('forum_topic', f=owner.forum, page=(owner, comments.pages), _anchor=str(c.id))
else:
# Is a program
url = url_for('program_view', page=(owner, comments.pages), _anchor=str(c.id))
return redirect(url, 301)