add new thread route
This commit is contained in:
@@ -239,6 +239,21 @@ class Threads(Model):
|
|||||||
def stickied(self):
|
def stickied(self):
|
||||||
return bool(self.is_stickied)
|
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):
|
class Posts(Model):
|
||||||
FULL_POSTS_QUERY = """
|
FULL_POSTS_QUERY = """
|
||||||
WITH user_badges AS (
|
WITH user_badges AS (
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ def thread(slug):
|
|||||||
posts_count = Posts.count({'thread_id': thread.id})
|
posts_count = Posts.count({'thread_id': thread.id})
|
||||||
page_count = max(1, math.ceil(posts_count / PER_PAGE))
|
page_count = max(1, math.ceil(posts_count / PER_PAGE))
|
||||||
page = 1
|
page = 1
|
||||||
after = request.args.get('after', default=None)
|
after = request.args.get('after')
|
||||||
if after is not None:
|
if after is not None:
|
||||||
try:
|
try:
|
||||||
after_id = int(after)
|
after_id = int(after)
|
||||||
@@ -56,7 +56,37 @@ def feed(slug):
|
|||||||
def new():
|
def new():
|
||||||
topics = Topics.select()
|
topics = Topics.select()
|
||||||
try:
|
try:
|
||||||
selected_topic = int(request.args.get('topic_id', default=None))
|
selected_topic = int(request.args.get('topic_id'))
|
||||||
except ValueError, TypeError:
|
except ValueError, TypeError:
|
||||||
selected_topic = None
|
selected_topic = None
|
||||||
return render_template('threads/new_thread.html', topics=topics, selected_topic=selected_topic)
|
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))
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
<label for="topic">Topic</label>
|
<label for="topic">Topic</label>
|
||||||
<select name="topic_id" id="topic" autocomplete="off">
|
<select name="topic_id" id="topic" autocomplete="off">
|
||||||
{%- for topic in topics -%}
|
{%- 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 -%}
|
{%- endfor -%}
|
||||||
</select>
|
</select>
|
||||||
<label for="title">Title</label>
|
<label for="title">Title</label>
|
||||||
|
|||||||
Reference in New Issue
Block a user