refactor model #3

Merged
southfox merged 3 commits from 1-pr into master 2023-02-17 06:31:41 +01:00
Showing only changes of commit 5c57d884ee - Show all commits

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']
settings = Settings.query.first()
if settings == None:
domain = request.form['domain']
if not domain or len(domain) > 50: if not domain or len(domain) > 50:
flash('无效输入') flash('无效输入')
return redirect(url_for('settings')) return redirect(url_for('settings'))
settings = Settings.query.first()
if settings == None:
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
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'])
mastodon, _ = app_login(url) mastodon, _ = app_login(url)
account = mastodon.me().acct account = mastodon.me().acct
settings.account = account settings.account = account
db.session.commit() 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'): if os.path.isfile('user.secret'):
flash('应用已授权!') flash('应用已授权!')
return redirect(url_for('settings')) return redirect(url_for('settings'))
@ -209,12 +208,13 @@ def process_toot(toots_):
if toot.acct == settings.account: if toot.acct == settings.account:
toot.is_myself = True toot.is_myself = True
else:
toot.is_myself = False
if toot.reblog_id != None: if toot.reblog_id != None:
toot = Toot.query.get(toot.reblog_id) toot = Toot.query.get(toot.reblog_id)
toot = SimpleNamespace(**toot.__dict__) toot = SimpleNamespace(**toot.__dict__)
toot.is_reblog = True toot.is_reblog = True
else:
toot.is_myself = False
if toot.media_list != "": if toot.media_list != "":
toot.medias = [] toot.medias = []