Skip to content

Commit b21f3c8

Browse files
committed
把对model的操作单独使用一个endpoint
1 parent 97c6cd6 commit b21f3c8

File tree

5 files changed

+47
-27
lines changed

5 files changed

+47
-27
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ name: Python package
55

66
on:
77
push:
8-
branches: [ main ]
8+
branches: [ dev ]
99
pull_request:
1010
branches: [ main ]
1111

fast_tmp/admin/constant.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
model_router = "/endpoint"
2+
crud_root_rooter = "endpoint/"

fast_tmp/admin/server.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from fast_tmp.utils.token import create_access_token
1919

2020
from ..jinja_extension.tags import register_tags
21+
from .constant import crud_root_rooter, model_router
2122
from .endpoint import router
2223
from .responses import BaseRes
2324

@@ -30,7 +31,7 @@
3031
admin.mount("/static", app=StaticFiles(directory=base_path + "/static"), name="static")
3132

3233
register_model_site({"Auth": [UserAdmin]})
33-
admin.include_router(router)
34+
admin.include_router(router, prefix=model_router)
3435

3536

3637
@admin.get(
@@ -63,8 +64,8 @@ def index_post(request: Request, user: Optional[User] = Depends(decode_access_to
6364
@admin.post("/login")
6465
def login(
6566
request: Request,
66-
username: str = Form(...),
67-
password: str = Form(...),
67+
username: Optional[str] = Form(None),
68+
password: Optional[str] = Form(None),
6869
session: Session = Depends(get_db_session),
6970
):
7071
context = {
@@ -122,7 +123,7 @@ def get_site(request: Request, user: Optional[User] = Depends(decode_access_toke
122123
{
123124
"label": model.name(),
124125
"url": model.name(),
125-
"schemaApi": model.name() + "/schema",
126+
"schemaApi": crud_root_rooter + model.name() + "/schema",
126127
}
127128
for model in ml
128129
],

fast_tmp/site/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from fast_tmp.admin.schema.frame import Dialog
1212
from fast_tmp.admin.schema.page import Page
1313

14+
from ..admin.constant import crud_root_rooter
1415
from .utils import get_columns_from_model, get_controls_from_model, get_pk
1516

1617

@@ -60,7 +61,7 @@ def get_create_dialogation_button(cls):
6061
name=f"新增{cls.name()}",
6162
title=f"新增{cls.name()}",
6263
body=get_controls_from_model(cls.create_fields),
63-
api=f"post:{cls.prefix}/{cls.name()}/create",
64+
api=f"post:{crud_root_rooter}{cls.name()}/create",
6465
),
6566
),
6667
)
@@ -87,7 +88,7 @@ def get_del_one_button(cls):
8788
type="button",
8889
level=ButtonLevelEnum.danger,
8990
confirmText="确认要删除?",
90-
api="delete:" + cls.name() + "/delete?" + cls.pks(),
91+
api="delete:" + crud_root_rooter + cls.name() + "/delete?" + cls.pks(),
9192
)
9293

9394
@classmethod
@@ -99,8 +100,8 @@ def get_update_one_button(cls):
99100
body=Form(
100101
name=f"修改{cls.name()}",
101102
body=get_controls_from_model(cls.update_fields),
102-
api="put:" + cls.name() + "/update?" + cls.pks(),
103-
initApi="get:" + cls.name() + "/update?" + cls.pks(),
103+
api="put:" + crud_root_rooter + cls.name() + "/update?" + cls.pks(),
104+
initApi="get:" + crud_root_rooter + cls.name() + "/update?" + cls.pks(),
104105
),
105106
),
106107
)
@@ -122,7 +123,7 @@ def get_crud(cls):
122123
columns.append(cls.get_operation())
123124
body.append(
124125
CRUD(
125-
api=cls.name() + "/list",
126+
api=crud_root_rooter + cls.name() + "/list",
126127
columns=columns,
127128
)
128129
)

tests/test_a.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ def test_sing_in():
1818
login_res = client.get("/admin/login")
1919
assert login_res.status_code == 200
2020
assert login_res.text.count("<h2 class=\"card-title text-center mb-4\">Login to your account</h2>") == 1
21+
login_post_res = client.post("/admin/login", data={"password": "root1"})
22+
assert login_post_res.status_code == 200
23+
assert login_post_res.text.count('Username can not be null.') == 1
24+
login_post_res = client.post("/admin/login", data={"username": "root1"})
25+
assert login_post_res.status_code == 200
26+
assert login_post_res.text.count('Password can not be null.') == 1
27+
28+
login_post_res = client.post("/admin/login", data={"username": "root1", "password": "sadf"})
29+
assert login_post_res.status_code == 200
30+
assert login_post_res.text.count('Username or password is error!') == 1
31+
2132
login_post_res = client.post("/admin/login", data={"username": "root", "password": "root"})
2233
assert login_post_res.status_code == 307
2334
assert login_post_res.headers.get("location") == 'http://testserver/admin/'
@@ -28,31 +39,32 @@ def test_site():
2839
get_cookie(client)
2940
site_res = client.get("/admin/site")
3041
assert site_res.status_code == 200
31-
assert site_res.json() == {"status": 0, "msg": "", "data": {
32-
"pages": [{"label": "Auth", "children": [{"label": "User", "url": "User", "schemaApi": "User/schema"}]}]}}
42+
assert site_res.json() == {'data': {'pages': [
43+
{'children': [{'label': 'User', 'schemaApi': 'endpoint/User/schema', 'url': 'User'}], 'label': 'Auth'}]},
44+
'msg': '', 'status': 0}
3345

3446

3547
def test_schema():
3648
client = TestClient(app)
3749
get_cookie(client)
38-
user_schema = client.get("/admin/User/schema")
50+
user_schema = client.get("/admin/endpoint/User/schema")
3951
assert user_schema.status_code == 200
40-
assert user_schema.text == '{"status":0,"msg":"","data":{"type":"page","title":"User","body":[{"type":"button","label":"新增","actionType":"dialog","size":"md","level":"primary","dialog":{"title":"新增","nextCondition":true,"size":"md","body":{"type":"form","name":"新增User","title":"新增User","api":"post:/admin/User/create","body":[{"type":"input-text","name":"username","label":"username","inline":false,"submitOnChange":false,"disabled":false,"required":false,"validations":{"maxLength":128},"mode":"normal","size":"full"},{"type":"input-text","name":"password","label":"password","inline":false,"submitOnChange":false,"disabled":false,"required":false,"validations":{"maxLength":128},"mode":"normal","size":"full"}]}}},{"type":"crud","api":"User/list","columns":[{"type":"text","name":"id","label":"id"},{"type":"text","name":"username","label":"username"},{"type":"text","name":"is_active","label":"is_active"},{"buttons":[{"type":"button","label":"删除","actionType":"ajax","size":"md","level":"danger","confirmText":"确认要删除?","api":"delete:User/delete?id=$id"},{"type":"button","label":"修改","actionType":"dialog","size":"md","level":"primary","dialog":{"title":"修改","nextCondition":true,"size":"md","body":{"type":"form","name":"修改User","api":"put:User/update?id=$id","initApi":"get:User/update?id=$id","body":[{"type":"input-text","name":"password","label":"password","inline":false,"submitOnChange":false,"disabled":false,"required":false,"validations":{"maxLength":128},"mode":"normal","size":"full"}]}}}],"type":"operation","label":"操作"}],"affixHeader":false}],"initFetch":false}}'
52+
assert user_schema.text == '{"status":0,"msg":"","data":{"type":"page","title":"User","body":[{"type":"button","label":"新增","actionType":"dialog","size":"md","level":"primary","dialog":{"title":"新增","nextCondition":true,"size":"md","body":{"type":"form","name":"新增User","title":"新增User","api":"post:endpoint/User/create","body":[{"type":"input-text","name":"username","label":"username","inline":false,"submitOnChange":false,"disabled":false,"required":false,"validations":{"maxLength":128},"mode":"normal","size":"full"},{"type":"input-text","name":"password","label":"password","inline":false,"submitOnChange":false,"disabled":false,"required":false,"validations":{"maxLength":128},"mode":"normal","size":"full"}]}}},{"type":"crud","api":"endpoint/User/list","columns":[{"type":"text","name":"id","label":"id"},{"type":"text","name":"username","label":"username"},{"type":"text","name":"is_active","label":"is_active"},{"buttons":[{"type":"button","label":"删除","actionType":"ajax","size":"md","level":"danger","confirmText":"确认要删除?","api":"delete:endpoint/User/delete?id=$id"},{"type":"button","label":"修改","actionType":"dialog","size":"md","level":"primary","dialog":{"title":"修改","nextCondition":true,"size":"md","body":{"type":"form","name":"修改User","api":"put:endpoint/User/update?id=$id","initApi":"get:endpoint/User/update?id=$id","body":[{"type":"input-text","name":"password","label":"password","inline":false,"submitOnChange":false,"disabled":false,"required":false,"validations":{"maxLength":128},"mode":"normal","size":"full"}]}}}],"type":"operation","label":"操作"}],"affixHeader":false}],"initFetch":false}}'
4153

4254

4355
def test_crud():
4456
client = TestClient(app)
4557
get_cookie(client)
4658
# create
47-
create_user = client.post("/admin/User/create", json={"username": "crud_user", "password": "crud_user"})
59+
create_user = client.post("/admin/endpoint/User/create", json={"username": "crud_user", "password": "crud_user"})
4860
assert create_user.status_code == 200
4961
assert create_user.text == '{"status":0,"msg":"","data":{"username":"crud_user","password":"crud_user"}}'
5062
# login
5163
login_post_res = client.post("/admin/login", data={"username": "crud_user", "password": "crud_user"})
5264
assert login_post_res.status_code == 307
5365
assert login_post_res.next.path_url == "/admin/"
5466
# list
55-
user_list = client.get("/admin/User/list")
67+
user_list = client.get("/admin/endpoint/User/list")
5668
assert user_list.status_code == 200
5769
assert user_list.text.count('"username":"crud_user"') == 1
5870
user_data = user_list.json()["data"]
@@ -64,30 +76,34 @@ def test_crud():
6476
else:
6577
raise Exception("not found user id")
6678
# update
67-
user_update = client.get(f"/admin/User/update?id={user_id}")
79+
user_update = client.get(f"/admin/endpoint/User/update?id={user_id}")
6880
assert user_update.status_code == 200
6981
assert user_update.text.count('"password":') == 1
70-
user_update_p = client.put(f"/admin/User/update?id={user_id}", json={"password": "root"})
82+
user_update_p = client.put(f"/admin/endpoint/User/update?id={user_id}", json={"password": "root"})
7183
assert user_update_p.status_code == 200
7284
assert user_update_p.json()["status"] == 0
7385
# test error pk
74-
user_delete_err = client.delete(f"/admin/User/delete?ids={user_id}")
86+
user_delete_err = client.delete(f"/admin/endpoint/User/delete?ids={user_id}")
7587
error_data = user_delete_err.json()
7688
assert error_data["status"] == 400
7789
assert error_data["msg"] == "主键错误"
78-
error_data = client.put(f"/admin/User/update?ids={user_id}", json={"password": "root"}).json()
90+
error_data = client.put(f"/admin/endpoint/User/update?ids={user_id}", json={"password": "root"}).json()
7991
assert error_data["status"] == 400
8092
assert error_data["msg"] == "主键错误"
8193
# delete
82-
user_delete = client.delete(f"/admin/User/delete?id={user_id}")
94+
user_delete = client.delete(f"/admin/endpoint/User/delete?id={user_id}")
8395
assert user_delete.status_code == 200
8496

8597

8698
def test_not_singin():
8799
client = TestClient(app)
88-
assert client.get("/admin/User/list").text.count("Login to your account") == 1
89-
assert client.post("/admin/User/create", json={"username": "crud_user1", "password": "tt"}).status_code == 307
90-
assert client.get("/admin/User/update?id=1").text.count("Login to your account") == 1
91-
assert client.put("/admin/User/update?id=1", json={"password": "asdfadf"}).status_code == 307
92-
assert client.delete("/admin/User/delete?id=1").status_code == 307
93-
assert client.get("/admin/User/schema").text.count("Login to your account") == 1
100+
assert client.get("/admin/endpoint/User/list").text.count("Login to your account") == 1
101+
assert client.post("/admin/endpoint/User/create",
102+
json={"username": "crud_user1", "password": "tt"}).status_code == 307
103+
assert client.get("/admin/endpoint/User/update?id=1").text.count("Login to your account") == 1
104+
assert client.put("/admin/endpoint/User/update?id=1", json={"password": "asdfadf"}).status_code == 307
105+
assert client.delete("/admin/endpoint/User/delete?id=1").status_code == 307
106+
assert client.get("/admin/endpoint/User/schema").text.count("Login to your account") == 1
107+
assert client.get("/admin/site").text.count("Login to your account") == 1
108+
assert client.get("/admin/").text.count("Login to your account") == 1
109+
assert client.post("/admin/").status_code == 307

0 commit comments

Comments
 (0)