2019-12-03 20:32:01 +01:00
|
|
|
from flask_login import current_user
|
2019-12-04 01:16:16 +01:00
|
|
|
from flask import request, redirect, url_for, flash, abort
|
2019-12-03 20:32:01 +01:00
|
|
|
|
2019-12-04 13:58:48 +01:00
|
|
|
from app import app, db
|
|
|
|
from config import V5Config
|
2019-12-03 20:32:01 +01:00
|
|
|
from app.utils.render import render
|
2020-07-17 23:49:04 +02:00
|
|
|
from app.forms.forum import CommentForm, AnonymousCommentForm
|
2019-12-03 20:32:01 +01:00
|
|
|
from app.models.forum import Forum
|
|
|
|
from app.models.topic import Topic
|
|
|
|
from app.models.thread import Thread
|
|
|
|
from app.models.comment import Comment
|
2020-07-17 23:49:04 +02:00
|
|
|
from app.models.users import Guest
|
2019-12-03 20:32:01 +01:00
|
|
|
|
|
|
|
|
2020-07-17 00:17:11 +02:00
|
|
|
@app.route('/forum/<forum:f>/<topicpage:page>', methods=['GET', 'POST'])
|
2020-07-16 23:58:21 +02:00
|
|
|
def forum_topic(f, page):
|
|
|
|
t, page = page
|
|
|
|
|
2019-12-04 01:16:16 +01:00
|
|
|
# Quick n' dirty workaround to converters
|
|
|
|
if f != t.forum:
|
|
|
|
abort(404)
|
|
|
|
|
2020-07-17 23:49:04 +02:00
|
|
|
if current_user.is_authenticated:
|
|
|
|
form = CommentForm()
|
|
|
|
else:
|
|
|
|
form = AnonymousCommentForm()
|
2019-12-03 20:32:01 +01:00
|
|
|
|
2020-07-17 23:49:04 +02:00
|
|
|
if form.validate_on_submit() and \
|
|
|
|
(V5Config.ENABLE_GUEST_POST or current_user.is_authenticated):
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
author = current_user
|
|
|
|
else:
|
|
|
|
author = Guest(form.pseudo.data)
|
|
|
|
db.session.add(author)
|
|
|
|
|
|
|
|
c = Comment(author, form.message.data, t.thread)
|
2019-12-03 20:32:01 +01:00
|
|
|
db.session.add(c)
|
|
|
|
db.session.commit()
|
2019-12-04 12:22:42 +01:00
|
|
|
|
2019-12-10 11:22:56 +01:00
|
|
|
# Update member's xp and trophies
|
2020-07-17 23:49:04 +02:00
|
|
|
if current_user.is_authenticated:
|
|
|
|
current_user.add_xp(V5Config.XP_POINTS['comment'])
|
|
|
|
current_user.update_trophies('new-post')
|
2019-12-04 12:22:42 +01:00
|
|
|
|
2019-12-03 20:32:01 +01:00
|
|
|
flash('Message envoyé', 'ok')
|
|
|
|
# Redirect to empty the form
|
2020-07-17 00:17:11 +02:00
|
|
|
return redirect(url_for('forum_topic', f=f, page=(t,"fin"),
|
|
|
|
_anchor=c.id))
|
2019-12-03 20:32:01 +01:00
|
|
|
|
|
|
|
# Update views
|
|
|
|
t.views += 1
|
|
|
|
db.session.merge(t)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-07-16 23:58:21 +02:00
|
|
|
if page == -1:
|
2019-12-10 19:03:08 +01:00
|
|
|
page = (t.thread.comments.count() - 1) \
|
|
|
|
// V5Config.COMMENTS_PER_PAGE + 1
|
2019-12-07 16:06:00 +01:00
|
|
|
|
|
|
|
comments = t.thread.comments.paginate(page,
|
|
|
|
V5Config.COMMENTS_PER_PAGE, True)
|
|
|
|
|
|
|
|
return render('/forum/topic.html', t=t, form=form, comments=comments)
|