108 lines
2.6 KiB
Python
108 lines
2.6 KiB
Python
from flask import (
|
|
Blueprint, render_template, request, redirect, url_for
|
|
)
|
|
from .users import login_required, mod_only, get_active_user, is_logged_in
|
|
from ..db import db
|
|
from ..models import Threads, Topics, Posts
|
|
from .posts import create_post
|
|
from slugify import slugify
|
|
import math
|
|
import time
|
|
|
|
bp = Blueprint("threads", __name__, url_prefix = "/threads/")
|
|
|
|
|
|
@bp.get("/<slug>")
|
|
def thread(slug):
|
|
POSTS_PER_PAGE = 10
|
|
thread = Threads.find({"slug": slug})
|
|
if not thread:
|
|
return "no"
|
|
|
|
post_count = Posts.count({"thread_id": thread.id})
|
|
page_count = max(math.ceil(post_count / POSTS_PER_PAGE), 1)
|
|
|
|
page = 1
|
|
after = request.args.get("after", default=None)
|
|
if after is not None:
|
|
after_id = int(after)
|
|
post_position = Posts.count([
|
|
("thread_id", "=", thread.id),
|
|
("id", "<=", after_id),
|
|
])
|
|
print(post_position)
|
|
page = math.floor((post_position + 1) / POSTS_PER_PAGE) + 1
|
|
else:
|
|
page = max(1, min(page_count, int(request.args.get("page", default = 1))))
|
|
|
|
posts = thread.get_posts(POSTS_PER_PAGE, (page - 1) * POSTS_PER_PAGE)
|
|
print(posts)
|
|
topic = Topics.find({"id": thread.topic_id})
|
|
other_topics = Topics.select()
|
|
|
|
#TODO: subscription last seen
|
|
|
|
return render_template(
|
|
"threads/thread.html",
|
|
thread = thread,
|
|
current_page = page,
|
|
page_count = page_count,
|
|
posts = posts,
|
|
topic = topic,
|
|
topics = other_topics,
|
|
)
|
|
|
|
|
|
@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))
|
|
|
|
|
|
@bp.post("/<slug>/lock")
|
|
@login_required
|
|
def lock(slug):
|
|
pass
|
|
|
|
|
|
@bp.post("/<slug>/sticky")
|
|
@login_required
|
|
@mod_only(".thread", slug = lambda slug: slug)
|
|
def sticky(slug):
|
|
pass
|
|
|
|
|
|
@bp.post("/<slug>/move")
|
|
@login_required
|
|
@mod_only(".thread", slug = lambda slug: slug)
|
|
def move(slug):
|
|
pass
|