diff --git a/freak/__init__.py b/freak/__init__.py index eaa67c4..c191ef2 100644 --- a/freak/__init__.py +++ b/freak/__init__.py @@ -163,6 +163,8 @@ async def error_handler_for(status: int, message: str, template: str): case WantsContentType.JSON: return jsonify({'error': f'{message}', 'status': status}), status case WantsContentType.HTML: + if request.path.startswith('/admin'): + return await render_template('admin/' + template, message=f'{message}'), status return await render_template(template, message=f'{message}'), status case WantsContentType.PLAIN: return f'{message} (HTTP {status})', status, {'content-type': 'text/plain; charset=UTF-8'} diff --git a/freak/templates/admin/400.html b/freak/templates/admin/400.html new file mode 100644 index 0000000..302b34c --- /dev/null +++ b/freak/templates/admin/400.html @@ -0,0 +1,10 @@ +{% extends "admin/admin_base.html" %} + + +{% block content %} +
+

Bad Request

+ +

Back to homepage.

+
+{% endblock %} diff --git a/freak/templates/admin/403.html b/freak/templates/admin/403.html new file mode 100644 index 0000000..d48cc53 --- /dev/null +++ b/freak/templates/admin/403.html @@ -0,0 +1,12 @@ +{% extends "admin/admin_base.html" %} +{% from "macros/title.html" import title_tag with context %} + + + +{% block content %} +
+

Access Denied

+ +

Back to homepage.

+
+{% endblock %} diff --git a/freak/templates/admin/404.html b/freak/templates/admin/404.html new file mode 100644 index 0000000..b7dd7ff --- /dev/null +++ b/freak/templates/admin/404.html @@ -0,0 +1,12 @@ +{% extends "admin/admin_base.html" %} +{% from "macros/title.html" import title_tag with context %} + + + +{% block content %} +
+

Not Found

+ +

Back

+
+{% endblock %} diff --git a/freak/templates/admin/500.html b/freak/templates/admin/500.html new file mode 100644 index 0000000..23e2f50 --- /dev/null +++ b/freak/templates/admin/500.html @@ -0,0 +1,11 @@ +{% extends "admin/admin_base.html" %} + + +{% block content %} +
+

Internal Server Error

+ +

It's on us. Refresh the page.

+
+{% endblock %} + diff --git a/freak/templates/admin/admin_base.html b/freak/templates/admin/admin_base.html index 5288f35..46d9655 100644 --- a/freak/templates/admin/admin_base.html +++ b/freak/templates/admin/admin_base.html @@ -5,7 +5,7 @@ {{ title_tag("Admin") }} - + {% for private_style in private_styles %} {% endfor %} diff --git a/freak/templates/macros/embed.html b/freak/templates/macros/embed.html index c96db78..8b5633c 100644 --- a/freak/templates/macros/embed.html +++ b/freak/templates/macros/embed.html @@ -1,6 +1,6 @@ {% macro embed_post(p) %}
-

{{ p.title }}

+

{{ p.title }}

Posted by @{{ p.author.username }} {% if p.parent_post %} as a comment on post “{{ p.parent_post.title }}” diff --git a/freak/website/admin.py b/freak/website/admin.py index 46cb9b0..accb25e 100644 --- a/freak/website/admin.py +++ b/freak/website/admin.py @@ -2,9 +2,10 @@ import datetime from functools import wraps +import os from typing import Callable import warnings -from quart import Blueprint, abort, redirect, render_template, request, url_for +from quart import Blueprint, abort, redirect, render_template, request, send_from_directory, url_for from quart_auth import current_user from markupsafe import Markup from sqlalchemy import insert, select, update @@ -23,11 +24,11 @@ current_user: UserLoader def admin_required(func: Callable): @wraps(func) - def wrapper(*a, **ka): + async def wrapper(*a, **ka): user: User = current_user.user if not user or not user.is_administrator: abort(403) - return func(*a, **ka) + return await func(*a, **ka) return wrapper @@ -155,10 +156,14 @@ def escalate_report(target, source: PostReport): async def homepage(): return await render_template('admin/admin_home.html') +@bp.route('/admin/style.css') +async def style_css(): + return await send_from_directory(os.path.dirname(os.path.dirname(__file__)) + '/static/css', 'style.css') + @bp.route('/admin/reports/') @admin_required async def reports(): - report_list = db.paginate(select(PostReport).order_by(PostReport.id.desc())) + report_list = await db.paginate(select(PostReport).order_by(PostReport.id.desc())) return await render_template('admin/admin_reports.html', report_list=report_list, report_reasons=REPORT_REASON_STRINGS) @@ -169,10 +174,13 @@ async def report_detail(id: int): report = (await session.execute(select(PostReport).where(PostReport.id == id))).scalar() if report is None: abort(404) + target = await report.target() + if target is None: + abort(404) if request.method == 'POST': form = await get_request_form() action = REPORT_ACTIONS[form['do']] - await action(report.target(), report) + await action(target, report) return redirect(url_for('admin.reports')) return await render_template('admin/admin_report_detail.html', report=report, report_reasons=REPORT_REASON_STRINGS) @@ -188,7 +196,7 @@ async def strikes(): @bp.route('/admin/users/') @admin_required async def users(): - user_list = db.paginate(select(User).order_by(User.joined_at.desc())) + user_list = await db.paginate(select(User).order_by(User.joined_at.desc())) return await render_template('admin/admin_users.html', user_list=user_list, account_status_string=colorized_account_status_string) @@ -219,5 +227,7 @@ async def user_detail(id: int): else: abort(400) strikes = (await session.execute(select(UserStrike).where(UserStrike.user_id == id).order_by(UserStrike.id.desc()))).scalars() - return render_template('admin/admin_user_detail.html', u=u, + return await render_template('admin/admin_user_detail.html', u=u, report_reasons=REPORT_REASON_STRINGS, account_status_string=colorized_account_status_string, strikes=strikes) + + diff --git a/freak/website/reports.py b/freak/website/reports.py index f24273c..21b08c5 100644 --- a/freak/website/reports.py +++ b/freak/website/reports.py @@ -5,6 +5,7 @@ from __future__ import annotations from quart import Blueprint, render_template, request from quart_auth import current_user, login_required from sqlalchemy import insert, select +from suou import Snowflake from freak import UserLoader from ..models import REPORT_TARGET_COMMENT, REPORT_TARGET_POST, ReportReason, User, post_report_reasons, Comment, Post, PostReport, REPORT_REASONS, db @@ -35,7 +36,7 @@ async def report_post(id: int): reason_code = REPORT_REASONS[reason] )) session.commit() - return await render_template('reports/report_done.html', back_to_url=p.url()) + return await render_template('reports/report_done.html', back_to_url='/=' + Snowflake(p.id).to_b32l()) return await render_template('reports/report_post.html', id = id, report_reasons = post_report_reasons, description_text=description_text)