From 604f9d6aba6a788f8affde22e82731da2131b6e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Tue, 1 Jul 2025 14:14:29 +0300 Subject: [PATCH] creating threads --- app/__init__.py | 2 ++ app/routes/app.py | 5 ++++ app/routes/posts.py | 26 +++++++++++++++++ app/routes/threads.py | 46 +++++++++++++++++++++++++++-- app/templates/common/macros.html | 48 ++++++++++++++++++++++++++++++- app/templates/threads/create.html | 21 ++++++++++++++ app/templates/topics/topic.html | 19 +++++++----- data/static/style.css | 4 +++ sass/style.scss | 4 +++ 9 files changed, 165 insertions(+), 10 deletions(-) create mode 100644 app/routes/posts.py create mode 100644 app/templates/threads/create.html diff --git a/app/__init__.py b/app/__init__.py index 3a49a34..4fc7569 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -70,10 +70,12 @@ def create_app(): from app.routes.app import bp as app_bp from app.routes.topics import bp as topics_bp + from app.routes.threads import bp as threads_bp from app.routes.users import bp as users_bp from app.routes.mod import bp as mod_bp app.register_blueprint(app_bp) app.register_blueprint(topics_bp) + app.register_blueprint(threads_bp) app.register_blueprint(users_bp) app.register_blueprint(mod_bp) diff --git a/app/routes/app.py b/app/routes/app.py index bef5e5a..0892bc3 100644 --- a/app/routes/app.py +++ b/app/routes/app.py @@ -5,3 +5,8 @@ bp = Blueprint("app", __name__, url_prefix = "/") @bp.route("/") def index(): return redirect(url_for("topics.all_topics")) + + +@bp.route("/babycode") +def babycode_guide(): + return "not yet" diff --git a/app/routes/posts.py b/app/routes/posts.py new file mode 100644 index 0000000..4677a21 --- /dev/null +++ b/app/routes/posts.py @@ -0,0 +1,26 @@ +from flask import Blueprint, redirect, url_for +from ..lib.babycode import babycode_to_html +from ..db import db +from ..models import Posts, PostHistory + +bp = Blueprint("posts", __name__, url_prefix = "/posts") + +def create_post(thread_id, user_id, content, markup_language="babycode"): + with db.transaction(): + post = Posts.create({ + "thread_id": thread_id, + "user_id": user_id, + "current_revision_id": None, + }) + + revision = PostHistory.create({ + "post_id": post.id, + "content": babycode_to_html(content), + "is_initial_revision": True, + "original_markup": content, + "markup_language": markup_language, + }) + + post.update({"current_revision_id": revision.id}) + return post + diff --git a/app/routes/threads.py b/app/routes/threads.py index fdaca97..2d4c7ff 100644 --- a/app/routes/threads.py +++ b/app/routes/threads.py @@ -1,7 +1,49 @@ -from flask import Blueprint, render_template +from flask import ( + Blueprint, render_template, request, redirect, url_for + ) +from .users import login_required, get_active_user +from ..models import Threads, Topics +from .posts import create_post +from slugify import slugify +import time + +bp = Blueprint("threads", __name__, url_prefix = "/threads/") -bp = Blueprint("topics", __name__, url_prefix = "/threads/") @bp.get("/") def thread(slug): return slug + + +@bp.get("/create") +@login_required +def create(): + all_topics = Topics.select() + return render_template("threads/create.html", all_topics = all_topics) + + +@bp.post("/create") +@login_required +def create_form(): + topic = Topics.find({"id": request.form['topic_id']}) + user = get_active_user() + if not topic: + return "no" + + if topic.is_locked and not get_active_user().is_mod(): + return "no" + + title = request.form['title'].strip() + now = int(time.time()) + slug = f"{slugify(title)}-{now}" + + post_content = request.form['initial_post'] + thread = Threads.create({ + "topic_id": topic.id, + "user_id": user.id, + "title": title, + "slug": slug, + "created_at": now, + }) + post = create_post(thread.id, user.id, post_content) + return redirect(url_for(".thread", slug = thread.slug)) diff --git a/app/templates/common/macros.html b/app/templates/common/macros.html index 51875a6..7520e04 100644 --- a/app/templates/common/macros.html +++ b/app/templates/common/macros.html @@ -1,7 +1,6 @@ {% macro pager(current_page, page_count) %} {% set left_start = (1, current_page - 5) | max %} {% set right_end = (page_count, current_page + 5) | min %} -
Page: {% if current_page > 5 %} @@ -42,3 +41,50 @@ {% macro timestamp(unix_ts) %} {{ unix_ts | ts_datetime('%Y-%m-%d %H:%M')}} ST {% endmacro %} + +{% macro babycode_editor_component(ta_name, ta_placeholder="Post body", optional=False, prefill="") %} +
+
+ + +
+
+ + + + + + + + + + + + babycode guide +
+
+
Type something!
+
+
+
+ +{% endmacro %} + +{% macro babycode_editor_form(ta_name, prefill = "", cancel_url="", endpoint="") %} +{% set save_button_text = "Post reply" if not cancel_url else "Save" %} +
+ {{babycode_editor_component(ta_name, prefill = prefill)}} + {% if not cancel_url %} + + + + + {% endif %} + + + {% if cancel_url %} + Cancel + {% endif %} + +
+{% endmacro %} diff --git a/app/templates/threads/create.html b/app/templates/threads/create.html new file mode 100644 index 0000000..85c9e1a --- /dev/null +++ b/app/templates/threads/create.html @@ -0,0 +1,21 @@ +{% from "common/macros.html" import babycode_editor_component %} +{% extends "base.html" %} +{% block title %}drafting a thread{% endblock %} +{% block content %} +
+

New thread

+
+ +
+ + +
+ {{ babycode_editor_component("initial_post") }} + +
+
+{% endblock %} diff --git a/app/templates/topics/topic.html b/app/templates/topics/topic.html index ca4c59d..bccacf3 100644 --- a/app/templates/topics/topic.html +++ b/app/templates/topics/topic.html @@ -6,13 +6,18 @@

All threads in "{{topic['name']}}"

{{topic['description']}}
- {% if active_user and active_user.is_mod() %} - Edit topic -
- - -
- + {% if active_user %} + {% if not (topic['is_locked']) | int or active_user.is_mod() %} + New thread + {% endif %} + {% if active_user.is_mod() %} + Edit topic +
+ + +
+ + {% endif %} {% endif %}
diff --git a/data/static/style.css b/data/static/style.css index c72806d..130f0be 100644 --- a/data/static/style.css +++ b/data/static/style.css @@ -467,6 +467,10 @@ input[type=text]:focus, input[type=password]:focus, textarea:focus, select:focus height: 50%; width: 50%; } +.contain-svg.full > svg, .contain-svg img { + height: 100%; + width: 100%; +} .block-img { object-fit: contain; diff --git a/sass/style.scss b/sass/style.scss index 5867ce3..bb520b1 100644 --- a/sass/style.scss +++ b/sass/style.scss @@ -476,6 +476,10 @@ input[type="text"], input[type="password"], textarea, select { height: 50%; width: 50%; } + &.full > svg, img { + height: 100%; + width: 100%; + } } .block-img {