From e079552833617fafc2fb815a32da584fb20c2dc1 Mon Sep 17 00:00:00 2001 From: SouthFox Date: Fri, 17 Feb 2023 12:33:34 +0800 Subject: [PATCH] refactor/settings --- BDSM/commands.py | 5 ++--- BDSM/models.py | 3 ++- BDSM/templates/settings.html | 17 ++++++++--------- BDSM/toot.py | 7 ++++--- BDSM/views.py | 37 ++++++++++++++++++++++++++---------- 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/BDSM/commands.py b/BDSM/commands.py index 6be5677..b291fe0 100644 --- a/BDSM/commands.py +++ b/BDSM/commands.py @@ -193,10 +193,9 @@ def renderfile(): def graball(): """Grab all toots context""" settings = Settings.query.first() - account = settings.account[1:] - username, domain = account.split("@") + domain = settings.domain url = "https://" + domain - mastodon, user = app_login(url) + mastodon, _ = app_login(url) acct = mastodon.me().acct toots = Toot.query.filter(Toot.in_reply_to_id.isnot(None)).all() diff --git a/BDSM/models.py b/BDSM/models.py index 07601ef..596e5ba 100644 --- a/BDSM/models.py +++ b/BDSM/models.py @@ -79,6 +79,7 @@ class Poll(db.Model): options = db.Column(db.Text) class Settings(db.Model): - account = db.Column(db.Text, primary_key=True) + domain = db.Column(db.Text, primary_key=True) + account = db.Column(db.Text) timezone = db.Column(db.Text) setup = db.Column(db.Boolean) diff --git a/BDSM/templates/settings.html b/BDSM/templates/settings.html index 483fb3b..2e48400 100644 --- a/BDSM/templates/settings.html +++ b/BDSM/templates/settings.html @@ -4,13 +4,12 @@

设置

{% if settings != None %} -
- -
- -
-
+
+

站点地址:

+

{{ settings.domain }}

+
+

用户名:

+

{{ settings.account }}

@@ -24,9 +23,9 @@
{% else %}
- +
- +
diff --git a/BDSM/toot.py b/BDSM/toot.py index c0efba5..bcc8e06 100644 --- a/BDSM/toot.py +++ b/BDSM/toot.py @@ -3,7 +3,7 @@ from mastodon import Mastodon from tenacity import * from BDSM import db -from BDSM.models import Other, Toot, Tag, Media, Emoji, Poll +from BDSM.models import Other, Toot, Tag, Media, Emoji, Poll, Settings import sys import dateutil.parser @@ -40,8 +40,9 @@ def app_login(url): return mastodon, user def get_context(url, toot_id): - mastodon, user = app_login(url) - acct = mastodon.me().acct + mastodon, _ = app_login(url) + settings = Settings.query.first() + acct = settings.account context = mastodon.status_context(toot_id) statuses = [] statuses= context['ancestors'] + context['descendants'] diff --git a/BDSM/views.py b/BDSM/views.py index d0a9570..7969828 100644 --- a/BDSM/views.py +++ b/BDSM/views.py @@ -7,7 +7,7 @@ from flask import render_template, request, url_for, redirect, flash, abort from flask_sqlalchemy import Pagination from BDSM import app, db from BDSM.models import Media, Poll, Settings, Toot, Emoji, Other -from BDSM.toot import app_register, archive_toot, get_context +from BDSM.toot import app_login, app_register, archive_toot, get_context from mastodon import Mastodon from types import SimpleNamespace from datetime import timezone @@ -32,6 +32,21 @@ def index(): return render_template('view.html', toots=toots, pagination=toots_, path=path) +@app.route('/favourited', methods=['GET', 'POST']) +def favourited(): + settings = Settings.query.first() + if settings == None: + return redirect(url_for('settings')) + else: + page = request.args.get('page', 1, type=int) + toots_ = Toot.query.order_by(Toot.created_at.desc()).paginate(page=page, per_page=50) + toots = process_toot(toots_) + path=SimpleNamespace() + path.path = "favourited" + path.args = {} + + return render_template('view.html', toots=toots, pagination=toots_, path=path) + @app.route('/search', methods=['GET', 'POST']) def search(): if request.method == 'POST': @@ -106,20 +121,19 @@ def grab(toot_id): @app.route('/settings', methods=['GET', 'POST']) def settings(): if request.method == 'POST': - account = request.form['account'] + domain = request.form['domain'] timezone = request.form['timezone'] - if not account or len(account) > 30: + if not domain or len(domain) > 50: flash('无效输入') return redirect(url_for('settings')) settings = Settings.query.first() if settings == None: - settings = Settings(account=account, timezone=timezone) + settings = Settings(domain=domain, timezone=timezone) db.session.add(settings) else: - settings.account = account settings.timezone = timezone db.session.commit() @@ -137,11 +151,10 @@ def settings(): def register(): settings = Settings.query.first() if settings == None: - flash('请先输入用户名!') + flash('请先输入站点地址!') return redirect(url_for('settings')) else: - account = settings.account[1:] - username, domain = account.split("@") + domain = settings.domain url = "https://" + domain if request.method == 'POST': @@ -149,6 +162,11 @@ def register(): mastodon = Mastodon(client_id='pyBDSM_clientcred.secret', api_base_url=url) mastodon.log_in(code=token, to_file='user.secret', scopes=['read']) + mastodon, _ = app_login(url) + account = mastodon.me().acct + settings.account = account + db.session.commit() + if os.path.isfile('user.secret'): flash('应用已授权!') return redirect(url_for('settings')) @@ -168,8 +186,7 @@ def archive(): settings = Settings.query.first() if request.method == 'POST': archive_match = request.form.getlist("archive_match") - account = settings.account[1:] - username, domain = account.split("@") + domain = settings.domain url = "https://" + domain archive_toot(url, archive_match)