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):
|
review of privileges and forum permissions
* Sorted privileges into categories, similar to the v4.3 style
Added privilege check utilities:
* Forum: is_news(), is_default_accessible() and is_default_postable()
* Member: can_access_forum(), can_post_in_forum(), can_edit_post(),
and can_delete_post()
Unfortunately current_user is not a Guest when logged out, so one
cannot usually write current_user.can_*() without checking for
authentication first, so the checks are still somewhat verbose.
Reviewed forum permissions; the following permission issues have been
fixed (I have tested most but not all of them prior to fixing):
* app/routes/forum/index.py: Users that were not meant to access a
forum could still obtain a listing of the topics
* app/routes/forum/topic.py: Users that were not meant to see topics
could still read them by browsing the URL
* app/routes/forum/topic.py: Authenticated users could post in any
topic, including ones that they should not have access to
* app/routes/posts/edit.py: Users with edit.posts (eg. mods) could edit
and delete messages in forums they can't access (eg. creativecalc)
* app/templates/account/user.html: Users with admin panel access would
see account editing links they can't use (affects developers)
* app/templates/base/navbar/forum.html: The "Forum" tab would list all
forums including ones the user doesn't have access to
* app/templates/forum/index.html: Users would see every single forum,
including ones they can't access
* app/template/widgets/thread.html: Anyone would see Edit/Delete links
on every message, even though most were unusable
Miscellaneous changes:
* app/routes/forum/topic.py: Ordered comments by date as intended,
which I assume worked by chance until now
* Removed the old assets/privs.txt files which is now superseded by the
list implemented in app/data/groups.yaml
This commit changes group and forum information, run master.py with:
@> forums update
@> groups update
2021-02-26 18:29:25 +01:00
|
|
|
if not f.is_default_accessible() and not (
|
|
|
|
current_user.is_authenticated and current_user.can_access_forum(f)):
|
|
|
|
abort(403)
|
|
|
|
|
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
|
|
|
if form.validate_on_submit() and (
|
review of privileges and forum permissions
* Sorted privileges into categories, similar to the v4.3 style
Added privilege check utilities:
* Forum: is_news(), is_default_accessible() and is_default_postable()
* Member: can_access_forum(), can_post_in_forum(), can_edit_post(),
and can_delete_post()
Unfortunately current_user is not a Guest when logged out, so one
cannot usually write current_user.can_*() without checking for
authentication first, so the checks are still somewhat verbose.
Reviewed forum permissions; the following permission issues have been
fixed (I have tested most but not all of them prior to fixing):
* app/routes/forum/index.py: Users that were not meant to access a
forum could still obtain a listing of the topics
* app/routes/forum/topic.py: Users that were not meant to see topics
could still read them by browsing the URL
* app/routes/forum/topic.py: Authenticated users could post in any
topic, including ones that they should not have access to
* app/routes/posts/edit.py: Users with edit.posts (eg. mods) could edit
and delete messages in forums they can't access (eg. creativecalc)
* app/templates/account/user.html: Users with admin panel access would
see account editing links they can't use (affects developers)
* app/templates/base/navbar/forum.html: The "Forum" tab would list all
forums including ones the user doesn't have access to
* app/templates/forum/index.html: Users would see every single forum,
including ones they can't access
* app/template/widgets/thread.html: Anyone would see Edit/Delete links
on every message, even though most were unusable
Miscellaneous changes:
* app/routes/forum/topic.py: Ordered comments by date as intended,
which I assume worked by chance until now
* Removed the old assets/privs.txt files which is now superseded by the
list implemented in app/data/groups.yaml
This commit changes group and forum information, run master.py with:
@> forums update
@> groups update
2021-02-26 18:29:25 +01:00
|
|
|
(V5Config.ENABLE_GUEST_POST and f.is_default_postable()) or \
|
|
|
|
(current_user.is_authenticated and current_user.can_post_in_forum(f))):
|
2020-07-18 07:52:29 +02:00
|
|
|
|
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)
|