PCv5/app/routes/programs/program.py

59 lines
2.0 KiB
Python
Raw Normal View History

from app import app, db
2022-05-26 21:08:01 +02:00
from app.models.user import Guest
from app.models.program import Program
from app.models.comment import Comment
from app.models.thread import Thread
from app.utils.render import render
2023-06-12 20:04:20 +02:00
from app.utils.glados import say, BOLD
from app.forms.forum import CommentForm, AnonymousCommentForm
from config import V5Config
from flask_login import current_user
from flask import redirect, url_for, flash
@app.route('/programmes/<programpage:page>', methods=['GET','POST'])
def program_view(page):
p, page = page
if current_user.is_authenticated:
form = CommentForm()
else:
form = AnonymousCommentForm()
if form.validate_on_submit() and not p.thread.locked and (
V5Config.ENABLE_GUEST_POST or current_user.is_authenticated):
# Manage author
if current_user.is_authenticated:
author = current_user
else:
author = Guest(form.pseudo.data)
db.session.add(author)
# Create comment
c = Comment(author, form.message.data, p.thread)
db.session.add(c)
db.session.commit()
2022-05-26 21:08:01 +02:00
c.create_attachments(form.attachments.data)
# Update member's xp and trophies
if current_user.is_authenticated:
current_user.add_xp(1)
current_user.update_trophies('new-post')
flash('Message envoyé', 'ok')
app.v5logger.info(f"<{c.author.name}> has posted a the comment #{c.id}")
2023-06-12 20:04:20 +02:00
say(f"Nouveau commentaire de {author.name} sur le programme : {BOLD}{p.name}{BOLD}")
say(url_for('program_view', page=(p, "fin"), _anchor=str(c.id), _external=True))
# Redirect to empty the form
return redirect(url_for('program_view', page=(p, "fin"), _anchor=str(c.id)))
if page == -1:
page = (p.thread.comments.count() - 1) // Thread.COMMENTS_PER_PAGE + 1
comments = p.thread.comments.order_by(Comment.date_created.asc()) \
.paginate(page, Thread.COMMENTS_PER_PAGE, True)
return render('/programs/program.html', p=p, form=form, comments=comments)