niming_backend/blueprints/article.py

73 lines
2.0 KiB
Python
Raw Normal View History

2024-11-07 02:19:53 +08:00
# /api/article
# 負責公開範圍內的資料上傳與存取
from flask import Blueprint, current_app, request
from supabase import Client
from hashlib import sha256
import time, math
article = Blueprint('article', __name__)
# listing - public
# /api/article/list?page=&count=
@article.route("/list", methods = ["GET"])
def list():
page = int(request.args.get("page"))
count = int(request.args.get("count"))
client: Client = current_app.shared_resource.client
response = client.table("niming_posts").select("id, content, igid, created_at").order("id", desc = True).range(start=page*count, end=(page+1)*count-1).execute()
return response.data
# fetching article - public
# /api/article/get?id=
@article.route("/get", methods = ["GET"])
def get():
id = str(int(request.args.get("id")))
client: Client = current_app.shared_resource.client
response = client.table("niming_posts").select("id, content, igid, created_at").eq("id", id).execute()
return response.data
# new post - public
@article.route("/post", methods = ["POST"])
def post():
# variables
ctx = request.form.get("ctx")
if ctx is None: return "No content"
else: ctx = str(ctx)
# future - images
# future - videos
# IG API Part
# hash
t = str(math.floor(time.time()*10000000))
hash = sha256( ( ctx + t ).encode() ).hexdigest()
# Supabase
client: Client = current_app.shared_resource.client
response = (client.table("niming_posts").insert({
"content": ctx,
"hash": hash
}).execute())
# log
return response.data
# 發文者專用 - 發文者有義務記住並保護好自己發的文章的sha256
# fetching article - article owner
@article.route("/owner/get", methods = ["GET"])
def owner_list():
hash = str(request.args.get("hash"))
client: Client = current_app.shared_resource.client
response = client.table("niming_posts").select("id, content, igid, created_at, hash").eq("hash", hash).execute()
return response.data
# delete article - article owner
# Coming Soon...