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_sqlalchemy import Pagination
from sqlalchemy import or_
from BDSM import app, db
from BDSM.models import Media, Poll, Settings, Toot, Emoji
from BDSM.toot import app_login, app_register, archive_toot, get_context
@ -24,7 +25,7 @@ def index():
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_ = 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_)
path=SimpleNamespace()
path.path = "index"
@ -104,8 +105,7 @@ def context(toot_id):
@app.route('/grab/<int:toot_id>', methods=['GET', 'POST'])
def grab(toot_id):
settings = Settings.query.first()
account = settings.account[1:]
username, domain = account.split("@")
domain = settings.domain
url = "https://" + domain
get_context(url, toot_id)
@ -115,16 +115,15 @@ def grab(toot_id):
@app.route('/settings', methods=['GET', 'POST'])
def settings():
if request.method == 'POST':
domain = request.form['domain']
timezone = request.form['timezone']
settings = Settings.query.first()
if settings == None:
domain = request.form['domain']
if not domain or len(domain) > 50:
flash('无效输入')
return redirect(url_for('settings'))
settings = Settings.query.first()
if settings == None:
settings = Settings(domain=domain, timezone=timezone)
db.session.add(settings)
else:
@ -151,16 +150,16 @@ def register():
domain = settings.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)
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'))
@ -209,12 +208,13 @@ def process_toot(toots_):
if toot.acct == settings.account:
toot.is_myself = True
else:
toot.is_myself = False
if toot.reblog_id != None:
toot = Toot.query.get(toot.reblog_id)
toot = SimpleNamespace(**toot.__dict__)
toot.is_reblog = True
else:
toot.is_myself = False
if toot.media_list != "":
toot.medias = []