代码拉取完成,页面将自动刷新
import datetime
import random
import re
import click
import app.models as models
from app.extensions import *
from app.models import RoleModel, UserModel
def register_command(app: Flask):
@app.cli.command()
def create_role():
db.drop_all()
db.create_all()
roles = [
{"name": "学生", "code": "stu"},
{"name": "老师", "code": "tea"},
]
role1 = models.RoleModel(name=roles[0]["name"], code=roles[0]["code"])
role2 = models.RoleModel(name=roles[1]["name"], code=roles[1]["code"])
db.session.add(role1)
db.session.add(role2)
_permission = {
"index": "/",
"dashboard": "/dashboard",
}
power1 = models.PowerModel(code="index", url=_permission["index"])
power2 = models.PowerModel(code="dashboard", url=_permission["dashboard"])
db.session.add(power1)
db.session.add(power2)
role1.power.append(power1)
role2.power.append(power1)
role2.power.append(power2)
db.session.commit()
@app.cli.command()
@click.option("--username")
@click.option("--password")
@click.option("--mobile")
@click.option("--gender")
@click.option("--place")
def new_tea(username, password, mobile, gender, place):
pattern = r"^1[3-9]\d{9}$" # 后端为什么还要再次校验? 有没有可能请求不是前端发送过来的
ret = re.match(pattern, mobile)
if not ret:
click.echo("手机号码不符合规范")
return
if not all([username, gender, mobile, place, password]):
click.echo("数据缺少")
return
q = db.select(UserModel)
q = q.filter(UserModel.stu_or_tea_name == username)
user = db.session.execute(q).first()
if user:
click.echo("已有数据不能添加")
return
try:
user = UserModel()
user.stu_or_tea_name = username
user.gender = gender
user.place = place
user.mobile = mobile
user.set_password_hash(password)
user.role_id = 2
except Exception as e:
current_app.logger.error(e)
current_app.logger.error("操作失败")
click.echo("操作失败")
return
user.save_add_db()
click.echo("添加成功")
return
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。