diff --git a/app/__init__.py b/app/__init__.py index d7a415a..b7c6ded 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -203,12 +203,14 @@ def create_app(): from app.routes.guides import bp as guides_bp from app.routes.mod import bp as mod_bp from app.routes.posts import bp as posts_bp + from app.routes.api import bp as api_bp app.register_blueprint(topics_bp) app.register_blueprint(threads_bp) app.register_blueprint(users_bp) app.register_blueprint(guides_bp) app.register_blueprint(mod_bp) app.register_blueprint(posts_bp) + app.register_blueprint(api_bp) with app.app_context(): from .schema import create as create_tables diff --git a/app/auth.py b/app/auth.py index bfb88ef..2e83818 100644 --- a/app/auth.py +++ b/app/auth.py @@ -105,6 +105,14 @@ def login_required(view_func): return view_func(*args, **kwargs) return wrapper +def hard_login_required(view_func): + @wraps(view_func) + def wrapper(*args, **kwargs): + if not is_logged_in(): + abort(403) + return view_func(*args, **kwargs) + return wrapper + def mod_only(view_func): @wraps(view_func) def wrapper(*args, **kwargs): diff --git a/app/routes/api.py b/app/routes/api.py new file mode 100644 index 0000000..40a14a4 --- /dev/null +++ b/app/routes/api.py @@ -0,0 +1,21 @@ +from flask import Blueprint, request +from ..auth import is_logged_in, hard_login_required, get_active_user +from ..lib.babycode import babycode_to_html +from ..models import APIRateLimits + +bp = Blueprint('api', __name__, url_prefix='/api/') + +@bp.post('/babycode-preview/') +@hard_login_required +def babycode_preview(): + user = get_active_user() + if not APIRateLimits.is_allowed(user.id, 'babycode_preview', 5): + return {'error': 'too many requests'}, 429 + markup = str(request.json.get('markup', '')) + if not markup: + return {'error': 'markup field missing or invalid type'}, 400 + banned_tags = request.json.get('banned_tags', []) + if not isinstance(banned_tags, list): + return {'error': 'banned_tags field is invalid type'}, 400 + rendered = babycode_to_html(markup, banned_tags).result + return {'html': rendered} diff --git a/app/templates/common/macros.html b/app/templates/common/macros.html index 99aad5a..126ab7a 100644 --- a/app/templates/common/macros.html +++ b/app/templates/common/macros.html @@ -72,15 +72,15 @@ {%- endmacro %} -{% macro tabs(prefix='', labels = []) -%} +{% macro tabs(prefix='', labels=[], signal_ss=[], signal_rs=[]) -%}