add new thread route

This commit is contained in:
2026-04-17 06:33:40 +03:00
parent 7c0cb623e3
commit 54ed6fef3a
3 changed files with 48 additions and 3 deletions

View File

@@ -239,6 +239,21 @@ class Threads(Model):
def stickied(self):
return bool(self.is_stickied)
@classmethod
def new(cls, user_id: int, topic_id: int, title: str, content: str, language: str = 'babycode') -> Threads:
from slugify import slugify
now = int(time.time())
slug = f'{slugify(title)}-{now}'
thread = Threads.create({
'topic_id': topic_id,
'user_id': user_id,
'title': title.strip(),
'slug': slug,
'created_at': int(time.time()),
})
post = Posts.new(user_id, thread.id, content, language)
return thread
class Posts(Model):
FULL_POSTS_QUERY = """
WITH user_badges AS (

View File

@@ -16,7 +16,7 @@ def thread(slug):
posts_count = Posts.count({'thread_id': thread.id})
page_count = max(1, math.ceil(posts_count / PER_PAGE))
page = 1
after = request.args.get('after', default=None)
after = request.args.get('after')
if after is not None:
try:
after_id = int(after)
@@ -56,7 +56,37 @@ def feed(slug):
def new():
topics = Topics.select()
try:
selected_topic = int(request.args.get('topic_id', default=None))
selected_topic = int(request.args.get('topic_id'))
except ValueError, TypeError:
selected_topic = None
return render_template('threads/new_thread.html', topics=topics, selected_topic=selected_topic)
@bp.post('/new')
@login_required
def new_post():
try:
topic_id = int(request.form.get('topic_id'))
except ValueError, TypeError:
abort(404)
topic_id = int(topic_id)
topic = Topics.find({'id': topic_id})
if not topic:
abort(404)
user = get_active_user()
if not user.can_post_to_topic(topic):
abort(404)
title = request.form.get('title')
if not title:
abort(404)
if not title.strip():
abort(404)
title = title.strip()
content = request.form.get('babycode_content')
thread = Threads.new(user.id, topic.id, title, content)
return redirect(url_for('.thread', slug=thread.slug))

View File

@@ -7,7 +7,7 @@
<label for="topic">Topic</label>
<select name="topic_id" id="topic" autocomplete="off">
{%- for topic in topics -%}
<option value="{{topic.id}}" {{'selected' if selected_topic == topic.id else ''}} {{'disabled' if not get_active_user().can_post_to_topic(topic) else ''}}>{{topic.name}}</option>
<option value="{{topic.id}}" {{'selected' if selected_topic == topic.id else ''}} {{'disabled' if not get_active_user().can_post_to_topic(topic) else ''}}>{{topic.name}}{{ ' (locked)' if topic.locked() else ''}}</option>
{%- endfor -%}
</select>
<label for="title">Title</label>