68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
from flask_login import current_user
|
|
from flask import request, redirect, url_for, abort, flash
|
|
|
|
from app import app, db
|
|
from config import V5Config
|
|
from app.utils.render import render
|
|
from app.forms.forum import TopicCreationForm, AnonymousTopicCreationForm
|
|
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.models.users import Guest
|
|
|
|
|
|
@app.route('/forum/')
|
|
def forum_index():
|
|
return render('/forum/index.html')
|
|
|
|
@app.route('/forum/<forum:f>/', methods=['GET', 'POST'])
|
|
def forum_page(f):
|
|
if current_user.is_authenticated:
|
|
form = TopicCreationForm()
|
|
else:
|
|
form = AnonymousTopicCreationForm()
|
|
|
|
# TODO: do not hardcode name of news forums
|
|
if form.validate_on_submit() and (
|
|
# User can write anywhere
|
|
(current_user.is_authenticated and current_user.priv('write-anywhere'))
|
|
# Forum is news forum TODO: add good condition to check if it's news
|
|
or ("/actus" in f.url and current_user.is_authenticated
|
|
and current_user.priv('write-news'))
|
|
# Forum is not news and is a leaf:
|
|
or ("/actus" not in f.url and not f.sub_forums)) and (
|
|
V5Config.ENABLE_GUEST_POST or current_user.is_authenticated):
|
|
|
|
# First create the thread, then the comment, then the topic
|
|
th = Thread()
|
|
db.session.add(th)
|
|
db.session.commit()
|
|
|
|
if current_user.is_authenticated:
|
|
author = current_user
|
|
else:
|
|
author = Guest(form.pseudo.data)
|
|
db.session.add(author)
|
|
|
|
c = Comment(author, form.message.data, th)
|
|
db.session.add(c)
|
|
db.session.commit()
|
|
|
|
th.set_top_comment(c)
|
|
db.session.merge(th)
|
|
|
|
t = Topic(f, author, form.title.data, th)
|
|
db.session.add(t)
|
|
db.session.commit()
|
|
|
|
# Update member's xp and trophies
|
|
if current_user.is_authenticated:
|
|
current_user.add_xp(2) # 2 points for a topic
|
|
current_user.update_trophies('new-post')
|
|
|
|
flash('Le sujet a bien été créé', 'ok')
|
|
return redirect(url_for('forum_topic', f=f, page=(t,1)))
|
|
|
|
return render('/forum/forum.html', f=f, form=form)
|