From dc98b7491feaf8bc8632874ec9df59525c2207f7 Mon Sep 17 00:00:00 2001 From: pictures2333 Date: Tue, 12 Nov 2024 19:23:11 +0000 Subject: [PATCH] dev box & FBK so cutegit add . --- .gitignore | 4 +- app.py | 86 ++++++++--------- blueprints/article.py | 218 +++++++++++++++++++++--------------------- pgclass.py | 32 +++---- requirements.txt | 6 +- 5 files changed, 173 insertions(+), 173 deletions(-) diff --git a/.gitignore b/.gitignore index 6d17870..7a1ec96 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -__pycache__ -.env \ No newline at end of file +__pycache__ +*.env \ No newline at end of file diff --git a/app.py b/app.py index d41add8..611d1e2 100644 --- a/app.py +++ b/app.py @@ -1,44 +1,44 @@ -from flask import Flask -from dotenv import load_dotenv -load_dotenv() -import os -from sqlalchemy import create_engine -from pgclass import Base, SQLarticle -# blueprints -from blueprints.article import article - -# Global Variables -PG_HOST = os.getenv("PG_HOST") -PG_PORT = os.getenv("PG_PORT") -PG_NAME = os.getenv("PG_NAME") -PG_USER = os.getenv("PG_USER") -PG_PASS = os.getenv("PG_PASS") -JWT_KEY = os.getenv("JWT_KEY") - -# Postgresql -engine = create_engine('postgresql+psycopg2://%s:%s@%s:%s/%s'%(PG_USER, PG_PASS, PG_HOST, PG_PORT, PG_NAME)) -Base.metadata.create_all(engine) - -# shared class -class shared(): - def __init__(self, engine): - self.engine = engine - self.SQLarticle = SQLarticle -sh = shared(engine) - -# flask app -app = Flask(__name__) -app.config["SECRET_KEY"] = os.urandom(64) -app.shared_resource = sh - -# register blueprints -app.register_blueprint(article, url_prefix = "/article") - -# index -@app.route("/", methods = ["GET", "POST"]) -def index(): - return "Hello, World!" - -# app run -if __name__ == "__main__": +from flask import Flask +# from dotenv import load_dotenv +# load_dotenv() +import os +from sqlalchemy import create_engine +from pgclass import Base, SQLarticle +# blueprints +from blueprints.article import article + +# Global Variables +PG_HOST = os.getenv("PG_HOST") +PG_PORT = os.getenv("PG_PORT") +PG_NAME = os.getenv("PG_NAME") +PG_USER = os.getenv("PG_USER") +PG_PASS = os.getenv("PG_PASS") +JWT_KEY = os.getenv("JWT_KEY") + +# Postgresql +engine = create_engine('postgresql+psycopg2://%s:%s@%s:%s/%s'%(PG_USER, PG_PASS, PG_HOST, PG_PORT, PG_NAME)) +Base.metadata.create_all(engine) + +# shared class +class shared(): + def __init__(self, engine): + self.engine = engine + self.SQLarticle = SQLarticle +sh = shared(engine) + +# flask app +app = Flask(__name__) +app.config["SECRET_KEY"] = os.urandom(64) +app.shared_resource = sh + +# register blueprints +app.register_blueprint(article, url_prefix = "/article") + +# index +@app.route("/", methods = ["GET", "POST"]) +def index(): + return "Hello, World!" + +# app run +if __name__ == "__main__": app.run(host="0.0.0.0", port=5000, debug=False) \ No newline at end of file diff --git a/blueprints/article.py b/blueprints/article.py index 4d3a8fe..880885c 100644 --- a/blueprints/article.py +++ b/blueprints/article.py @@ -1,110 +1,110 @@ -from flask import Blueprint, current_app, request, jsonify -import hashlib -import time -from sqlalchemy.orm import sessionmaker -from sqlalchemy import desc - -""" -TODO: -- Image & Video -- IG post -- LOG -""" - -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 - table = current_app.shared_resource.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 - 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/", methods = ["GET"]) -def getarticle(id:int): - db = current_app.shared_resource.engine - Session = sessionmaker(bind=db) - session = Session() - - # get ctx - table = current_app.shared_resource.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 - 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"]) - if ctx is None: return "No brain no content" - - # hash - seed = ctx + str(time.time()) - hash = hashlib.sha256(seed.encode()).hexdigest() - - # file processing - - # ig posting - igid = None - - # mark - mark = "visible" - - # pg commit - table = current_app.shared_resource.SQLarticle - data = table(hash = hash, ctx = ctx, igid = igid, mark = mark) - session.add(data) - 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 ] - - session.close() - return jsonify(res) - -# 只有發文者可以看到的獲取指定文章 -# 只有發文者可以做到的刪除文章 -@article.route("/own/", methods = ["GET", "DELETE"]) -def owner_getarticle(sha256:str): - db = current_app.shared_resource.engine - Session = sessionmaker(bind=db) - session = Session() - table = current_app.shared_resource.SQLarticle - - # 獲取指定文章 - 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": - res = session.query(table).filter(table.hash == sha256).first() - session.delete(res) - session.commit() - return "OK", 200 - +from flask import Blueprint, current_app, request, jsonify +import hashlib +import time +from sqlalchemy.orm import sessionmaker +from sqlalchemy import desc + +""" +TODO: +- Image & Video +- IG post +- LOG +""" + +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 + table = current_app.shared_resource.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 + 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/", methods = ["GET"]) +def getarticle(id:int): + db = current_app.shared_resource.engine + Session = sessionmaker(bind=db) + session = Session() + + # get ctx + table = current_app.shared_resource.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 + 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"]) + if ctx is None: return "No brain no content" + + # hash + seed = ctx + str(time.time()) + hash = hashlib.sha256(seed.encode()).hexdigest() + + # file processing + + # ig posting + igid = None + + # mark + mark = "visible" + + # pg commit + table = current_app.shared_resource.SQLarticle + data = table(hash = hash, ctx = ctx, igid = igid, mark = mark) + session.add(data) + 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 ] + + session.close() + return jsonify(res) + +# 只有發文者可以看到的獲取指定文章 +# 只有發文者可以做到的刪除文章 +@article.route("/own/", methods = ["GET", "DELETE"]) +def owner_getarticle(sha256:str): + db = current_app.shared_resource.engine + Session = sessionmaker(bind=db) + session = Session() + table = current_app.shared_resource.SQLarticle + + # 獲取指定文章 + 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": + res = session.query(table).filter(table.hash == sha256).first() + session.delete(res) + session.commit() + return "OK", 200 + session.close() \ No newline at end of file diff --git a/pgclass.py b/pgclass.py index d54d911..902f21a 100644 --- a/pgclass.py +++ b/pgclass.py @@ -1,17 +1,17 @@ -from sqlalchemy import Column, Integer, String, TIMESTAMP, func -from sqlalchemy.ext.declarative import declarative_base - -Base = declarative_base() - -class SQLarticle(Base): - __tablename__ = 'posts' - - id = Column(Integer, primary_key=True) - created_at = Column(TIMESTAMP(timezone=True), server_default=func.now()) - hash = Column(String) - ctx = Column(String) - igid = Column(String) - mark = Column(String) - - def __repr__(self): +from sqlalchemy import Column, Integer, String, TIMESTAMP, func +from sqlalchemy.ext.declarative import declarative_base + +Base = declarative_base() + +class SQLarticle(Base): + __tablename__ = 'posts' + + id = Column(Integer, primary_key=True) + created_at = Column(TIMESTAMP(timezone=True), server_default=func.now()) + hash = Column(String) + ctx = Column(String) + igid = Column(String) + mark = Column(String) + + def __repr__(self): return f"" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 90de530..eade94c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -sqlalchemy -flask -pyjwt +sqlalchemy +flask +pyjwt psycopg2 \ No newline at end of file