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
|
2020-07-18 07:52:29 +02:00
|
|
|
from app.forms.forum import TopicCreationForm, AnonymousTopicCreationForm
|
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
|
2020-08-25 22:57:45 +02:00
|
|
|
from app.models.user import Guest
|
2020-08-01 21:26:06 +02:00
|
|
|
from app.models.attachment import Attachment
|
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'])
|
2020-08-02 17:28:43 +02:00
|
|
|
@app.route('/forum/<forum:f>/p/<int:page>', methods=['GET', 'POST'])
|
|
|
|
def forum_page(f, page=1):
|
2020-07-18 07:52:29 +02:00
|
|
|
if current_user.is_authenticated:
|
|
|
|
form = TopicCreationForm()
|
|
|
|
else:
|
|
|
|
form = AnonymousTopicCreationForm()
|
2019-09-08 14:34:51 +02:00
|
|
|
|
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
|
2019-12-16 23:57:50 +01:00
|
|
|
or ("/actus" in f.url and current_user.is_authenticated
|
2019-12-10 23:01:40 +01:00
|
|
|
and current_user.priv('write-news'))
|
|
|
|
# Forum is not news and is a leaf:
|
2020-07-18 07:52:29 +02:00
|
|
|
or ("/actus" not in f.url and not f.sub_forums)) and (
|
|
|
|
V5Config.ENABLE_GUEST_POST or current_user.is_authenticated):
|
|
|
|
|
2020-08-01 21:26:06 +02:00
|
|
|
# Manage author
|
2020-07-18 07:52:29 +02:00
|
|
|
if current_user.is_authenticated:
|
|
|
|
author = current_user
|
|
|
|
else:
|
|
|
|
author = Guest(form.pseudo.data)
|
|
|
|
db.session.add(author)
|
|
|
|
|
2020-08-01 21:26:06 +02:00
|
|
|
# First create the thread, then the comment, then the topic
|
|
|
|
th = Thread()
|
|
|
|
db.session.add(th)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-07-18 07:52:29 +02:00
|
|
|
c = Comment(author, form.message.data, th)
|
2019-12-03 20:32:01 +01:00
|
|
|
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
|
|
|
|
2020-07-18 07:52:29 +02:00
|
|
|
t = Topic(f, author, form.title.data, th)
|
2019-09-08 14:34:51 +02:00
|
|
|
db.session.add(t)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-08-01 21:26:06 +02:00
|
|
|
# Manage files
|
|
|
|
attachments = []
|
|
|
|
for file in form.attachments.data:
|
|
|
|
if file.filename != "":
|
|
|
|
a = Attachment(file, c)
|
|
|
|
attachments.append((a, file))
|
|
|
|
db.session.add(a)
|
|
|
|
db.session.commit()
|
|
|
|
for a, file in attachments:
|
|
|
|
a.set_file(file)
|
|
|
|
|
2019-12-10 11:22:56 +01:00
|
|
|
# Update member's xp and trophies
|
2020-07-18 07:52:29 +02:00
|
|
|
if current_user.is_authenticated:
|
2020-08-05 22:52:56 +02:00
|
|
|
current_user.add_xp(2) # 2 points for a topic
|
2020-07-18 07:52:29 +02:00
|
|
|
current_user.update_trophies('new-post')
|
2019-12-04 12:22:42 +01:00
|
|
|
|
2019-12-03 20:36:26 +01:00
|
|
|
flash('Le sujet a bien été créé', 'ok')
|
2020-07-16 23:58:21 +02:00
|
|
|
return redirect(url_for('forum_topic', f=f, page=(t,1)))
|
2019-09-08 14:34:51 +02:00
|
|
|
|
2020-08-02 17:28:43 +02:00
|
|
|
# Paginate topic pages
|
|
|
|
# TODO: order by last comment date
|
|
|
|
topics = f.topics.order_by(Topic.date_created.desc()).paginate(
|
|
|
|
page, Forum.TOPICS_PER_PAGE, True)
|
|
|
|
|
|
|
|
return render('/forum/forum.html', f=f, topics=topics, form=form)
|