added get threads endpoint
This commit is contained in:
1 parent
af7d682a38
commit
c9cf3412d8
5 files changed
+36
-2
No files matched your search
@@ -1 +1,3 @@
|
|||||||
__pycache__
|
__pycache__
|
||||||
|
|
||||||
|
*.db
|
||||||
+25
-2
@@ -1,10 +1,12 @@
|
|||||||
import time
|
import time
|
||||||
|
import dataclasses
|
||||||
|
|
||||||
import flask
|
import flask
|
||||||
import flask_login
|
import flask_login
|
||||||
|
|
||||||
from . import helpers
|
from . import helpers
|
||||||
import database
|
import database
|
||||||
|
import mgU
|
||||||
|
|
||||||
|
|
||||||
forum = flask.Blueprint(
|
forum = flask.Blueprint(
|
||||||
@@ -42,6 +44,27 @@ def send_message() -> tuple[str, int]:
|
|||||||
|
|
||||||
@forum.route('/forum/get_threads')
|
@forum.route('/forum/get_threads')
|
||||||
@flask_login.login_required
|
@flask_login.login_required
|
||||||
def get_forum_threads() -> tuple[str, int]:
|
def get_forum_threads() -> tuple[flask.Response, int]:
|
||||||
return '', 200
|
"""
|
||||||
|
Get all threads from forums the current user has access to.
|
||||||
|
"""
|
||||||
|
user_id: str = flask_login.current_user.get_id()
|
||||||
|
|
||||||
|
# Get all forums the user has access to
|
||||||
|
user_forums: list[mgU.Forum] = database.get_user_forums(int(user_id))
|
||||||
|
|
||||||
|
if not user_forums:
|
||||||
|
return flask.jsonify({"threads": []}), 200
|
||||||
|
|
||||||
|
# Get all threads from all user's forums
|
||||||
|
all_threads: list[mgU.Thread] = []
|
||||||
|
for forum in user_forums:
|
||||||
|
if forum.id: # Make sure forum has an ID
|
||||||
|
forum_threads = database.get_forum_threads(forum.id)
|
||||||
|
all_threads.extend(forum_threads)
|
||||||
|
|
||||||
|
# Convert threads to dictionary format
|
||||||
|
threads_dict = {"threads": [dataclasses.asdict(thread) for thread in all_threads]}
|
||||||
|
|
||||||
|
return flask.jsonify(threads_dict), 200
|
||||||
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"threads": [
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"name": "Thread Name",
|
||||||
|
"forum": 1
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
File renamed without changes.
File renamed without changes.
Reference in new issue
Block a user