提交 65e09749 编辑于 作者: yuudi's avatar yuudi
浏览文件

fix: display all clans of user

上级 64d16466
# 更新日志
## 3.6.3
- 用户首页显示所有已加入的公会
- 兼容 `utf-8 with BOM` 格式的配置文件
- 修改角色昵称表刷新规则
## 3.6.2
- 按日计算的统计图表([#99](https://github.com/yuudi/yobot/pull/99)
......
......@@ -22,10 +22,7 @@ do
rm .YOBOT_RESTART
fi
done
""".format(
'./yobot' if '_MEIPASS' in dir(sys) else 'python3 main.py'
)
)
""".format('./yobot' if '_MEIPASS' in dir(sys) else 'python3 main.py'))
print('请通过"sh yobotg.sh"启动')
sys.exit()
if os.path.exists('.YOBOT_RESTART'):
......@@ -35,7 +32,7 @@ import asyncio
import json
import time
import pytz
import tzlocal
from aiocqhttp import CQHttp
from apscheduler.schedulers.asyncio import AsyncIOScheduler
......@@ -61,7 +58,7 @@ def main():
basedir = "./yobot_data"
if os.path.exists(os.path.join(basedir, "yobot_config.json")):
try:
with open(os.path.join(basedir, "yobot_config.json"), "r", encoding="utf-8") as f:
with open(os.path.join(basedir, "yobot_config.json"), "r", encoding="utf-8-sig") as f:
config = json.load(f)
except json.JSONDecodeError as e:
print('配置文件格式错误,请检查配置文件。三秒后关闭')
......@@ -74,9 +71,15 @@ def main():
else:
token = None
try:
tzlocal.get_localzone()
except:
print("无法获取系统时区,请将系统时区设置为北京/上海时区")
sys.exit()
cqbot = CQHttp(access_token=token,
enable_http_post=False)
sche = AsyncIOScheduler(timezone=pytz.timezone('Asia/Shanghai'))
sche = AsyncIOScheduler()
bot = yobot.Yobot(data_path=basedir,
scheduler=sche,
quart_app=cqbot.server_app,
......
......@@ -33,22 +33,26 @@
<a href="{{ url_for('yobot_user_info', qqid=user.qqid) }}">
<el-button type="primary">个人中心</el-button>
</a>
{% if user.clan_group_id is none -%}
<a href="{{ url_for('yobot_reset_pwd') }}">
<el-button type="primary">修改密码</el-button>
</a>
<a href="{{ url_for('yobot_logout') }}">
<el-button type="primary">登出</el-button>
</a>
</el-row>
<el-row>
{% if not clan_groups -%}
<el-tooltip effect="dark" placement="bottom">
<div slot="content">你还没有选择默认公会<br />请在你的公会群里发送“加入公会”来设置默认公会</div>
<el-button type="primary" disabled>无公会</el-button>
</el-tooltip>
{%- else -%}
<a href="{{ url_for('yobot_clan', group_id=user.clan_group_id) }}">
<el-button type="primary">我的公会</el-button>
{% for group in clan_groups -%}
<a href="{{ url_for('yobot_clan', group_id=group.group_id) }}">
<el-button type="primary">公会:{{ group.info.group_name }}</el-button>
</a>
{% endfor -%}
{%- endif %}
<a href="{{ url_for('yobot_reset_pwd') }}">
<el-button type="primary">修改密码</el-button>
</a>
<a href="{{ url_for('yobot_logout') }}">
<el-button type="primary">登出</el-button>
</a>
</el-row>
</div>
<p><a href="{{ url_for('yobot_about') }}">关于yobot</a></p>
......
import asyncio
import json
import os
import random
......@@ -7,7 +8,6 @@ from typing import Dict, List, Optional, Tuple
from urllib.parse import urljoin
import aiohttp
import requests
from .templating import render_template
from .yobot_exceptions import ServerError
......@@ -35,17 +35,33 @@ class Consult:
Nicknames_csv = "https://gitee.com/yobot/pcr-nickname/raw/master/nicknames.csv"
Nicknames_repo = "https://gitee.com/yobot/pcr-nickname/blob/master/nicknames.csv"
def __init__(self, glo_setting: dict, *args, refresh_nickfile=False, **kwargs):
def __init__(self, glo_setting: dict, *args, **kwargs):
self.setting = glo_setting
self.nickname_dict: Dict[str, Tuple[str, str]] = {}
nickfile = os.path.join(glo_setting["dirname"], "nickname3.csv")
if refresh_nickfile or not os.path.exists(nickfile):
res = requests.get(self.Nicknames_csv)
if res.status_code != 200:
raise ServerError(
"bad server response. code: "+str(res.status_code))
with open(nickfile, "w", encoding="utf-8-sig") as f:
f.write(res.text)
if not os.path.exists(nickfile):
asyncio.ensure_future(self.update_nicknames(),
loop=asyncio.get_event_loop())
else:
with open(nickfile, encoding="utf-8-sig") as f:
csv = f.read()
for line in csv.split("\n")[1:]:
row = line.split(",")
for col in row:
self.nickname_dict[col] = (row[0], row[1])
async def update_nicknames(self):
nickfile = os.path.join(self.setting["dirname"], "nickname3.csv")
try:
async with aiohttp.request('GET', self.Nicknames_csv) as resp:
if resp.status != 200:
raise ServerError(
"bad server response. code: "+str(resp.status))
restxt = await resp.text()
with open(nickfile, "w", encoding="utf-8-sig") as f:
f.write(restxt)
except aiohttp.ClientError as e:
raise RuntimeError('错误'+str(e))
with open(nickfile, encoding="utf-8-sig") as f:
csv = f.read()
for line in csv.split("\n")[1:]:
......@@ -66,6 +82,7 @@ class Consult:
if is_retry:
msg = "没有找到【{}】,目前昵称表:{}".format(
index, self.Nicknames_repo)
asyncio.ensure_future(self.update_nicknames())
raise ValueError(msg)
else:
self.__init__(self.setting, refresh_nickfile=True)
......
......@@ -12,7 +12,7 @@ from quart import (Quart, Response, jsonify, make_response, redirect, request,
from .templating import render_template, template_folder
from .web_util import rand_string
from .ybdata import MAX_TRY_TIMES, User, User_login
from .ybdata import MAX_TRY_TIMES, Clan_group, Clan_member, User, User_login
EXPIRED_TIME = 7 * 24 * 60 * 60 # 7 days
LOGIN_AUTH_COOKIE_NAME = 'yobot_login'
......@@ -80,7 +80,6 @@ class Login:
else:
assert False, f"没有实现匹配码{match_num}对应的操作"
return {
'reply': reply,
'block': True
......@@ -407,9 +406,20 @@ class Login:
async def yobot_user():
if 'yobot_user' not in session:
return redirect(url_for('yobot_login', callback=request.path))
clan_groups = Clan_member.select(
Clan_member.group_id,
Clan_group.group_name,
).join(
Clan_group,
on=(Clan_member.group_id == Clan_group.group_id),
attr='info',
).where(
Clan_member.qqid == session['yobot_user']
)
return await render_template(
'user.html',
user=User.get_by_id(session['yobot_user']),
clan_groups=list(clan_groups),
)
@app.route(
......
......@@ -61,7 +61,7 @@ class WebUtil:
if not os.path.exists(os.path.join(self.resource_path, 'background.jpg')):
try:
r = requests.get('http://x.jingzhidh.com/s/background.jpg')
r = requests.get('https://i.loli.net/2020/05/31/IirkP9TpnV7Ks6q.jpg')
assert r.status_code == 200
with open(os.path.join(self.resource_path, 'background.jpg'), 'wb') as f:
f.write(r.content)
......
......@@ -4,7 +4,7 @@ from playhouse.migrate import SqliteMigrator, migrate
from .web_util import rand_string
_db = SqliteDatabase(None)
_version = 8 # 目前版本
_version = 9 # 目前版本
MAX_TRY_TIMES = 3
......@@ -30,7 +30,6 @@ class User(_BaseModel):
authority_group = IntegerField(default=100)
privacy = IntegerField(default=MAX_TRY_TIMES) # 密码错误次数
clan_group_id = BigIntegerField(null=True)
last_login_time = BigIntegerField(default=0)
last_login_ipaddr = IPField(default='0.0.0.0')
password = FixedCharField(max_length=64, null=True)
......@@ -74,7 +73,7 @@ class Clan_group(_BaseModel):
class Clan_member(_BaseModel):
group_id = BigIntegerField()
qqid = BigIntegerField()
qqid = BigIntegerField(index=True)
role = IntegerField(default=100)
last_save_slot = IntegerField(null=True)
remaining_status = TextField(null=True)
......@@ -231,5 +230,9 @@ def db_upgrade(old_version):
IntegerField(default=0)),
migrator.add_index('clan_challenge', ('bid', 'gid'), False)
)
if old_version < 9:
migrate(
migrator.add_index('clan_member', ('qqid',), False)
)
DB_schema.replace(key='version', value=str(_version)).execute()
......@@ -33,8 +33,8 @@ else:
class Yobot:
Version = "[v3.6.2]"
Version_id = 187
Version = "[v3.6.3-beta.1]"
Version_id = 189
# "git rev-list --count HEAD"
def __init__(self, *,
......@@ -84,21 +84,16 @@ class Yobot:
default_pool_filepath = os.path.join(
os.path.dirname(__file__), "packedfiles", "default_pool.json")
shutil.copyfile(default_pool_filepath, pool_filepath)
with open(config_f_path, "r+", encoding="utf-8") as config_file:
with open(config_f_path, "r", encoding="utf-8-sig") as config_file:
cfg = json.load(config_file)
for k in self.glo_setting.keys():
if k in cfg:
self.glo_setting[k] = cfg[k]
config_file.seek(0)
config_file.truncate()
json.dump(self.glo_setting, config_file, indent=4)
if verinfo is None:
verinfo = updater.get_version(self.Version, self.Version_id)
print(verinfo['ver_name'])
modified = False
# initialize database
ybdata.init(os.path.join(dirname, 'yobotdata.db'))
......@@ -115,17 +110,16 @@ class Yobot:
ipaddr,
self.glo_setting["port"],
)
modified = True
if not self.glo_setting["public_address"].endswith("/"):
self.glo_setting["public_address"] += "/"
modified = True
if not self.glo_setting["public_basepath"].startswith("/"):
self.glo_setting["public_basepath"] = "/" + \
self.glo_setting["public_basepath"]
modified = True
if not self.glo_setting["public_basepath"].endswith("/"):
self.glo_setting["public_basepath"] += "/"
modified = True
# initialize update time
if self.glo_setting["update-time"] == "random":
......@@ -133,17 +127,14 @@ class Yobot:
random.randint(2, 4),
random.randint(0, 59)
)
modified = True
# initialize client salt
if self.glo_setting["client_salt"] is None:
self.glo_setting["client_salt"] = web_util.rand_string(16)
modified = True
# save initialization
if modified:
with open(config_f_path, "w", encoding="utf-8") as config_file:
json.dump(self.glo_setting, config_file, indent=4)
with open(config_f_path, "w", encoding="utf-8") as config_file:
json.dump(self.glo_setting, config_file, indent=4)
# initialize utils
templating.Ver = self.Version[2:-1]
......
Supports Markdown
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册