diff options
Diffstat (limited to 'apioforum/forum.py')
-rw-r--r-- | apioforum/forum.py | 48 |
1 files changed, 45 insertions, 3 deletions
diff --git a/apioforum/forum.py b/apioforum/forum.py index 410bee5..cae4eab 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -19,6 +19,19 @@ bp = Blueprint("forum", __name__, url_prefix="/") def not_actual_index(): return redirect("/1") +def get_avail_tags(forum_id): + db = get_db() + tags = db.execute(""" + WITH RECURSIVE fs AS + (SELECT * FROM forums WHERE id = ? + UNION ALL + SELECT forums.* FROM forums, fs WHERE fs.parent=forums.id) + SELECT * FROM tags + WHERE tags.forum in (SELECT id FROM fs) + ORDER BY id; + """,(forum_id,)).fetchall() + return tags + def forum_path(forum_id): db = get_db() ancestors = db.execute(""" @@ -73,7 +86,7 @@ def view_forum(forum): threads = db.execute( """SELECT threads.id, threads.title, threads.creator, threads.created, - threads.updated, number_of_posts.num_replies, + threads.updated, threads.poll, number_of_posts.num_replies, most_recent_posts.created as mrp_created, most_recent_posts.author as mrp_author, most_recent_posts.id as mrp_id, @@ -86,6 +99,10 @@ def view_forum(forum): ORDER BY threads.updated DESC; """,(forum['id'],)).fetchall() thread_tags = {} + thread_polls = {} + + avail_tags = get_avail_tags(forum['id']) + #todo: somehow optimise this for thread in threads: thread_tags[thread['id']] = db.execute( @@ -95,6 +112,29 @@ def view_forum(forum): ORDER BY tags.id; """,(thread['id'],)).fetchall() + if thread['poll'] is not None: + # todo: make this not be duplicated from thread.py + poll_row= db.execute(""" + SELECT polls.*,total_vote_counts.total_votes FROM polls + LEFT OUTER JOIN total_vote_counts ON polls.id = total_vote_counts.poll + WHERE polls.id = ?; + """,(thread['poll'],)).fetchone() + options = db.execute(""" + SELECT poll_options.*, vote_counts.num + FROM poll_options + LEFT OUTER JOIN vote_counts ON poll_options.poll = vote_counts.poll + AND poll_options.option_idx = vote_counts.option_idx + WHERE poll_options.poll = ? + ORDER BY option_idx asc; + """,(poll_row['id'],)).fetchall() + + poll = {} + poll.update(poll_row) + poll['options'] = options + poll['total_votes']=poll['total_votes'] or 0 + thread_polls[thread['id']]=poll + + subforums_rows = db.execute(""" SELECT max(threads.updated) as updated, forums.* FROM forums LEFT OUTER JOIN threads ON threads.forum=forums.id @@ -121,7 +161,9 @@ def view_forum(forum): subforums=subforums, threads=threads, thread_tags=thread_tags, - bureaucrats=bureaucrats + bureaucrats=bureaucrats, + thread_polls=thread_polls, + avail_tags=avail_tags, ) @forum_route("create_thread",methods=("GET","POST")) @@ -351,7 +393,7 @@ def search(): """, (query,)).fetchall() except OperationalError: flash('your search query was malformed.') - return redirect(url_for("forum.view_forum")) + return redirect(url_for("forum.not_actual_index")) display_thread_id = [ True ] * len(results) last_thread = None |