start porting some topics stuff
This commit is contained in:
104
app/models.py
104
app/models.py
@ -56,6 +56,110 @@ class Users(Model):
|
||||
class Topics(Model):
|
||||
table = "topics"
|
||||
|
||||
@classmethod
|
||||
def get_list(_cls):
|
||||
q = """
|
||||
SELECT
|
||||
topics.id, topics.name, topics.slug, topics.description, topics.is_locked,
|
||||
users.username AS latest_thread_username,
|
||||
threads.title AS latest_thread_title,
|
||||
threads.slug AS latest_thread_slug,
|
||||
threads.created_at AS latest_thread_created_at
|
||||
FROM
|
||||
topics
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
*,
|
||||
row_number() OVER (PARTITION BY threads.topic_id ORDER BY threads.created_at DESC) as rn
|
||||
FROM
|
||||
threads
|
||||
) threads ON threads.topic_id = topics.id AND threads.rn = 1
|
||||
LEFT JOIN
|
||||
users on users.id = threads.user_id
|
||||
ORDER BY
|
||||
topics.sort_order ASC"""
|
||||
return db.query(q)
|
||||
|
||||
@classmethod
|
||||
def get_active_threads(cls):
|
||||
q = """
|
||||
WITH ranked_threads AS (
|
||||
SELECT
|
||||
threads.topic_id, threads.id AS thread_id, threads.title AS thread_title, threads.slug AS thread_slug,
|
||||
posts.id AS post_id, posts.created_at AS post_created_at,
|
||||
users.username,
|
||||
ROW_NUMBER() OVER (PARTITION BY threads.topic_id ORDER BY posts.created_at DESC) AS rn
|
||||
FROM
|
||||
threads
|
||||
JOIN
|
||||
posts ON threads.id = posts.thread_id
|
||||
LEFT JOIN
|
||||
users ON posts.user_id = users.id
|
||||
)
|
||||
SELECT
|
||||
topic_id,
|
||||
thread_id, thread_title, thread_slug,
|
||||
post_id, post_created_at,
|
||||
username
|
||||
FROM
|
||||
ranked_threads
|
||||
WHERE
|
||||
rn = 1
|
||||
ORDER BY
|
||||
topic_id"""
|
||||
|
||||
active_threads_raw = db.query(q)
|
||||
active_threads = {}
|
||||
for thread in active_threads_raw:
|
||||
active_threads[int(thread['topic_id'])] = {
|
||||
'thread_title': thread['thread_title'],
|
||||
'thread_slug': thread['thread_slug'],
|
||||
'post_id': thread['post_id'],
|
||||
'username': thread['username'],
|
||||
'post_created_at': thread['post_created_at']
|
||||
}
|
||||
return active_threads
|
||||
|
||||
def get_threads(self, per_page, page, sort_by = "activity"):
|
||||
order_clause = ""
|
||||
if sort_by == "thread":
|
||||
order_clause = "ORDER BY threads.is_stickied DESC, threads.created_at DESC"
|
||||
else:
|
||||
order_clause = "ORDER BY threads.is_stickied DESC, latest_post_created_at DESC"
|
||||
|
||||
q = """
|
||||
SELECT
|
||||
threads.title, threads.slug, threads.created_at, threads.is_locked, threads.is_stickied,
|
||||
users.username AS started_by,
|
||||
u.username AS latest_post_username,
|
||||
ph.content AS latest_post_content,
|
||||
posts.created_at AS latest_post_created_at,
|
||||
posts.id AS latest_post_id
|
||||
FROM
|
||||
threads
|
||||
JOIN users ON users.id = threads.user_id
|
||||
JOIN (
|
||||
SELECT
|
||||
posts.thread_id,
|
||||
posts.id,
|
||||
posts.user_id,
|
||||
posts.created_at,
|
||||
posts.current_revision_id,
|
||||
ROW_NUMBER() OVER (PARTITION BY posts.thread_id ORDER BY posts.created_at DESC) AS rn
|
||||
FROM
|
||||
posts
|
||||
) posts ON posts.thread_id = threads.id AND posts.rn = 1
|
||||
JOIN
|
||||
post_history ph ON ph.id = posts.current_revision_id
|
||||
JOIN
|
||||
users u ON u.id = posts.user_id
|
||||
WHERE
|
||||
threads.topic_id = ?
|
||||
""" + order_clause + " LIMIT ? OFFSET ?"
|
||||
|
||||
return db.query(q, self.id, per_page, (page - 1) * per_page)
|
||||
|
||||
|
||||
class Threads(Model):
|
||||
table = "threads"
|
||||
|
||||
|
Reference in New Issue
Block a user