2019-09-08 14:34:51 +02:00
|
|
|
from flask_login import current_user
|
2019-12-03 20:36:26 +01:00
|
|
|
from flask import request, redirect, url_for, abort, flash
|
2019-09-08 14:34:51 +02:00
|
|
|
|
2019-12-04 12:22:42 +01:00
|
|
|
from app import app, db
|
|
|
|
from config import V5Config
|
2019-09-05 23:42:47 +02:00
|
|
|
from app.utils.render import render
|
2019-09-08 14:34:51 +02:00
|
|
|
from app.forms.forum import TopicCreationForm
|
2019-09-05 23:42:47 +02:00
|
|
|
from app.models.forum import Forum
|
2019-09-08 14:34:51 +02:00
|
|
|
from app.models.topic import Topic
|
|
|
|
from app.models.thread import Thread
|
|
|
|
from app.models.comment import Comment
|
2019-12-04 12:22:42 +01:00
|
|
|
|
2019-09-05 23:42:47 +02:00
|
|
|
|
2019-09-08 14:34:51 +02:00
|
|
|
@app.route('/forum/')
|
2019-09-06 10:00:39 +02:00
|
|
|
def forum_index():
|
2019-12-04 01:16:16 +01:00
|
|
|
return render('/forum/index.html')
|
2019-09-06 10:00:39 +02:00
|
|
|
|
2019-09-08 14:34:51 +02:00
|
|
|
@app.route('/forum/<forum:f>/', methods=['GET', 'POST'])
|
2019-09-06 10:00:39 +02:00
|
|
|
def forum_page(f):
|
2019-09-08 14:34:51 +02:00
|
|
|
form = TopicCreationForm()
|
|
|
|
|
2019-12-10 23:01:40 +01:00
|
|
|
# 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 ("/news" in f.url and current_user.is_authenticated
|
|
|
|
and current_user.priv('write-news'))
|
|
|
|
# Forum is not news and is a leaf:
|
|
|
|
or ("/news" not in f.name and not f.sub_forums)):
|
2019-09-08 14:34:51 +02:00
|
|
|
|
|
|
|
# First create the thread, then the comment, then the topic
|
|
|
|
th = Thread()
|
|
|
|
db.session.add(th)
|
|
|
|
db.session.commit()
|
|
|
|
|
2019-12-03 20:32:01 +01:00
|
|
|
c = Comment(current_user, form.message.data, th)
|
|
|
|
db.session.add(c)
|
|
|
|
db.session.commit()
|
|
|
|
|
2019-09-08 14:34:51 +02:00
|
|
|
th.set_top_comment(c)
|
2019-12-03 20:32:01 +01:00
|
|
|
db.session.merge(th)
|
2019-09-08 14:34:51 +02:00
|
|
|
|
2019-12-03 20:32:01 +01:00
|
|
|
t = Topic(f, current_user, form.title.data, th)
|
2019-09-08 14:34:51 +02:00
|
|
|
db.session.add(t)
|
|
|
|
db.session.commit()
|
|
|
|
|
2019-12-10 11:22:56 +01:00
|
|
|
# Update member's xp and trophies
|
2019-12-04 12:22:42 +01:00
|
|
|
current_user.add_xp(V5Config.XP_POINTS['topic'])
|
2019-12-10 11:22:56 +01:00
|
|
|
current_user.update_trophies('new-post')
|
2019-12-10 23:01:40 +01:00
|
|
|
|
2019-12-04 12:22:42 +01:00
|
|
|
|
2019-12-03 20:36:26 +01:00
|
|
|
flash('Le sujet a bien été créé', 'ok')
|
|
|
|
return redirect(url_for('forum_topic', f=f, t=t))
|
2019-09-08 14:34:51 +02:00
|
|
|
|
2019-12-03 20:32:01 +01:00
|
|
|
return render('/forum/forum.html', f=f, form=form)
|