103 lines
3.3 KiB
Python
103 lines
3.3 KiB
Python
from flask import Blueprint, redirect, url_for, render_template, request, abort
|
|
from ..auth import login_required, get_active_user
|
|
from ..models import Threads, Posts, Topics, Users, Reactions
|
|
import math
|
|
|
|
bp = Blueprint('threads', __name__, url_prefix='/threads/')
|
|
|
|
@bp.get('/<int:thread_id>/')
|
|
def thread_by_id(thread_id):
|
|
thread = Threads.find({'id': thread_id})
|
|
if not thread:
|
|
abort(404)
|
|
return redirect(url_for('.thread', thread_id=thread_id, slug=thread.slug, **request.args))
|
|
|
|
@bp.get('/<int:thread_id>/<slug>/')
|
|
def thread(thread_id, slug):
|
|
thread = Threads.find({'id': thread_id})
|
|
if not thread:
|
|
abort(404)
|
|
if thread.slug != slug:
|
|
return redirect(url_for('.thread', thread_id=thread_id, slug=thread.slug, **request.kwargs))
|
|
|
|
topic = Topics.find({'id': thread.topic_id})
|
|
started_by = Users.find({'id': thread.user_id})
|
|
PER_PAGE = 10
|
|
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')
|
|
if after is not None:
|
|
try:
|
|
after_id = int(after)
|
|
post_position = Posts.count([
|
|
('thread_id', '=', thread.id),
|
|
('id', '<=', after_id),
|
|
])
|
|
page = math.ceil((post_position) / PER_PAGE)
|
|
except ValueError:
|
|
abort(404)
|
|
else:
|
|
try:
|
|
page = max(1, min(int(request.args.get('page', default=1)), page_count))
|
|
except ValueError:
|
|
abort(404)
|
|
return render_template('threads/thread.html', thread=thread, posts=thread.get_posts(PER_PAGE, page), page=page, page_count=page_count, topic=topic, started_by=started_by, topics=Topics.get_list(), Reactions=Reactions)
|
|
|
|
@bp.post('/<int:thread_id>/reply/')
|
|
@login_required
|
|
def reply(thread_id):
|
|
user = get_active_user()
|
|
thread = Threads.find({'id': thread_id})
|
|
if not thread:
|
|
abort(404)
|
|
if thread.locked() and not user.is_mod():
|
|
# TODO: flash
|
|
return redirect(url_for('.thread_by_id', thread_id=thread_id))
|
|
post = Posts.new(user.id, thread.id, request.form.get('babycode_content'))
|
|
return redirect(url_for('.thread_by_id', thread_id=thread_id, after=post.id, _anchor=f'post-{post.id}'))
|
|
|
|
@bp.get('/<int:thread_id>/feed.atom/')
|
|
def feed(thread_id):
|
|
return 'stub'
|
|
|
|
@bp.get('/new/')
|
|
@login_required
|
|
def new():
|
|
topics = Topics.select()
|
|
try:
|
|
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))
|