# Author : ZZH
# Date : 2024/10/6
# __coding: utf-8__
from flask_wtf import FlaskForm
from flask_wtf.file import FileAllowed
from wtforms import StringField, PasswordField, SubmitField, FileField, TextAreaField, RadioField, SelectField
from wtforms.validators import DataRequired, ValidationError
from app.models import Admin
class LoginForm(FlaskForm):
"""
管理员登录表单
"""
account = StringField(
label='账号',
validators=[
DataRequired('账号不能为空哦')
],
description='账号',
render_kw={
'class': 'form-control',
'placeholder': '请输入账号,亲~',
}
)
pwd = PasswordField(
label='密码',
validators=[
DataRequired('密码不能为空')
],
description='密码',
render_kw={
'class': 'form-control',
'placeholder': '亲,输入密码哦~',
}
)
submit = SubmitField(
'登录',
render_kw={
'class': 'btn btn-primary btn-block btn-flat',
}
)
# 验证账号,命名规则:validate_ + 字段名。若要验证密码,可创建函数 validate_pwd
def validate_account(self, filed):
account = filed.data
admin = Admin.query.filter_by(name=account).count()
if admin == 0:
raise ValidationError('账号不存在!')
class PwdForm(FlaskForm):
old_pwd = PasswordField(
label='旧密码',
validators=[
DataRequired('旧密码是不能为空的啊')
],
description='旧密码',
render_kw={
'class': 'form-control',
'placeholder': '请输入旧密码哦',
}
)
new_pwd = PasswordField(
label='新密码',
validators=[
DataRequired('新密码不可为空啦')
],
description='新密码',
render_kw={
'class': 'form-control',
'placeholder': '亲,输入新密码哦',
}
)
submit = SubmitField(
'保存',
render_kw={
'class': 'btn btn-primary',
}
)
def validate_old_pwd(self, field):
from flask import session
pwd = field.data
name = session['admin']
admin = Admin.query.filter_by(
name=name
).first()
if not admin.check_pwd(pwd):
raise ValidationError('旧密码错误!同学,记性不太行哦~')
class AreaForm(FlaskForm):
"""添加/编辑地区的表单"""
name = StringField(
label='名称',
validators=[
DataRequired('地区名不可为空!')
],
description='地区',
render_kw={
'class': 'form-control',
'placeholder': '请输入地区名称!'
}
)
is_recommended = RadioField(
label='是否推荐',
description='是否推荐',
coerce=int,
choices=[(0, '否'), (1, '是')], default=0,
)
introduction = TextAreaField(
label='简介',
validators=[
DataRequired('简介不能为空!不然有游客来?!')
],
description='简介',
render_kw={
'class': 'form-control',
'rows': 5
}
)