init
This commit is contained in:
commit
6dcb020bb5
16 files changed
+488
No files matched your search
@@ -0,0 +1,14 @@
|
||||
import flask
|
||||
|
||||
from . import user_api
|
||||
from . import forum_api
|
||||
from . import thread_api
|
||||
|
||||
api = flask.Blueprint(
|
||||
name='api',
|
||||
import_name=__name__,
|
||||
url_prefix='/api/'
|
||||
)
|
||||
api.register_blueprint(user_api.user)
|
||||
api.register_blueprint(forum_api.forum)
|
||||
api.register_blueprint(thread_api.thread)
|
||||
@@ -0,0 +1,47 @@
|
||||
import time
|
||||
|
||||
import flask
|
||||
import flask_login
|
||||
|
||||
from . import helpers
|
||||
import database
|
||||
|
||||
|
||||
forum = flask.Blueprint(
|
||||
name='forum_api',
|
||||
import_name=__name__,
|
||||
url_prefix='/forum/'
|
||||
)
|
||||
|
||||
|
||||
@forum.route('/forum/send_message', methods=['POST'])
|
||||
@flask_login.login_required
|
||||
def send_message() -> tuple[str, int]:
|
||||
|
||||
data = flask.request.json
|
||||
|
||||
if not data:
|
||||
return '', 400
|
||||
|
||||
if any([not p in data for p in ['content', 'thread']]):
|
||||
return '', 400
|
||||
|
||||
new_message = database.MessageORM(
|
||||
user=flask_login.current_user.get_id(),
|
||||
content=data['content'],
|
||||
thread=data['thread'],
|
||||
time=int(time.time())
|
||||
)
|
||||
|
||||
if not helpers.user_in_thread(flask_login.current_user.get_id(), data['thread']):
|
||||
return '', 401
|
||||
|
||||
database.insert_message(new_message)
|
||||
|
||||
return '', 201
|
||||
|
||||
@forum.route('/forum/get_threads')
|
||||
@flask_login.login_required
|
||||
def get_forum_threads() -> tuple[str, int]:
|
||||
return '', 200
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import logging
|
||||
|
||||
import database
|
||||
|
||||
def user_in_thread(user_id: int, thread_id: int) -> bool:
|
||||
forums = database.get_user_forums(user_id)
|
||||
|
||||
if not forums:
|
||||
return False
|
||||
|
||||
for forum in forums:
|
||||
if not forum.id:
|
||||
logging.error(f'Forum {forum.name} has no id but user is in it?')
|
||||
continue
|
||||
|
||||
threads = database.get_forum_threads(forum.id)
|
||||
|
||||
for thread in threads:
|
||||
if not thread.id:
|
||||
logging.error(f'Thread {thread.name} has no id?')
|
||||
continue
|
||||
|
||||
if thread.id == thread_id:
|
||||
return True
|
||||
|
||||
return False
|
||||
@@ -0,0 +1,24 @@
|
||||
import dataclasses
|
||||
|
||||
import flask
|
||||
import flask_login
|
||||
|
||||
import database
|
||||
|
||||
|
||||
thread = flask.Blueprint(
|
||||
name='thread_api',
|
||||
import_name=__name__,
|
||||
url_prefix='/thread/'
|
||||
)
|
||||
|
||||
@thread.route('/get_messages')
|
||||
@flask_login.login_required
|
||||
def get_thread_messages() -> tuple[flask.Response, int]:
|
||||
thread_id = flask.request.args.get("thread", type=int)
|
||||
if thread_id is None:
|
||||
return flask.jsonify({"error": "Missing thread parameter"}), 400
|
||||
|
||||
messages = database.get_messages_by_thread(thread_id)
|
||||
messages_dicts = {"messages" : [dataclasses.asdict(msg) for msg in messages]}
|
||||
return flask.jsonify(messages_dicts), 200
|
||||
@@ -0,0 +1,25 @@
|
||||
import dataclasses
|
||||
|
||||
import flask
|
||||
import flask_login
|
||||
|
||||
import mgU
|
||||
import database
|
||||
|
||||
|
||||
user = flask.Blueprint(
|
||||
name='user_api',
|
||||
import_name=__name__,
|
||||
url_prefix='/user/'
|
||||
)
|
||||
|
||||
|
||||
@user.route('/get_forums')
|
||||
@flask_login.login_required
|
||||
def get_user_forums() -> tuple[flask.Response, int]:
|
||||
user_id: str = flask_login.current_user.get_id()
|
||||
|
||||
forums: list[mgU.Forum] = database.get_user_forums(int(user_id))
|
||||
forums_dict = {"forums" : [dataclasses.asdict(forum) for forum in forums]}
|
||||
|
||||
return flask.jsonify(forums_dict), 200
|
||||
Reference in new issue
Block a user