Skip to content

Commit 68d094b

Browse files
committed
超级管理员
1 parent dabe803 commit 68d094b

6 files changed

Lines changed: 82 additions & 26 deletions

File tree

apps/forms.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import re
4+
5+
from .models import User
6+
from utils import ParamsError, Constant
7+
8+
9+
EMAIL_PATTERN = re.compile(
10+
"^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$")
11+
12+
13+
def checkEmail(email):
14+
if not email:
15+
raise(ParamsError("email error"))
16+
if len(email) > User.email.max_length or not EMAIL_PATTERN.match(email):
17+
raise(ParamsError('email error'))
18+
return True
19+
20+
21+
def checkPassword(password):
22+
if not password:
23+
raise(ParamsError("password error"))
24+
if len(password) < Constant.password_min_length or len(password) > Constant.password_max_length:
25+
raise ParamsError("password length must between %s and %",
26+
Constant.password_min_length, Constant.password_max_length)
27+
return True
28+
29+
30+
def checkUsername(name):
31+
if not name:
32+
raise(ParamsError("name error"))
33+
if len(name) > User.name.max_length:
34+
raise(ParamsError("name too long"))
35+
return True
36+
37+
38+
def checkSex(sex):
39+
if not sex:
40+
return False
41+
if sex not in User.sex.choices:
42+
raise(ParamsError("sex error"))
43+
return True

apps/models.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class Permission(object):
6262
EDIT = 4
6363
DELETE = 5
6464
ADD = 6
65+
COMMENT = 7
6566

6667

6768
class Role(BaseModel):
@@ -73,6 +74,15 @@ class Role(BaseModel):
7374
created_time = DateTimeField(default=datetime.datetime.now)
7475
updated_time = DateTimeField(default=datetime.datetime.now)
7576

77+
@staticmethod
78+
def initAdmin():
79+
role = Role.create(name="admin", description="super admin", permission=Permission.ADMIN)
80+
return role
81+
82+
class Meta:
83+
db_table = 'role'
84+
85+
7686

7787
class User(BaseModel):
7888
"""表名都用单数"""
@@ -224,7 +234,7 @@ def createAllTables():
224234
s_user_todos = f"select id, {title}, {detail}, {is_completed}, {created_time}, {updated_time}, {edit_num} from {todo_table} where {user_id}=%s and {is_deleted}=0"
225235
s_user_todo = f"select id, {title}, {detail}, {is_completed}, {created_time}, {updated_time}, {edit_num} from {todo_table} where {user_id}=%s and {is_deleted}=0 and id=%s"
226236
s_todo_title = f"select id, {title} from {todo_table} where title=%s and {user_id}=%s and {is_deleted}=0"
227-
s_all_todo = f"select id, A.{title}, A.{detail}, A.{created_time}, A.{updated_time}, A.{edit_num}, B.{username} from {todo_table} A {user_table} B where A.{user_id}=B.id and {is_deleted}=0 group by B.{username}"
237+
s_all_todo = f"select A.id, A.{title}, A.{detail}, A.{created_time}, A.{updated_time}, A.{edit_num}, B.{username} from {todo_table} A, {user_table} B where A.{user_id}=B.id and A.{is_deleted}=0 and B.{is_deleted}=0 order by B.id"
228238

229239
i_user = f"insert into {user_table} ({username}, {password}, {email}, {age}, {sex}, {city}, {signature}, {created_time}, {updated_time}, {last_login}) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
230240
i_todo = f"insert into {todo_table} ({user_id}, {title}, {detail}, {created_time}, {updated_time}) values (%s, %s, %s, %s, %s)"

apps/todo/view.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from sanic import Blueprint
66
from .. import app
7-
from ..models import S
7+
from ..models import S, User
88
from ..auth import login_required
99
from ..baseview import BaseView
1010
from .forms import postTodoListView

apps/user/forms.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
# -*- coding: utf-8 -*-
22

33
import logging
4-
import re
54

65
from ..models import User
6+
from ..forms import checkEmail, checkPassword, checkUsername, checkSex
77

88
from utils import AttrDict, ParamsError, safeInt, Constant
99

1010

1111
logger = logging.getLogger(__name__)
1212

13-
EMAIL_PATTERN = re.compile(
14-
"^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$")
15-
1613

1714
def getTokenView(request):
1815
"""处理TokenView的get方法的表单"""
@@ -26,31 +23,18 @@ def postTokenView(request):
2623
def postUserListView(request):
2724
name = request.form.get("name")
2825
password = request.form.get("password")
29-
if None in (name, password):
30-
raise(ParamsError)
31-
if len(name) > User.name.max_length:
32-
raise(ParamsError("name too long"))
33-
if len(password) < Constant.password_min_length or len(password) > Constant.password_max_length:
34-
raise ParamsError("password length must between %s and %",
35-
Constant.password_min_length, Constant.password_max_length)
36-
password = User.generalPassword(password)
37-
3826
email = request.form.get("email")
39-
if email:
40-
if len(email) > User.email.max_length or not EMAIL_PATTERN.match(email):
41-
raise(ParamsError("email error"))
42-
else:
43-
email = User.email.default
27+
checkUsername(name)
28+
checkPassword(password)
29+
checkEmail(email)
30+
password = User.generalPassword(password)
4431

4532
age = safeInt(request.form.get("age", User.age.default))
4633
if age < Constant.age_range[0] or age > Constant.age_range[1]:
4734
raise(ParamsError("age error"))
4835
# 先不给默认值
4936
sex = request.form.get("sex")
50-
if sex:
51-
if sex not in User.sex.choices:
52-
raise(ParamsError("sex error"))
53-
else:
37+
if not checkSex(sex):
5438
sex = User.sex.choices.unknown
5539
city = request.form.get("city", User.city.default)
5640
if len(city) > User.city.max_length:

apps/user/view.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ async def post(self, request, **kwargs):
4444
sql_data = await cur.fetchone()
4545
if not sql_data:
4646
return webJson(RetCode.PARAMETER_ERROR, data="无此用户")
47-
logger.debug("sql data: %s", sql_data)
4847
if User.verifyPassword(password, sql_data.password):
4948
data = {Constant.user_id: sql_data.id}
5049
data = User.generalToken(data, expiration=Constant.expires_in_login)

stodo_server.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,28 @@ def cli():
5151

5252
@cli.command(help="初始化数据库")
5353
def initdb():
54-
from apps.models import S
54+
from apps.models import S, Role
5555
S.createAllTables()
56+
Role.initAdmin()
57+
58+
59+
@cli.command("createSuperUser", help="创建超级管理员账号")
60+
def createSuperUser():
61+
from apps.models import User, Role
62+
from apps.forms import checkEmail, checkUsername, checkPassword
63+
# 从数据库绑定admin id
64+
role_admin = Role.select(Role.id).where(Role.name=="admin").get().id
65+
if not role_admin:
66+
return("必须先在Role表中创建管理员角色")
67+
email = click.prompt("请输入邮箱")
68+
checkEmail(email)
69+
name = click.prompt("请输入用户名")
70+
checkUsername(name)
71+
password = click.prompt("请输入密码", hide_input=True, confirmation_prompt=True)
72+
checkPassword(password)
73+
password = User.generalPassword(password)
74+
User.create(email=email, name=name, password=password, sex=User.sex.choices.unknown, role_id=role_admin, signature="I am super admin")
75+
print("成功创建超级管理员账户;%s" % name)
5676

5777

5878
@cli.command(help="运行server")

0 commit comments

Comments
 (0)