refactor model #3

Merged
southfox merged 3 commits from 1-pr into master 2023-02-17 06:31:41 +01:00
5 changed files with 43 additions and 26 deletions
Showing only changes of commit e079552833 - Show all commits

View file

@ -193,10 +193,9 @@ def renderfile():
def graball(): def graball():
"""Grab all toots context""" """Grab all toots context"""
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
mastodon, user = app_login(url) mastodon, _ = app_login(url)
acct = mastodon.me().acct acct = mastodon.me().acct
toots = Toot.query.filter(Toot.in_reply_to_id.isnot(None)).all() toots = Toot.query.filter(Toot.in_reply_to_id.isnot(None)).all()

View file

@ -79,6 +79,7 @@ class Poll(db.Model):
options = db.Column(db.Text) options = db.Column(db.Text)
class Settings(db.Model): 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) timezone = db.Column(db.Text)
setup = db.Column(db.Boolean) setup = db.Column(db.Boolean)

View file

@ -4,13 +4,12 @@
<h3>设置</h3> <h3>设置</h3>
<form method="post" class="form-horizontal" role="form"> <form method="post" class="form-horizontal" role="form">
{% if settings != None %} {% if settings != None %}
<div class="form-group"> <br>
<label class="col-sm-2 control-label">用户名</label> <h4>站点地址:</h4>
<div class="col-sm-10"> <p> {{ settings.domain }} </p>
<input type="text" class="form-control" name="account" autocomplete="off" required <br>
value="{{ settings.account }}"> <h4>用户名:</h4>
</div> <p> {{ settings.account }} </p>
</div>
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 control-label">时区</label> <label class="col-sm-2 control-label">时区</label>
<div class="col-sm-10"> <div class="col-sm-10">
@ -24,9 +23,9 @@
</div> </div>
{% else %} {% else %}
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 control-label">用户名</label> <label class="col-sm-2 control-label">站点地址</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" class="form-control" name="account" autocomplete="off" required value="@用户名@实例"> <input type="text" class="form-control" name="domain" autocomplete="off" required value="例如mastodon.social">
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">

View file

@ -3,7 +3,7 @@
from mastodon import Mastodon from mastodon import Mastodon
from tenacity import * from tenacity import *
from BDSM import db 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 sys
import dateutil.parser import dateutil.parser
@ -40,8 +40,9 @@ def app_login(url):
return mastodon, user return mastodon, user
def get_context(url, toot_id): def get_context(url, toot_id):
mastodon, user = app_login(url) mastodon, _ = app_login(url)
acct = mastodon.me().acct settings = Settings.query.first()
acct = settings.account
context = mastodon.status_context(toot_id) context = mastodon.status_context(toot_id)
statuses = [] statuses = []
statuses= context['ancestors'] + context['descendants'] statuses= context['ancestors'] + context['descendants']

View file

@ -7,7 +7,7 @@ from flask import render_template, request, url_for, redirect, flash, abort
from flask_sqlalchemy import Pagination from flask_sqlalchemy import Pagination
from BDSM import app, db from BDSM import app, db
from BDSM.models import Media, Poll, Settings, Toot, Emoji, Other 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 mastodon import Mastodon
from types import SimpleNamespace from types import SimpleNamespace
from datetime import timezone from datetime import timezone
@ -32,6 +32,21 @@ def index():
return render_template('view.html', toots=toots, pagination=toots_, path=path) 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']) @app.route('/search', methods=['GET', 'POST'])
def search(): def search():
if request.method == 'POST': if request.method == 'POST':
@ -106,20 +121,19 @@ 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':
account = request.form['account'] domain = request.form['domain']
timezone = request.form['timezone'] timezone = request.form['timezone']
if not account or len(account) > 30: if not domain or len(domain) > 50:
flash('无效输入') flash('无效输入')
return redirect(url_for('settings')) return redirect(url_for('settings'))
settings = Settings.query.first() settings = Settings.query.first()
if settings == None: if settings == None:
settings = Settings(account=account, timezone=timezone) settings = Settings(domain=domain, timezone=timezone)
db.session.add(settings) db.session.add(settings)
else: else:
settings.account = account
settings.timezone = timezone settings.timezone = timezone
db.session.commit() db.session.commit()
@ -137,11 +151,10 @@ def settings():
def register(): def register():
settings = Settings.query.first() settings = Settings.query.first()
if settings == None: if settings == None:
flash('请先输入用户名') flash('请先输入站点地址')
return redirect(url_for('settings')) return redirect(url_for('settings'))
else: else:
account = settings.account[1:] domain = settings.domain
username, domain = account.split("@")
url = "https://" + domain url = "https://" + domain
if request.method == 'POST': if request.method == 'POST':
@ -149,6 +162,11 @@ def register():
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'))
@ -168,8 +186,7 @@ def archive():
settings = Settings.query.first() settings = Settings.query.first()
if request.method == 'POST': if request.method == 'POST':
archive_match = request.form.getlist("archive_match") archive_match = request.form.getlist("archive_match")
account = settings.account[1:] domain = settings.domain
username, domain = account.split("@")
url = "https://" + domain url = "https://" + domain
archive_toot(url, archive_match) archive_toot(url, archive_match)