creating threads
This commit is contained in:
@ -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