PCv5/app/routes/forum/index.py

43 lines
1.2 KiB
Python

from flask_login import current_user
from flask import request, abort
from app.utils.render import render
from app.forms.forum import TopicCreationForm
from app.models.forum import Forum
from app.models.topic import Topic
from app.models.thread import Thread
from app.models.comment import Comment
from app import app, db
@app.route('/forum/')
def forum_index():
main_forum = Forum.query.filter_by(parent=None).first()
return render('/forum/index.html', main_forum=main_forum)
@app.route('/forum/<forum:f>/', methods=['GET', 'POST'])
def forum_page(f):
form = TopicCreationForm()
if form.validate_on_submit():
# TODO: Check user privileges for this specific forum!
# First create the thread, then the comment, then the topic
th = Thread()
db.session.add(th)
db.session.commit()
c = Comment(current_user, form.message.data, th)
db.session.add(c)
db.session.commit()
th.set_top_comment(c)
db.session.merge(th)
t = Topic(f, current_user, form.title.data, th)
db.session.add(t)
db.session.commit()
print(th, c, t)
return render('/forum/forum.html', f=f, form=form)