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