diff --git a/app/routes/forum/index.py b/app/routes/forum/index.py index ad08dbc..7ea9782 100644 --- a/app/routes/forum/index.py +++ b/app/routes/forum/index.py @@ -4,11 +4,12 @@ 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 +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/') @@ -17,7 +18,10 @@ def forum_index(): @app.route('/forum//', methods=['GET', 'POST']) def forum_page(f): - form = TopicCreationForm() + if current_user.is_authenticated: + form = TopicCreationForm() + else: + form = AnonymousTopicCreationForm() # TODO: do not hardcode name of news forums if form.validate_on_submit() and ( @@ -27,28 +31,37 @@ def forum_page(f): 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)): + or ("/actus" not in f.url and not f.sub_forums)) and ( + V5Config.ENABLE_GUEST_POST or current_user.is_authenticated): + + print("User can post topic") # First create the thread, then the comment, then the topic th = Thread() db.session.add(th) db.session.commit() - c = Comment(current_user, form.message.data, th) + 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, current_user, form.title.data, th) + t = Topic(f, author, form.title.data, th) db.session.add(t) db.session.commit() # Update member's xp and trophies - current_user.add_xp(V5Config.XP_POINTS['topic']) - current_user.update_trophies('new-post') - + if current_user.is_authenticated: + current_user.add_xp(V5Config.XP_POINTS['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))) diff --git a/app/templates/forum/forum.html b/app/templates/forum/forum.html index 04a3496..3ce57e9 100644 --- a/app/templates/forum/forum.html +++ b/app/templates/forum/forum.html @@ -43,11 +43,21 @@ {% if (current_user.is_authenticated and current_user.priv('write-anywhere')) or ("/actus" in f.url and current_user.is_authenticated and current_user.priv('write-news')) - or ("/actus" not in f.url and not f.sub_forums) %} + or ("/actus" not in f.url and not f.sub_forums) + and (current_user.is_authenticated or V5Config.ENABLE_GUEST_POST) %}

Créer un nouveau sujet

{{ form.hidden_tag() }} + {% if form.pseudo %} +
+ {{ form.pseudo.label }} + {{ form.pseudo() }} + {% for error in form.pseudo.errors %} + {{ error }} + {% endfor %} +
+ {% endif %}
{{ form.title.label }} diff --git a/app/templates/forum/topic.html b/app/templates/forum/topic.html index 58076bd..50ba387 100644 --- a/app/templates/forum/topic.html +++ b/app/templates/forum/topic.html @@ -11,7 +11,11 @@

{{ t.title }}

- + {% if t.author.type == "member" %} + + {% else %} + + {% endif %}
{{ widget_member.profile(t.author ) }}{{ widget_member.profile(t.author ) }}Invité{{ t.author.name }}{{ t.thread.top_comment.text }}