niming_backend/blueprints/admin/article.py
2024-12-22 18:03:55 +00:00

110 lines
3.4 KiB
Python

from flask import Blueprint, request, g, abort
from blueprints.admin.utils import check_key, role_required
from utils import pgclass, dbhelper, ighelper
from utils.misc import internal_json2protobuf
from protobuf_files import niming_pb2
# prefix: /admin
bl_admin_article = Blueprint("admin_article", __name__)
# 這裡要改
# get file
@bl_admin_article.route("/article/file/<fnhash>", methods = ["GET"])
@role_required(["article.read"])
def article_fileget(fnhash:str):
resp, code = dbhelper.solo_file_fetcher("admin", fnhash)
return resp, code
# list articles
@bl_admin_article.route('/article/list', methods = ["GET"])
@role_required(["article.read"])
def article_list():
res, code = dbhelper.multi_article_fetcher("admin", request.args.get("page"), 80)
return res, code
# get article / comment
@bl_admin_article.route("/<type>/<key>", methods=["GET"])
@role_required(["article.read"])
def article_read(type:str, key:str):
key = check_key(type, key)
if type == 'article':
type = 'a'
res, code = dbhelper.solo_article_fetcher("admin", key)
elif type == 'comment':
type = 'c'
res, code = dbhelper.solo_comment_fetcher("admin", key)
if code == 200:
return internal_json2protobuf(role="admin", otype=type, original=[res]), code
return abort(code)
# delete article / comment
@bl_admin_article.route("/<type>/<key>", methods=["DELETE"])
@role_required(["article.del"])
def article_del(type:str, key:str):
key = check_key(type, key)
opuser = g.opuser
if type == 'article':
rtype = niming_pb2.AdminFetchPostResponse
result, code = dbhelper.solo_article_remover(role="admin", id=key, opuser=opuser.user)
elif type == 'comment':
rtype = niming_pb2.AdminFetchCommentResponse
result, code = dbhelper.solo_comment_remover(role="admin", sha1=key, opuser=opuser.user)
if not code == 200: # Exception
return abort(code)
if type == 'article':
appobj = rtype.Message(id=result["id"], mark=result["mark"])
elif type == 'comment':
appobj = rtype.Message(sha1=result["sha1"], mark=result["mark"])
ret = rtype()
ret.posts.append(appobj)
return ret.SerializeToString(), 200
# pend article / comment
@bl_admin_article.route("/<type>/<key>", methods=["PUT"])
@role_required(["article.pend"])
def article_pend(type:str, key:str):
key = check_key(type, key)
# 找到本體
if type == 'article':
tg, code = dbhelper.solo_article_fetcher(role="admin", key=key)
elif type == 'comment':
tg, code = dbhelper.solo_comment_fetcher(role="admin", key=key)
if code != 200:
return abort(code)
# db
mark = pgclass.SQLmark
with dbhelper.db.getsession() as session:
res = session.query(mark).filter(mark.hash==tg["hash"]).first()
if res is None:
return abort(404)
# 如果文章已經公開
if res.mark == "visible":
return abort(400)
elif res.mark == "pending":
res.mark = "visible"
session.commit()
# run IG Post
if type == 'article':
result, err = ighelper.request_upload(tg["id"])
if err or result["result"] == "Canceled delete post request":
return abort(500)
return "OK", 200
else:
return abort(500)