diff --git a/app/routes/mod.py b/app/routes/mod.py index 3e9c929..ee514c2 100644 --- a/app/routes/mod.py +++ b/app/routes/mod.py @@ -1,6 +1,8 @@ -from flask import Blueprint, abort, redirect, url_for, request, render_template +from flask import Blueprint, abort, redirect, url_for, request, render_template, flash +from ..constants import InfoboxKind from ..auth import is_logged_in, get_active_user, csrf_verified from ..models import Topics, Threads +from slugify import slugify bp = Blueprint('mod', __name__, url_prefix='/mod/') @bp.before_request @@ -39,9 +41,21 @@ def edit_topic_post(topic_id): topic = Topics.find({'id': topic_id}) if not topic: abort(404) + + target_name = request.form.get('name').strip() + + name_exists = Topics.count([ + ('lower(name)', '=', target_name.lower()), + ('id', '!=', topic.id) + ]) > 0 + if name_exists: + flash(f'A topic named "{target_name}" already exists.', InfoboxKind.ERROR) + return redirect(url_for('.edit_topic', topic_id=topic_id)) + topic.update({ - 'name': request.form.get('name').strip(), + 'name': target_name, 'description': request.form.get('description').strip(), + 'slug': slugify(target_name[:50]), }) return redirect(url_for('topics.topic_by_id', topic_id=topic.id)) diff --git a/app/templates/base.html b/app/templates/base.html index 0ab750b..9a9e64d 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -1,3 +1,4 @@ +{%- from 'common/macros.html' import infobox with context -%}
@@ -13,6 +14,13 @@ {%- include 'common/topnav.html' -%} + {%- with messages = get_flashed_messages(with_categories=true) -%} + {%- if messages -%} + {%- for category, message in messages -%} + {{- infobox(message, category) -}} + {%- endfor -%} + {%- endif -%} + {%- endwith -%} {%- block content -%}{%- endblock -%} {%- include 'common/footer.html' -%} diff --git a/app/templates/common/icons.html b/app/templates/common/icons.html new file mode 100644 index 0000000..52ef0a8 --- /dev/null +++ b/app/templates/common/icons.html @@ -0,0 +1,11 @@ +{%- macro icn_info(width=48) -%} + +{%- endmacro -%} + +{%- macro icn_warn(width=48) -%} + +{%- endmacro -%} + +{%- macro icn_error(width=48) -%} + +{%- endmacro -%} diff --git a/app/templates/common/macros.html b/app/templates/common/macros.html index bfef71e..c63bed9 100644 --- a/app/templates/common/macros.html +++ b/app/templates/common/macros.html @@ -1,3 +1,5 @@ +{%- from 'common/icons.html' import icn_info, icn_warn, icn_error -%} + {% macro timestamp(unix_ts) -%} {%- endmacro %} @@ -180,3 +182,20 @@ {%- endif -%} {%- endmacro %} + +{% macro infobox(message, kind=InfoboxKind.INFO) -%} +