niming_backend/blueprints/article.py

120 lines
3.5 KiB
Python
Raw Normal View History

2024-11-13 03:23:11 +08:00
from flask import Blueprint, current_app, request, jsonify
import hashlib
import time
2024-11-13 21:20:21 +08:00
import logger
2024-11-13 03:23:11 +08:00
from sqlalchemy.orm import sessionmaker
from sqlalchemy import desc
2024-11-13 21:20:21 +08:00
import pgclass
2024-11-13 03:23:11 +08:00
"""
TODO:
- Image & Video
- IG post
"""
article = Blueprint('article', __name__)
# 匿名文列表
@article.route('/list', methods = ["GET"])
def listing():
# variables
rst = int(request.args.get("start"))
count = int(request.args.get("count"))
# db
db = current_app.shared_resource.engine
Session = sessionmaker(bind=db)
session = Session()
# get ctx
2024-11-13 21:20:21 +08:00
table = pgclass.SQLarticle
2024-11-13 03:23:11 +08:00
res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark).order_by(desc(table.id)).filter(table.mark == 'visible').offset(rst).limit(count).all()
# mapping
res = [ {"id":r[0], "ctx":r[1], "igid":r[2], "created_at":r[3], "mark":r[4]} for r in res ]
return jsonify(res)
# 獲取指定文章
@article.route("/get/<int:id>", methods = ["GET"])
def getarticle(id:int):
db = current_app.shared_resource.engine
Session = sessionmaker(bind=db)
session = Session()
# get ctx
2024-11-13 21:20:21 +08:00
table = pgclass.SQLarticle
2024-11-13 03:23:11 +08:00
res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark).filter(table.id == id).filter(table.mark == 'visible').all()
# mapping
res = [ {"id":r[0], "ctx":r[1], "igid":r[2], "created_at":r[3], "mark":r[4]} for r in res ]
return jsonify(res)
# 上傳文章
@article.route("/post", methods = ["POST"])
def posting():
db = current_app.shared_resource.engine
Session = sessionmaker(bind=db)
session = Session()
# content
ctx = str(request.json["ctx"])
2024-11-13 21:20:21 +08:00
if ctx is None: return "no content"
2024-11-13 03:23:11 +08:00
# hash
seed = ctx + str(time.time())
hash = hashlib.sha256(seed.encode()).hexdigest()
# file processing
# ig posting
igid = None
# mark
mark = "visible"
2024-11-13 21:20:21 +08:00
# posting
table = pgclass.SQLarticle
2024-11-13 03:23:11 +08:00
data = table(hash = hash, ctx = ctx, igid = igid, mark = mark)
session.add(data)
2024-11-13 21:20:21 +08:00
# commit
2024-11-13 03:23:11 +08:00
session.commit()
# pg getdata
2024-11-13 21:20:21 +08:00
res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark, table.hash).filter(table.hash == hash).all()
res = [ {"id":r[0], "ctx":r[1], "igid":r[2], "created_at":r[3], "mark":r[4], "hash":r[5]} for r in res ]
2024-11-13 03:23:11 +08:00
session.close()
2024-11-13 21:20:21 +08:00
# logger
logger.logger(db, "newpost", "Insert (id=%d): posts"%(res[0]["id"]))
2024-11-13 03:23:11 +08:00
return jsonify(res)
# 只有發文者可以看到的獲取指定文章
# 只有發文者可以做到的刪除文章
@article.route("/own/<sha256>", methods = ["GET", "DELETE"])
def owner_getarticle(sha256:str):
db = current_app.shared_resource.engine
Session = sessionmaker(bind=db)
session = Session()
2024-11-13 21:20:21 +08:00
table = pgclass.SQLarticle
2024-11-13 03:23:11 +08:00
# 獲取指定文章
if request.method == "GET":
res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark, table.hash).filter(table.hash == sha256).all()
res = [ {"id":r[0], "ctx":r[1], "igid":r[2], "created_at":r[3], "mark":r[4], "hash":r[5]} for r in res ]
return jsonify(res)
# 刪除指定文章
elif request.method == "DELETE":
2024-11-13 21:20:21 +08:00
# delete
2024-11-13 03:23:11 +08:00
res = session.query(table).filter(table.hash == sha256).first()
session.delete(res)
2024-11-13 21:20:21 +08:00
# commit
2024-11-13 03:23:11 +08:00
session.commit()
2024-11-13 21:20:21 +08:00
# logger
logger.logger(db, "delpost", "Delete (id=%d): posts"%(res.id))
2024-11-13 03:23:11 +08:00
return "OK", 200
session.close()