Skip to content
This repository was archived by the owner on May 18, 2023. It is now read-only.

Commit 041edb8

Browse files
committed
finish view: char table post, and char info query
1 parent 3278f16 commit 041edb8

File tree

5 files changed

+92
-20
lines changed

5 files changed

+92
-20
lines changed

app/api/character.py

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
import json
23
import logging
34

45
from flask.views import MethodView
@@ -36,13 +37,13 @@ class Meta:
3637
ordered = True
3738

3839
version = ma.fields.String(required=True)
39-
table = ma.fields.String(required=True, validate=lambda s: len(s) < 400000)
40+
table = ma.fields.String(required=True, load_only=True)
4041
table_type = ma.fields.String(required=True)
4142

4243
table_name = ma.fields.String(required=True) # 编码表名称(如小鹤音形拆字表)
4344

4445
group_id = ma.fields.String(required=True) # 一个编码表,需要绑定一个群号。
45-
group_platform = ma.fields.String(required=True) # 该群所属平台
46+
platform = ma.fields.String(required=True) # 该群所属平台
4647

4748
@validates("version")
4849
def validate_version(self, version: str):
@@ -76,21 +77,34 @@ def post(self, data: dict):
7677
"""
7778
# 验证当前用户是指定群的管理员
7879
if not is_(["owner", "admin"], current_user,
79-
group_id=data['group_id'], platform=data['group_platform']):
80+
group_id=data['group_id'], platform=data['platform']):
8081
abort(401, message="you are not the admin of this group")
8182

8283
return character.save_split_table(**data)
8384

8485

86+
class CharInfoSchema(ma.Schema):
87+
char = ma.fields.String()
88+
codes = ma.fields.String()
89+
split = ma.fields.String()
90+
other_info = ma.fields.Dict()
91+
92+
8593
@api_rest.definition('Char')
8694
class CharSchema(ma.Schema):
8795
class Meta:
8896
strict = True
8997
ordered = True
9098

91-
char = ma.fields.String(required=True, )
92-
info = ma.fields.String()
99+
char = ma.fields.String(required=True)
100+
info = ma.fields.Nested(CharInfoSchema, dump_only=True)
93101
version = ma.fields.String()
102+
table_name = ma.fields.String(required=True)
103+
104+
@validates("char")
105+
def validate_table(self, table: str):
106+
if len(table) != 1:
107+
raise ValidationError("`char` must be just one character!")
94108

95109

96110
@table_bp.route("/info/")
@@ -109,4 +123,5 @@ def get(self, data: dict):
109123
if not info:
110124
abort(404, message=f"no info for character {data['char']}")
111125

112-
return info
126+
data['info'] = info
127+
return data

app/models/character.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CharsTable(db.Model):
1818
version = db.Column(db.String(20), index=True, nullable=False)
1919

2020
# 拆字表所属群组,只有该群管理员可编辑该表
21-
group_id = db.Column(db.Integer, db.ForeignKey('group.id'), index=True, nullable=False)
21+
group_db_id = db.Column(db.Integer, db.ForeignKey('group.id'), index=True, nullable=False)
2222

2323
# 同一张拆字表的同一个版本号只能使用一次
2424
__table_args__ = (UniqueConstraint('name', 'version'),)

app/service/words/character.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ def save_split_table(table: str,
5555
table_type: str,
5656
table_name: str,
5757
group_id: str,
58-
group_platform: str):
58+
platform: str):
5959
"""
6060
6161
:param table: 待解析的拆字表字符串
6262
:param version: 拆字表版本号
6363
:param table_type: 编码表类型,用于确定应该调用的解析器
6464
:param table_name: 编码表名称(如小鹤音形拆字表)
6565
:param group_id: 群组 id(非数据库 id)
66-
:param group_platform: 该群所属平台
66+
:param platform: 该群所属平台
6767
:return:
6868
"""
6969
version_ = parse_version(version)
@@ -73,11 +73,11 @@ def save_split_table(table: str,
7373

7474
# 新建 version 行
7575
group_db_id = db.session.query(Group) \
76-
.filter_by(group_id=group_id, platform=group_platform) \
76+
.filter_by(group_id=group_id, platform=platform) \
7777
.first().id
7878
chars_table = CharsTable(name=table_name,
7979
version=version,
80-
group_id=group_db_id)
80+
group_db_id=group_db_id)
8181
db.session.add(chars_table) # 将 chars_table 插入表中
8282
table_id = db.session.query(CharsTable) \
8383
.filter_by(name=table_name, version=version) \
@@ -91,13 +91,18 @@ def save_split_table(table: str,
9191
# 最后提交修改
9292
db.session.commit()
9393

94-
return chars_table
94+
return {
95+
"version": version,
96+
"table_name": table_name,
97+
"group_id": group_id,
98+
"platform": platform,
99+
}
95100

96101

97102
def get_latest_version(table_name: str):
98103
"""获取最新的拆字表版本号"""
99104
versions = map(itemgetter(0), db.session.query(CharsTable.version) \
100-
.filer_by(name=table_name))
105+
.filter_by(name=table_name))
101106
versions = list(map(parse_version, versions))
102107
latest_version = max(versions) if versions else parse_version("0.0.0")
103108

@@ -113,8 +118,9 @@ def get_info(char: str, table_name, version=None):
113118
.filter_by(version=version).first()[0]
114119

115120
# 在该拆字表内查找 char 的信息
116-
info = db.session.query(Character) \
121+
info: Character = db.session.query(Character) \
117122
.filter_by(char=char, table_id=table_id) \
118123
.first()
119124

125+
info.other_info = json.loads(info.other_info)
120126
return info

migrations/versions/0253176ac39b_.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""empty message
2+
3+
Revision ID: 0253176ac39b
4+
Revises: 75c03fa5228c
5+
Create Date: 2019-03-06 20:59:18.574638
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '0253176ac39b'
14+
down_revision = '75c03fa5228c'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.add_column('chars_table', sa.Column('group_db_id', sa.Integer(), nullable=False))
22+
op.create_index(op.f('ix_chars_table_group_db_id'), 'chars_table', ['group_db_id'], unique=False)
23+
op.drop_index('ix_chars_table_group_id', table_name='chars_table')
24+
op.drop_constraint('chars_table_group_id_fkey', 'chars_table', type_='foreignkey')
25+
op.create_foreign_key(None, 'chars_table', 'group', ['group_db_id'], ['id'])
26+
op.drop_column('chars_table', 'group_id')
27+
# ### end Alembic commands ###
28+
29+
30+
def downgrade():
31+
# ### commands auto generated by Alembic - please adjust! ###
32+
op.add_column('chars_table', sa.Column('group_id', sa.INTEGER(), autoincrement=False, nullable=False))
33+
op.drop_constraint(None, 'chars_table', type_='foreignkey')
34+
op.create_foreign_key('chars_table_group_id_fkey', 'chars_table', 'group', ['group_id'], ['id'])
35+
op.create_index('ix_chars_table_group_id', 'chars_table', ['group_id'], unique=False)
36+
op.drop_index(op.f('ix_chars_table_group_db_id'), table_name='chars_table')
37+
op.drop_column('chars_table', 'group_db_id')
38+
# ### end Alembic commands ###

tests/api/test_character.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
"""
44
测试 'api.characters'
55
"""
6-
6+
import json
77
import pytest
88
from flask import Response, url_for
99
from flask.testing import FlaskClient
1010

1111
from app.models import Group, GroupUser, GroupUserRelation
12+
from app.models.character import CharsTable
13+
from app.service.words.character import get_info
1214
from tests.conftest import username, password
1315

1416
platform = 'qq'
@@ -45,8 +47,8 @@ def login(client):
4547
return resp
4648

4749

48-
@pytest.mark.usefixtures("group_admin", "user")
49-
def test_post_table(client: FlaskClient):
50+
@pytest.mark.usefixtures("user", "group_admin")
51+
def test_post_table_query(client: FlaskClient, db):
5052
login(client)
5153

5254
payload = {
@@ -57,9 +59,20 @@ def test_post_table(client: FlaskClient):
5759
顷: qkb qkbr=拆分: 比左 一 ノ 冂 人=首末: 比左 人=编码: b r
5860
皆: jpb jpbb=拆分: 比左 匕 白=首末: 比左 白=编码: b b""",
5961
"group_id": group_id,
60-
"group_platform": platform,
62+
"platform": platform,
6163
}
62-
# resp: Response = client.post(url_for("characters.TableView"), json=payload)
64+
resp: Response = client.post(url_for("characters.TableView"), json=payload)
65+
66+
assert resp.status_code == 201
67+
assert resp.json['version'] == payload['version']
6368

64-
# assert resp.status_code == 201
69+
resp_2: Response = client.get(url_for("characters.CharView"), json={
70+
"char": "比",
71+
"version": "0.0.1",
72+
"table_name": "小鹤音形拆字表"
73+
})
6574

75+
info = resp_2.json['info']
76+
assert info['codes'] == "bi bibb*"
77+
assert info['split'] == '比左 匕'
78+
assert info['other_info'] == {'编码': 'b b', '首末': '比左 匕'}

0 commit comments

Comments
 (0)