fbk so cute! so i add it to index!

This commit is contained in:
pictures2333 2024-11-13 13:20:21 +00:00
parent dc98b7491f
commit 272f634de4
4 changed files with 57 additions and 14 deletions

5
app.py
View File

@ -3,7 +3,7 @@ from flask import Flask
# load_dotenv() # load_dotenv()
import os import os
from sqlalchemy import create_engine from sqlalchemy import create_engine
from pgclass import Base, SQLarticle from pgclass import Base
# blueprints # blueprints
from blueprints.article import article from blueprints.article import article
@ -23,7 +23,6 @@ Base.metadata.create_all(engine)
class shared(): class shared():
def __init__(self, engine): def __init__(self, engine):
self.engine = engine self.engine = engine
self.SQLarticle = SQLarticle
sh = shared(engine) sh = shared(engine)
# flask app # flask app
@ -37,7 +36,7 @@ app.register_blueprint(article, url_prefix = "/article")
# index # index
@app.route("/", methods = ["GET", "POST"]) @app.route("/", methods = ["GET", "POST"])
def index(): def index():
return "Hello, World!" return "Hello! World!<br>Shirakami Fubuki: cutest fox!!!"
# app run # app run
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -1,14 +1,15 @@
from flask import Blueprint, current_app, request, jsonify from flask import Blueprint, current_app, request, jsonify
import hashlib import hashlib
import time import time
import logger
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from sqlalchemy import desc from sqlalchemy import desc
import pgclass
""" """
TODO: TODO:
- Image & Video - Image & Video
- IG post - IG post
- LOG
""" """
article = Blueprint('article', __name__) article = Blueprint('article', __name__)
@ -26,7 +27,7 @@ def listing():
session = Session() session = Session()
# get ctx # get ctx
table = current_app.shared_resource.SQLarticle table = pgclass.SQLarticle
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() 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 # mapping
@ -42,7 +43,7 @@ def getarticle(id:int):
session = Session() session = Session()
# get ctx # get ctx
table = current_app.shared_resource.SQLarticle table = pgclass.SQLarticle
res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark).filter(table.id == id).filter(table.mark == 'visible').all() res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark).filter(table.id == id).filter(table.mark == 'visible').all()
# mapping # mapping
@ -59,7 +60,7 @@ def posting():
# content # content
ctx = str(request.json["ctx"]) ctx = str(request.json["ctx"])
if ctx is None: return "No brain no content" if ctx is None: return "no content"
# hash # hash
seed = ctx + str(time.time()) seed = ctx + str(time.time())
@ -73,17 +74,22 @@ def posting():
# mark # mark
mark = "visible" mark = "visible"
# pg commit # posting
table = current_app.shared_resource.SQLarticle table = pgclass.SQLarticle
data = table(hash = hash, ctx = ctx, igid = igid, mark = mark) data = table(hash = hash, ctx = ctx, igid = igid, mark = mark)
session.add(data) session.add(data)
# commit
session.commit() session.commit()
# pg getdata # pg getdata
res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark).filter(table.hash == hash).all() 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]} for r in res ] res = [ {"id":r[0], "ctx":r[1], "igid":r[2], "created_at":r[3], "mark":r[4], "hash":r[5]} for r in res ]
session.close() session.close()
# logger
logger.logger(db, "newpost", "Insert (id=%d): posts"%(res[0]["id"]))
return jsonify(res) return jsonify(res)
# 只有發文者可以看到的獲取指定文章 # 只有發文者可以看到的獲取指定文章
@ -93,7 +99,7 @@ def owner_getarticle(sha256:str):
db = current_app.shared_resource.engine db = current_app.shared_resource.engine
Session = sessionmaker(bind=db) Session = sessionmaker(bind=db)
session = Session() session = Session()
table = current_app.shared_resource.SQLarticle table = pgclass.SQLarticle
# 獲取指定文章 # 獲取指定文章
if request.method == "GET": if request.method == "GET":
@ -102,9 +108,13 @@ def owner_getarticle(sha256:str):
return jsonify(res) return jsonify(res)
# 刪除指定文章 # 刪除指定文章
elif request.method == "DELETE": elif request.method == "DELETE":
# delete
res = session.query(table).filter(table.hash == sha256).first() res = session.query(table).filter(table.hash == sha256).first()
session.delete(res) session.delete(res)
# commit
session.commit() session.commit()
# logger
logger.logger(db, "delpost", "Delete (id=%d): posts"%(res.id))
return "OK", 200 return "OK", 200
session.close() session.close()

23
logger.py Normal file
View File

@ -0,0 +1,23 @@
import pgclass
from sqlalchemy.orm import sessionmaker
def logger(engine, type, message):
Session = sessionmaker(bind=engine)
session = Session()
table = pgclass.SQLlog
flag = False
# new post & del post
if type == "newpost" or type == "delpost":
flag = True
log = table(source = "general", message = message)
# session.add
if flag:
session.add(log)
session.commit()
# session.close
session.close()

View File

@ -15,3 +15,14 @@ class SQLarticle(Base):
def __repr__(self): def __repr__(self):
return f"<article(id={self.id}, hash={self.hash}, ctx={self.ctx}, igid={self.igid}, mark={self.mark}, created_at={self.created_at})>" return f"<article(id={self.id}, hash={self.hash}, ctx={self.ctx}, igid={self.igid}, mark={self.mark}, created_at={self.created_at})>"
class SQLlog(Base):
__tablename__ = 'logs'
id = Column(Integer, primary_key=True)
created_at = Column(TIMESTAMP(timezone=True), server_default=func.now())
message = Column(String)
source = Column(String)
def __repr__(self):
return f"<article(id={self.id}, created_at={self.created_at}, message={self.message}, source={self.source})>"