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) -%} {{ unix_ts | ts_datetime('%Y-%m-%d %H:%M')}} ST {%- endmacro %} @@ -180,3 +182,20 @@ {%- endif -%} {%- endmacro %} + +{% macro infobox(message, kind=InfoboxKind.INFO) -%} +
+ {%- if kind == InfoboxKind.INFO -%} + {{- icn_info() -}} + {%- elif kind == InfoboxKind.WARN -%} + {{- icn_warn() -}} + {%- elif kind == InfoboxKind.ERROR -%} + {{- icn_error() -}} + {%- endif -%} + {%- set m = message.split(';', maxsplit=1) -%} + {{m[0]}} + {%- if m[1] -%} + {{m[1]}} + {%- endif -%} +
+{%- endmacro %} diff --git a/app/templates/threads/thread.html b/app/templates/threads/thread.html index 78b5d75..a6c4f3e 100644 --- a/app/templates/threads/thread.html +++ b/app/templates/threads/thread.html @@ -21,7 +21,7 @@ Actions {%- if is_logged_in() -%} {%- if thread.user_id == get_active_user().id -%} - Edit… + Edit {%- endif -%} @@ -32,7 +32,7 @@
Moderation actions {%- if thread.user_id != get_active_user().id -%} - Edit… + Edit {%- endif -%}
diff --git a/app/templates/topics/topics.html b/app/templates/topics/topics.html index f9a2412..5816dc0 100644 --- a/app/templates/topics/topics.html +++ b/app/templates/topics/topics.html @@ -1,4 +1,5 @@ {% from 'common/macros.html' import timestamp, subheader %} +{% from 'common/icons.html' import icn_info, icn_warn, icn_error %} {%- extends 'base.html' -%} {%- block content -%} {%- call() subheader('All topics') -%} diff --git a/data/static/css/style.css b/data/static/css/style.css index 946db0c..7c33b77 100644 --- a/data/static/css/style.css +++ b/data/static/css/style.css @@ -411,11 +411,11 @@ ul.horizontal, ol.horizontal { color: oklch(from var(--main-color) round(1.21 - L) 0 0); &.critical { - --main-color: hsl(from var(--critical-color) h 50% calc(l * 0.7)); + --main-color: hsl(from var(--critical-color) h 50% l); } &.warn { - --main-color: hsl(from var(--warn-color) h 50% calc(l * 1.2)); + --main-color: hsl(from var(--warn-color) h 50% 75%); } }