From 272f634de4d4070935b136dd66965d3d6625f652 Mon Sep 17 00:00:00 2001 From: pictures2333 Date: Wed, 13 Nov 2024 13:20:21 +0000 Subject: [PATCH] fbk so cute! so i add it to index! --- app.py | 5 ++--- blueprints/article.py | 30 ++++++++++++++++++++---------- logger.py | 23 +++++++++++++++++++++++ pgclass.py | 13 ++++++++++++- 4 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 logger.py diff --git a/app.py b/app.py index 611d1e2..1adace4 100644 --- a/app.py +++ b/app.py @@ -3,7 +3,7 @@ from flask import Flask # load_dotenv() import os from sqlalchemy import create_engine -from pgclass import Base, SQLarticle +from pgclass import Base # blueprints from blueprints.article import article @@ -23,7 +23,6 @@ Base.metadata.create_all(engine) class shared(): def __init__(self, engine): self.engine = engine - self.SQLarticle = SQLarticle sh = shared(engine) # flask app @@ -37,7 +36,7 @@ app.register_blueprint(article, url_prefix = "/article") # index @app.route("/", methods = ["GET", "POST"]) def index(): - return "Hello, World!" + return "Hello! World!
Shirakami Fubuki: cutest fox!!!" # app run if __name__ == "__main__": diff --git a/blueprints/article.py b/blueprints/article.py index 880885c..f350fc5 100644 --- a/blueprints/article.py +++ b/blueprints/article.py @@ -1,14 +1,15 @@ from flask import Blueprint, current_app, request, jsonify import hashlib import time +import logger from sqlalchemy.orm import sessionmaker from sqlalchemy import desc +import pgclass """ TODO: - Image & Video - IG post -- LOG """ article = Blueprint('article', __name__) @@ -26,7 +27,7 @@ def listing(): session = Session() # 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() # mapping @@ -42,7 +43,7 @@ def getarticle(id:int): session = Session() # 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() # mapping @@ -59,7 +60,7 @@ def posting(): # content ctx = str(request.json["ctx"]) - if ctx is None: return "No brain no content" + if ctx is None: return "no content" # hash seed = ctx + str(time.time()) @@ -73,17 +74,22 @@ def posting(): # mark mark = "visible" - # pg commit - table = current_app.shared_resource.SQLarticle + # posting + table = pgclass.SQLarticle data = table(hash = hash, ctx = ctx, igid = igid, mark = mark) session.add(data) + + # commit session.commit() # pg getdata - res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark).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 = 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 ] session.close() + + # logger + logger.logger(db, "newpost", "Insert (id=%d): posts"%(res[0]["id"])) + return jsonify(res) # 只有發文者可以看到的獲取指定文章 @@ -93,7 +99,7 @@ def owner_getarticle(sha256:str): db = current_app.shared_resource.engine Session = sessionmaker(bind=db) session = Session() - table = current_app.shared_resource.SQLarticle + table = pgclass.SQLarticle # 獲取指定文章 if request.method == "GET": @@ -102,9 +108,13 @@ def owner_getarticle(sha256:str): return jsonify(res) # 刪除指定文章 elif request.method == "DELETE": + # delete res = session.query(table).filter(table.hash == sha256).first() session.delete(res) + # commit session.commit() + # logger + logger.logger(db, "delpost", "Delete (id=%d): posts"%(res.id)) return "OK", 200 session.close() \ No newline at end of file diff --git a/logger.py b/logger.py new file mode 100644 index 0000000..6bd89e0 --- /dev/null +++ b/logger.py @@ -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() + + \ No newline at end of file diff --git a/pgclass.py b/pgclass.py index 902f21a..cb26079 100644 --- a/pgclass.py +++ b/pgclass.py @@ -14,4 +14,15 @@ class SQLarticle(Base): mark = Column(String) def __repr__(self): - return f"" \ No newline at end of file + return f"" + +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"" \ No newline at end of file