From 18f946b7065a3ecacf09766c5eb458a1df439acb Mon Sep 17 00:00:00 2001 From: SouthFox Date: Wed, 24 May 2023 14:05:05 +0800 Subject: [PATCH] [fix] fix secret path --- BDSM/toot.py | 2 +- BDSM/views.py | 44 ++++++++++++++++++++++---------------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/BDSM/toot.py b/BDSM/toot.py index a41f301..1ede998 100644 --- a/BDSM/toot.py +++ b/BDSM/toot.py @@ -22,7 +22,7 @@ def app_register(url): def app_login(url): mastodon = Mastodon( client_id='data/pyBDSM_clientcred.secret', - access_token='user.secret', + access_token='data/user.secret', api_base_url=url ) diff --git a/BDSM/views.py b/BDSM/views.py index fae5df5..45d693c 100644 --- a/BDSM/views.py +++ b/BDSM/views.py @@ -12,6 +12,7 @@ from BDSM.toot import app_login, app_register, archive_toot, get_context from mastodon import Mastodon from types import SimpleNamespace from datetime import timezone +from typing import Any @@ -157,47 +158,46 @@ def settings(): return redirect(url_for('settings')) settings = Settings.query.first() - app_init = os.path.isfile('pyBDSM_clientcred.secret') and os.path.isfile('user.secret') + app_init = os.path.isfile("data/pyBDSM_clientcred.secret") and os.path.isfile("data/user.secret") if settings == None: flash('请输入相关设置!') return render_template('settings.html',settings=settings, app_init=app_init) -@app.route('/register', methods=['GET', 'POST']) -def register(): +@app.route("/register", methods=["GET", "POST"]) +def register() -> Any: settings = Settings.query.first() if settings == None: flash('请先输入站点地址!') return redirect(url_for('settings')) else: + if os.path.isfile("data/user.secret"): + flash('应用已授权!') + return redirect(url_for("settings")) + domain = settings.domain url = "https://" + domain + if not os.path.isfile("data/pyBDSM_clientcred.secret"): + app_register(url) + + if request.method == 'POST': + token = request.form['token'].rstrip() + mastodon = Mastodon(client_id="data/pyBDSM_clientcred.secret", api_base_url=url) + print("test") + mastodon.log_in(code=token, to_file="data/user.secret", scopes=['read']) + + if not os.path.isfile("data/user.secret"): + mastodon = Mastodon(client_id="data/pyBDSM_clientcred.secret", api_base_url=url) + url = mastodon.auth_request_url(client_id="data/pyBDSM_clientcred.secret", scopes=['read']) + return render_template("register.html",url=url) + mastodon, _ = app_login(url) account = mastodon.me().acct settings.account = account db.session.commit() - if request.method == 'POST': - token = request.form['token'].rstrip() - mastodon = Mastodon(client_id='pyBDSM_clientcred.secret', api_base_url=url) - mastodon.log_in(code=token, to_file='user.secret', scopes=['read']) - - if os.path.isfile('user.secret'): - flash('应用已授权!') - return redirect(url_for('settings')) - - if not os.path.isfile('pyBDSM_clientcred.secret'): - app_register(url) - if not os.path.isfile('user.secret'): - mastodon = Mastodon(client_id='pyBDSM_clientcred.secret', api_base_url=url) - url = mastodon.auth_request_url(client_id='pyBDSM_clientcred.secret', scopes=['read']) - return render_template('register.html',url=url) - - flash('已授权过!') - return redirect(url_for('settings')) - @app.route('/archive', methods=['GET', 'POST']) def archive():