refactor/done, i think?

This commit is contained in:
SouthFox 2023-02-17 13:24:11 +08:00
parent fbfd8a1505
commit 5c57d884ee

View file

@ -5,6 +5,7 @@ import json
from flask import render_template, request, url_for, redirect, flash, abort from flask import render_template, request, url_for, redirect, flash, abort
from flask_sqlalchemy import Pagination from flask_sqlalchemy import Pagination
from sqlalchemy import or_
from BDSM import app, db from BDSM import app, db
from BDSM.models import Media, Poll, Settings, Toot, Emoji from BDSM.models import Media, Poll, Settings, Toot, Emoji
from BDSM.toot import app_login, app_register, archive_toot, get_context from BDSM.toot import app_login, app_register, archive_toot, get_context
@ -24,7 +25,7 @@ def index():
return redirect(url_for('settings')) return redirect(url_for('settings'))
else: else:
page = request.args.get('page', 1, type=int) page = request.args.get('page', 1, type=int)
toots_ = Toot.query.order_by(Toot.created_at.desc()).paginate(page=page, per_page=50) toots_ = Toot.query.order_by(Toot.created_at.desc()).filter(or_(Toot.acct==settings.account, Toot.reblog_id!=None)).paginate(page=page, per_page=50)
toots = process_toot(toots_) toots = process_toot(toots_)
path=SimpleNamespace() path=SimpleNamespace()
path.path = "index" path.path = "index"
@ -104,8 +105,7 @@ def context(toot_id):
@app.route('/grab/<int:toot_id>', methods=['GET', 'POST']) @app.route('/grab/<int:toot_id>', methods=['GET', 'POST'])
def grab(toot_id): def grab(toot_id):
settings = Settings.query.first() settings = Settings.query.first()
account = settings.account[1:] domain = settings.domain
username, domain = account.split("@")
url = "https://" + domain url = "https://" + domain
get_context(url, toot_id) get_context(url, toot_id)
@ -115,16 +115,15 @@ def grab(toot_id):
@app.route('/settings', methods=['GET', 'POST']) @app.route('/settings', methods=['GET', 'POST'])
def settings(): def settings():
if request.method == 'POST': if request.method == 'POST':
domain = request.form['domain']
timezone = request.form['timezone'] timezone = request.form['timezone']
if not domain or len(domain) > 50:
flash('无效输入')
return redirect(url_for('settings'))
settings = Settings.query.first() settings = Settings.query.first()
if settings == None: if settings == None:
domain = request.form['domain']
if not domain or len(domain) > 50:
flash('无效输入')
return redirect(url_for('settings'))
settings = Settings(domain=domain, timezone=timezone) settings = Settings(domain=domain, timezone=timezone)
db.session.add(settings) db.session.add(settings)
else: else:
@ -151,16 +150,16 @@ def register():
domain = settings.domain domain = settings.domain
url = "https://" + domain url = "https://" + domain
mastodon, _ = app_login(url)
account = mastodon.me().acct
settings.account = account
db.session.commit()
if request.method == 'POST': if request.method == 'POST':
token = request.form['token'].rstrip() token = request.form['token'].rstrip()
mastodon = Mastodon(client_id='pyBDSM_clientcred.secret', api_base_url=url) mastodon = Mastodon(client_id='pyBDSM_clientcred.secret', api_base_url=url)
mastodon.log_in(code=token, to_file='user.secret', scopes=['read']) 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'): if os.path.isfile('user.secret'):
flash('应用已授权!') flash('应用已授权!')
return redirect(url_for('settings')) return redirect(url_for('settings'))
@ -209,13 +208,14 @@ def process_toot(toots_):
if toot.acct == settings.account: if toot.acct == settings.account:
toot.is_myself = True toot.is_myself = True
if toot.reblog_id != None:
toot = Toot.query.get(toot.reblog_id)
toot = SimpleNamespace(**toot.__dict__)
toot.is_reblog = True
else: else:
toot.is_myself = False toot.is_myself = False
if toot.reblog_id != None:
toot = Toot.query.get(toot.reblog_id)
toot = SimpleNamespace(**toot.__dict__)
toot.is_reblog = True
if toot.media_list != "": if toot.media_list != "":
toot.medias = [] toot.medias = []
#media_list "1111,2222,333," #media_list "1111,2222,333,"