creating threads
This commit is contained in:
@ -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"
|
||||
|
26
app/routes/posts.py
Normal file
26
app/routes/posts.py
Normal file
@ -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
|
||||
|
@ -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("/<slug>")
|
||||
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))
|
||||
|
Reference in New Issue
Block a user