feat/use user timezone setting and search feat

This commit is contained in:
SouthFox 2022-09-21 05:09:00 +08:00
parent 8f2c209a45
commit 2f009c3b14
4 changed files with 95 additions and 44 deletions

View file

@ -17,6 +17,12 @@ body {
color: #606984;
}
.search-bar {
float: right;
padding: 5px;
display: flex;
}
.visibility-icon .fa-globle {
color: #388E3C;
}

View file

@ -30,6 +30,10 @@
<li><a href="{{ url_for('index') }}">主页</a></li>
<li><a href="{{ url_for('settings') }}">设置</a></li>
<li><a href="{{ url_for('archive') }}">存档</a></li>
<form class="search-bar" method="post" action="{{url_for('search')}}">
<input type="text" name="query" placeholder="请输入搜索内容">
<input type="submit" value="搜索">
</form>
</ul>
</nav>
{% block content %}{% endblock %}

View file

@ -76,9 +76,10 @@
{% endfor %}
<div class="pagination d-flex justify-content-center">
{% if pagination %}
{% if pagination.has_prev %}
<span>
<a class='page-number' href="{{ url_for('index', page=pagination.prev_num) }}">
<a class='page-number' href="{{ url_for(path.path, page=pagination.prev_num, **path.args) }}">
{{ '<<<' }}
</a>
</span>
@ -90,7 +91,7 @@
{% elif pagination.page != number %}
<span>
<a class='page-number'
href="{{ url_for('index', page=number) }}">
href="{{ url_for(path.path, page=number, **path.args) }}">
{{ number }}
</a>
</span>
@ -101,10 +102,11 @@
{% if pagination.has_next %}
<span>
<a class='page-number' href="{{ url_for('index', page=pagination.next_num) }}">
<a class='page-number' href="{{ url_for(path.path, page=pagination.next_num, **path.args) }}">
{{ '>>>' }}
</a>
</span>
{% endif %}
</div>
{% endif %}
{% endblock %}

View file

@ -1,61 +1,49 @@
#!/usr/bin/env python3
import os
import pytz
from flask import render_template, request, url_for, redirect, flash
from flask_sqlalchemy import Pagination
from BDSM import app, db
from BDSM.models import Media, Settings, Toot, Emoji, Reblog
from BDSM.toot import app_register, archive_toot
from mastodon import Mastodon
from types import SimpleNamespace
from datetime import timezone
@app.context_processor
def inject_setting():
settings = Settings.query.first()
return settings.__dict__
@app.route('/', methods=['GET', 'POST'])
def index():
page = request.args.get('page', 1, type=int)
toots_ = Toot.query.order_by(Toot.created_at.desc()).paginate(page, per_page=50)
toots = []
toots = process_toot(toots_)
path=SimpleNamespace()
path.path = "index"
path.args = {}
for toot_ in toots_.items:
toot = SimpleNamespace(**toot_.__dict__)
return render_template('view.html', toots=toots, pagination=toots_, path=path)
if toot.reblog_id != None:
if toot.reblog_myself:
toot = Toot.query.get(toot.reblog_id)
toot = SimpleNamespace(**toot.__dict__)
toot.is_reblog = True
else:
toot = Reblog.query.get(toot.reblog_id)
toot = SimpleNamespace(**toot.__dict__)
toot.is_reblog = True
@app.route('/search', methods=['GET', 'POST'])
def search():
if request.method == 'POST':
query = request.form['query']
return redirect(url_for('search',query=query))
if toot.media_list != "":
toot.medias = []
#media_list "1111,2222,333,"
media_list = toot.media_list[:-1].split(",")
query = request.args.get('query', "", type=str)
page = request.args.get('page', 1, type=int)
toots_ = Toot.query.order_by(Toot.created_at.desc()).filter(Toot.content.like("%"+query+"%")).paginate(page, per_page=50)
toots = process_toot(toots_)
path=SimpleNamespace()
# Rule: /serch
path.path = str(request.url_rule)[1:] #FIXME
path.args = {}
path.args["query"] = query
return render_template('view.html', toots=toots, pagination=toots_, path=path)
for media_id in media_list:
media = Media.query.get(int(media_id))
if media != None:
toot.medias.append(media)
if toot.emoji_list != "":
toot.emojis = []
#emoji_list "blobfoxaaa,blobcatwww,fox_think,"
emoji_list = toot.emoji_list[:-1].split(",")
for emoji_shortcode in emoji_list:
emoji = Emoji.query.filter_by(shortcode=emoji_shortcode, acct=toot.acct).first()
if emoji != None:
emoji_shortcode = ':' + emoji_shortcode + ':'
emoji_url = emoji.url
emoji_html = f'''
<img class="emojione custom-emoji" alt="{emoji_shortcode}" title="{emoji_shortcode}" src="{emoji.url}" >
'''
toot.content = toot.content.replace(emoji_shortcode, emoji_html)
toots.append(toot)
return render_template('view.html', toots=toots, pagination=toots_)
@app.route('/settings', methods=['GET', 'POST'])
def settings():
@ -134,3 +122,54 @@ def archive():
flash('存档完成……大概!')
return redirect(url_for('index'))
def process_toot(toots_):
toots = []
settings = Settings.query.first()
user_timezone = pytz.timezone(settings.timezone)
fmt = '%Y-%m-%d %H:%M:%S'
for toot_ in toots_.items:
toot = SimpleNamespace(**toot_.__dict__)
toot.created_at = toot.created_at.replace(tzinfo=timezone.utc)
toot.created_at = toot.created_at.astimezone(user_timezone).strftime(fmt)
if toot.reblog_id != None:
if toot.reblog_myself:
toot = Toot.query.get(toot.reblog_id)
toot = SimpleNamespace(**toot.__dict__)
toot.is_reblog = True
else:
toot = Reblog.query.get(toot.reblog_id)
toot = SimpleNamespace(**toot.__dict__)
toot.is_reblog = True
if toot.media_list != "":
toot.medias = []
#media_list "1111,2222,333,"
media_list = toot.media_list[:-1].split(",")
for media_id in media_list:
media = Media.query.get(int(media_id))
if media != None:
toot.medias.append(media)
if toot.emoji_list != "":
toot.emojis = []
#emoji_list "blobfoxaaa,blobcatwww,fox_think,"
emoji_list = toot.emoji_list[:-1].split(",")
for emoji_shortcode in emoji_list:
emoji = Emoji.query.filter_by(shortcode=emoji_shortcode, acct=toot.acct).first()
if emoji != None:
emoji_shortcode = ':' + emoji_shortcode + ':'
emoji_url = emoji.url
emoji_html = f'''
<img class="emojione custom-emoji" alt="{emoji_shortcode}" title="{emoji_shortcode}" src="{emoji.url}" >
'''
toot.content = toot.content.replace(emoji_shortcode, emoji_html)
toots.append(toot)
return toots