Atualizar app/app.py
This commit is contained in:
+40
-14
@@ -395,29 +395,31 @@ google = oauth.register(
|
|||||||
client_kwargs={"scope": "openid email profile"},
|
client_kwargs={"scope": "openid email profile"},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def login_required(f):
|
def login_required(f):
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
# If using OAuth
|
key = "user" if app.config["AUTH_MODE"] == "oauth" else "db_user"
|
||||||
if app.config["AUTH_MODE"] == "oauth":
|
if key not in session:
|
||||||
if "user" not in session:
|
|
||||||
return redirect(url_for("login"))
|
|
||||||
return f(*args, **kwargs)
|
|
||||||
|
|
||||||
# If using DB authentication
|
|
||||||
if "db_user" not in session:
|
|
||||||
return redirect("/auth/login")
|
return redirect("/auth/login")
|
||||||
|
|
||||||
email = session["db_user"]["email"]
|
email = session[key]["email"]
|
||||||
|
db = SessionLocal()
|
||||||
u = db.query(User).filter_by(email=email).first()
|
u = db.query(User).filter_by(email=email).first()
|
||||||
|
db.close()
|
||||||
|
|
||||||
if not u:
|
if not u:
|
||||||
session.pop("db_user", None)
|
session.pop(key, None)
|
||||||
return redirect("/auth/login")
|
return redirect("/auth/login")
|
||||||
|
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/auth/login", methods=["GET", "POST"])
|
@app.route("/auth/login", methods=["GET", "POST"])
|
||||||
def db_login():
|
def db_login():
|
||||||
if request.method == "GET":
|
if request.method == "GET":
|
||||||
@@ -655,7 +657,10 @@ def promote(email):
|
|||||||
@admin_required
|
@admin_required
|
||||||
def admin_conversations(email):
|
def admin_conversations(email):
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
|
email = session["user"]["email"] if app.config["AUTH_MODE"] == "oauth" else session["db_user"]["email"]
|
||||||
convs = db.query(Conversation).filter_by(user_email=email).all()
|
convs = db.query(Conversation).filter_by(user_email=email).all()
|
||||||
|
if not convs:
|
||||||
|
return jsonify({"erro": "Conversas não encontradas."}), 404
|
||||||
data = [{"id": c.id, "title": c.title, "tags": c.tags} for c in convs]
|
data = [{"id": c.id, "title": c.title, "tags": c.tags} for c in convs]
|
||||||
db.close()
|
db.close()
|
||||||
return jsonify(data)
|
return jsonify(data)
|
||||||
@@ -689,7 +694,16 @@ def chat_stream():
|
|||||||
cid = request.args.get("conversation_id")
|
cid = request.args.get("conversation_id")
|
||||||
|
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
msgs = db.query(Message).filter_by(conversation_id=cid).order_by(Message.created_at).all()
|
email = session["user"]["email"] if app.config["AUTH_MODE"] == "oauth" else session["db_user"]["email"]
|
||||||
|
msgs = (
|
||||||
|
db.query(Message)
|
||||||
|
.join(Conversation, Message.conversation_id == Conversation.id)
|
||||||
|
.filter(Conversation.id == cid, Conversation.user_email == email)
|
||||||
|
.order_by(Message.created_at)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
chatml = [{"role": m.role, "content": m.content} for m in msgs]
|
chatml = [{"role": m.role, "content": m.content} for m in msgs]
|
||||||
@@ -741,7 +755,11 @@ def list_conversations():
|
|||||||
@rate_limited
|
@rate_limited
|
||||||
def export_md(cid):
|
def export_md(cid):
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
|
email = session["user"]["email"] if app.config["AUTH_MODE"] == "oauth" else session["db_user"]["email"]
|
||||||
conv = db.query(Conversation).filter_by(id=cid, user_email=email).first()
|
conv = db.query(Conversation).filter_by(id=cid, user_email=email).first()
|
||||||
|
if not conv:
|
||||||
|
return jsonify({"erro": "Conversa não encontrada."}), 404
|
||||||
|
|
||||||
msgs = conv.messages
|
msgs = conv.messages
|
||||||
|
|
||||||
md = f"# {conv.title}\n\n"
|
md = f"# {conv.title}\n\n"
|
||||||
@@ -804,7 +822,10 @@ def import_file(cid):
|
|||||||
@rate_limited
|
@rate_limited
|
||||||
def export_pdf(cid):
|
def export_pdf(cid):
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
|
email = session["user"]["email"] if app.config["AUTH_MODE"] == "oauth" else session["db_user"]["email"]
|
||||||
conv = db.query(Conversation).filter_by(id=cid, user_email=email).first()
|
conv = db.query(Conversation).filter_by(id=cid, user_email=email).first()
|
||||||
|
if not conv:
|
||||||
|
return jsonify({"erro": "Conversa não encontrada."}), 404
|
||||||
msgs = conv.messages
|
msgs = conv.messages
|
||||||
|
|
||||||
html = "<h1>{}</h1>".format(conv.title)
|
html = "<h1>{}</h1>".format(conv.title)
|
||||||
@@ -936,7 +957,11 @@ def stream():
|
|||||||
def share(cid):
|
def share(cid):
|
||||||
target_email = request.json.get("email")
|
target_email = request.json.get("email")
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
|
email = session["user"]["email"] if app.config["AUTH_MODE"] == "oauth" else session["db_user"]["email"]
|
||||||
conv = db.query(Conversation).filter_by(id=cid, user_email=email).first()
|
conv = db.query(Conversation).filter_by(id=cid, user_email=email).first()
|
||||||
|
if not conv:
|
||||||
|
return jsonify({"erro": "Conversa não encontrada."}), 404
|
||||||
|
|
||||||
conv.shared = target_email
|
conv.shared = target_email
|
||||||
db.commit()
|
db.commit()
|
||||||
db.close()
|
db.close()
|
||||||
@@ -949,7 +974,11 @@ def share(cid):
|
|||||||
def update_tags(cid):
|
def update_tags(cid):
|
||||||
tags = request.json.get("tags", [])
|
tags = request.json.get("tags", [])
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
|
email = session["user"]["email"] if app.config["AUTH_MODE"] == "oauth" else session["db_user"]["email"]
|
||||||
conv = db.query(Conversation).filter_by(id=cid, user_email=email).first()
|
conv = db.query(Conversation).filter_by(id=cid, user_email=email).first()
|
||||||
|
if not conv:
|
||||||
|
return jsonify({"erro": "Conversa não encontrada."}), 404
|
||||||
|
|
||||||
conv.tags = ",".join(tags)
|
conv.tags = ",".join(tags)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.close()
|
db.close()
|
||||||
@@ -962,10 +991,8 @@ def update_tags(cid):
|
|||||||
def search():
|
def search():
|
||||||
q = request.args.get("q", "").lower()
|
q = request.args.get("q", "").lower()
|
||||||
user_email = session["user"]["email"]
|
user_email = session["user"]["email"]
|
||||||
|
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
convs = db.query(Conversation).filter_by(user_email=user_email).all()
|
convs = db.query(Conversation).filter_by(user_email=user_email).all()
|
||||||
|
|
||||||
results = []
|
results = []
|
||||||
for c in convs:
|
for c in convs:
|
||||||
for m in c.messages:
|
for m in c.messages:
|
||||||
@@ -977,7 +1004,6 @@ def search():
|
|||||||
"content": m.content,
|
"content": m.content,
|
||||||
"created_at": m.created_at.isoformat()
|
"created_at": m.created_at.isoformat()
|
||||||
})
|
})
|
||||||
|
|
||||||
db.close()
|
db.close()
|
||||||
return jsonify(results)
|
return jsonify(results)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user