59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
from flask import Blueprint, request, abort
|
|
|
|
from utils import dbhelper
|
|
from utils.misc import internal_json2protobuf
|
|
from protobuf_files import niming_pb2
|
|
|
|
# prefix: /owner/
|
|
bl_owner_article = Blueprint('owner_article', __name__)
|
|
|
|
|
|
# 只有發文者可以看到的獲取指定文章
|
|
# 只有發文者可以做到的刪除文章
|
|
@bl_owner_article.route("/<type>/<key>", methods = ["GET", "DELETE"])
|
|
def owner_getarticle(type:str, key:str):
|
|
# arguments
|
|
sha256 = request.args.get("hash", None)
|
|
if not sha256:
|
|
return abort(400)
|
|
sha256 = str(sha256)
|
|
|
|
if type == 'article':
|
|
if not (len(key) > 0 and key.isdigit()):
|
|
return abort(400)
|
|
type, key = 'a', int(key) # id
|
|
elif type == 'comment':
|
|
if not (len(key) > 0):
|
|
return abort(400)
|
|
type, key = 'c', str(key) # sha1
|
|
else:
|
|
return abort(404)
|
|
|
|
# 獲取指定文章/留言
|
|
if request.method == "GET":
|
|
if type == 'a': # 文章
|
|
resfn, code = dbhelper.solo_article_fetcher("owner", key=key, hash=sha256)
|
|
elif type == 'c': # 留言
|
|
resfn, code = dbhelper.solo_comment_fetcher("owner", key=key, hash=sha256)
|
|
if code == 200:
|
|
return internal_json2protobuf(role="owner", otype=type, original=[resfn]), code
|
|
return abort(code)
|
|
# 刪除指定文章/留言
|
|
elif request.method == "DELETE":
|
|
if type == 'a':
|
|
rtype = niming_pb2.FetchPostResponse
|
|
result, code = dbhelper.solo_article_remover("owner", hash=sha256, id=key)
|
|
elif type == 'c':
|
|
rtype = niming_pb2.FetchCommentResponse
|
|
result, code = dbhelper.solo_comment_remover("owner", hash=sha256, sha1=key)
|
|
|
|
if not code == 200: # Exception
|
|
return abort(code)
|
|
|
|
if type == 'a':
|
|
ret = rtype(posts=[ rtype.Message(id=result["id"]) ])
|
|
elif type == 'c':
|
|
ret = rtype(posts=[ rtype.Message(sha1=result["sha1"]) ])
|
|
|
|
return ret.SerializeToString(), 200
|