57 lines
2.0 KiB
Python
57 lines
2.0 KiB
Python
import sqlite3
|
|
|
|
PREFIX = "smf_"
|
|
GET_BOARDS = "SELECT * FROM `{}boards`".format(PREFIX)
|
|
GET_CATEGORIES = "SELECT * FROM `{}categories`".format(PREFIX)
|
|
GET_THREADS = """
|
|
SELECT * FROM `{}topics` AS `topics`, `{}messages` AS `messages`
|
|
WHERE `topics`.`id_board`=? AND `messages`.`id_msg`=`topics`.`id_first_msg`
|
|
ORDER BY `id_last_msg` DESC
|
|
LIMIT ? OFFSET ?
|
|
""".format(PREFIX, PREFIX)
|
|
GET_POSTS = """
|
|
SELECT * FROM `{}messages`
|
|
WHERE `id_topic`=?
|
|
ORDER BY `poster_time` ASC
|
|
LIMIT ? OFFSET ?
|
|
""".format(PREFIX)
|
|
|
|
class Forum():
|
|
def __init__ (self, db_path):
|
|
self.connection = sqlite3.connect(db_path)
|
|
self.connection.row_factory = sqlite3.Row
|
|
|
|
def get_board_tree (self):
|
|
categories = [dict(category) for category in self.get_categories()]
|
|
boards = [dict(board) for board in self.get_boards()]
|
|
for category in categories:
|
|
category['children'] = [board for board in boards if board['id_cat'] == category['id_cat']]
|
|
for board in boards:
|
|
board['children'] = [board for board in boards if board['id_parent'] == board['id_board']]
|
|
return categories
|
|
|
|
def get_categories (self):
|
|
cursor = self.connection.cursor()
|
|
cursor.execute(GET_CATEGORIES)
|
|
return cursor.fetchall()
|
|
|
|
def get_boards (self):
|
|
cursor = self.connection.cursor()
|
|
cursor.execute(GET_BOARDS)
|
|
return cursor.fetchall()
|
|
|
|
def get_threads_in_board (self, board, page=0, per_page=2000):
|
|
try:
|
|
board = board['id_board']
|
|
except ValueError: pass
|
|
cursor = self.connection.cursor()
|
|
cursor.execute(GET_THREADS, (board, per_page, page * per_page))
|
|
return cursor.fetchall()
|
|
|
|
def get_posts_in_thread (self, thread, page=0, per_page=15):
|
|
try:
|
|
thread = thread['id_topic']
|
|
except ValueError: pass
|
|
cursor = self.connection.cursor()
|
|
cursor.execute(GET_POSTS, (thread, per_page, page * per_page))
|
|
return cursor.fetchall() |