diff --git a/app/routes/threads.py b/app/routes/threads.py index cba37e8..3b4a1f8 100644 --- a/app/routes/threads.py +++ b/app/routes/threads.py @@ -18,7 +18,7 @@ def thread(slug): POSTS_PER_PAGE = 10 thread = Threads.find({"slug": slug}) if not thread: - return "no" + return redirect(url_for('topics.all_topics')) post_count = Posts.count({"thread_id": thread.id}) page_count = max(math.ceil(post_count / POSTS_PER_PAGE), 1) @@ -69,12 +69,12 @@ def thread(slug): def reply(slug): thread = Threads.find({"slug": slug}) if not thread: - return "no" + return redirect(url_for('topics.all_topics')) user = get_active_user() if user.is_guest(): - return "no" + return redirect(url_for('.thread', slug=slug)) if thread.locked() and not user.is_mod(): - return "no" + return redirect(url_for('.thread', slug=slug)) post_content = request.form['post_content'] post = create_post(thread.id, user.id, post_content) @@ -102,10 +102,12 @@ def create_form(): topic = Topics.find({"id": request.form['topic_id']}) user = get_active_user() if not topic: - return "no" + flash('Invalid topic', InfoboxKind.ERROR) + return redirect(url_for('.create')) if topic.is_locked and not get_active_user().is_mod(): - return "no" + flash(f'Topic "{topic.name}" is locked', InfoboxKind.ERROR) + return redirect(url_for('.create')) title = request.form['title'].strip() now = int(time.time()) @@ -128,8 +130,10 @@ def create_form(): def lock(slug): user = get_active_user() thread = Threads.find({'slug': slug}) + if not thread: + return redirect(url_for('topics.all_topics')) if not ((thread.user_id == user.id) or user.is_mod()): - return 'no' + return redirect(url_for('.thread', slug=slug)) target_op = request.form.get('target_op') thread.update({ 'is_locked': target_op @@ -143,8 +147,10 @@ def lock(slug): def sticky(slug): user = get_active_user() thread = Threads.find({'slug': slug}) + if not thread: + return redirect(url_for('topics.all_topics')) if not ((thread.user_id == user.id) or user.is_mod()): - return 'no' + return redirect(url_for('.thread', slug=slug)) target_op = request.form.get('target_op') thread.update({ 'is_stickied': target_op @@ -167,12 +173,12 @@ def move(slug): 'id': new_topic_id }) if not new_topic: - return 'no' + return redirect(url_for('topics.all_topics')) thread = Threads.find({ 'slug': slug }) if not thread: - return 'no' + return redirect(url_for('topics.all_topics')) if new_topic.id == thread.topic_id: flash('Thread is already in this topic.', InfoboxKind.ERROR) return redirect(url_for('.thread', slug=slug)) @@ -189,7 +195,7 @@ def subscribe(slug): user = get_active_user() thread = Threads.find({'slug': slug}) if not thread: - return 'no' + return redirect(url_for('topics.all_topics')) subscription = Subscriptions.find({ 'user_id': user.id, 'thread_id': thread.id, @@ -204,11 +210,11 @@ def subscribe(slug): }) elif request.form['subscribe'] == 'unsubscribe': if not subscription: - return 'no' + return redirect(url_for('.thread', slug=slug)) subscription.delete() elif request.form['subscribe'] == 'read': if not subscription: - return 'no' + return redirect(url_for('.thread', slug=slug)) subscription.update({ 'last_seen': int(time.time()) }) diff --git a/app/routes/topics.py b/app/routes/topics.py index dcfaca6..b13a899 100644 --- a/app/routes/topics.py +++ b/app/routes/topics.py @@ -51,7 +51,7 @@ def topic(slug): "slug": slug }) if not target_topic: - return "no" + return redirect(url_for('.all_topics')) threads_count = Threads.count({ "topic_id": target_topic.id @@ -77,7 +77,7 @@ def topic(slug): def edit(slug): topic = Topics.find({"slug": slug}) if not topic: - return "no" + return redirect(url_for('.all_topics')) return render_template("topics/edit.html", topic=topic) @@ -87,7 +87,7 @@ def edit(slug): def edit_post(slug): topic = Topics.find({"slug": slug}) if not topic: - return "no" + return redirect(url_for('.all_topics')) topic.update({ "name": request.form.get('name', default = topic.name).strip(), @@ -104,7 +104,7 @@ def edit_post(slug): def delete(slug): topic = Topics.find({"slug": slug}) if not topic: - return "no" + return redirect(url_for('.all_topics')) topic.delete() diff --git a/app/routes/users.py b/app/routes/users.py index e19093b..e9837bb 100644 --- a/app/routes/users.py +++ b/app/routes/users.py @@ -269,14 +269,17 @@ def settings_form(username): def set_avatar(username): user = get_active_user() if user.is_guest(): - return 'no' + flash('You must be logged in to perform this action.', InfoboxKind.ERROR) + return redirect(url_for('.settings', user.username)) if 'avatar' not in request.files: - return 'no!...' + flash('Avatar missing.', InfoboxKind.ERROR) + return redirect(url_for('.settings', user.username)) file = request.files['avatar'] if file.filename == '': - return 'no..?' + flash('Avatar missing.', InfoboxKind.ERROR) + return redirect(url_for('.settings', user.username)) file_bytes = file.read() @@ -300,7 +303,8 @@ def set_avatar(username): old_avatar.delete() return redirect(url_for('.settings', username=user.username)) else: - return 'uhhhh no' + flash('Something went wrong. Please try again later.', InfoboxKind.WARN) + return redirect(url_for('.settings', user.username)) @bp.post('//clear_avatar') @@ -308,7 +312,7 @@ def set_avatar(username): def clear_avatar(username): user = get_active_user() if user.is_default_avatar(): - return 'no' + return redirect(url_for('.settings', user.username)) old_avatar = Avatars.find({'id': user.avatar_id}) user.update({'avatar_id': 1}) @@ -336,9 +340,9 @@ def log_out(): def confirm_user(user_id): target_user = Users.find({"id": user_id}) if not target_user: - return "no" + return redirect(url_for('.all_topics')) if int(target_user.permission) > PermissionLevel.GUEST.value: - return "no" + return redirect(url_for('.page', username=target_user.username)) target_user.update({ "permission": PermissionLevel.USER.value, @@ -353,9 +357,9 @@ def confirm_user(user_id): def mod_user(user_id): target_user = Users.find({"id": user_id}) if not target_user: - return "no" + return redirect(url_for('.all_topics')) if target_user.is_mod(): - return "no" + return redirect(url_for('.page', username=target_user.username)) target_user.update({ "permission": PermissionLevel.MODERATOR.value, @@ -369,9 +373,9 @@ def mod_user(user_id): def demod_user(user_id): target_user = Users.find({"id": user_id}) if not target_user: - return "no" + return redirect(url_for('.all_topics')) if not target_user.is_mod(): - return "no" + return redirect(url_for('.page', username=target_user.username)) target_user.update({ "permission": PermissionLevel.USER.value, @@ -385,9 +389,9 @@ def demod_user(user_id): def guest_user(user_id): target_user = Users.find({"id": user_id}) if not target_user: - return "no" + return redirect(url_for('.all_topics')) if target_user.is_mod(): - return "no" + return redirect(url_for('.page', username=target_user.username)) target_user.update({ "permission": PermissionLevel.GUEST.value,