#encoding=utf-8
from app.models importStudentfrom flask importgimportrefrom flask.ext.wtf importFormfrom wtforms importStringField, RadioField, PasswordField, TextAreaField, BooleanField, DateField, ValidationError, \
IntegerFieldfrom wtforms.validators importDataRequired, Length, Regexp, Email, EqualTofrom wtforms.widgets importListWidget, HTMLStringclassBSListWidget(ListWidget):def __call__(self, field, **kwargs):
kwargs.setdefault('id', field.id)
html= ""
for subfield infield:
html+= u' %s%s ' %(subfield(), subfield.label.text)returnHTMLString(html)classFields(object):
notnull= u'该项输入不能为空'
def get_len_str(min=None, max=None):if min and notmax:return u"该项输入的最小长度必须是%d" %minelif max and notmin:return u"该项输入的最大长度必须是%d" %maxelse:return u'该输入的长度必须大于%d,小于%d' %(min, max)
username= StringField(label=u'请输入您的用户名',
validators=[DataRequired(message=notnull),
Length(min=0, max=15, message=get_len_str(0, 16)),
])
password= PasswordField(label=u'请输入密码', description=u'请输入密码',
validators=[DataRequired(message=notnull),
Length(min=0, max=60, message=get_len_str(min=0, max=61)),
])
confirm_password= PasswordField(label=u'请确认密码',
description=u'请确认密码',
validators=[DataRequired(message=notnull),
Length(min=5, max=60, message=get_len_str(min=4, max=61)),
EqualTo(u'confirm_password', message=u'两次输入的密码不一致'), ]
)
student_amount= StringField(label=u'请输入您指导的学生数量',
validators=[Regexp(re.compile(r"\d"))])
is_active= RadioField(label=u'是否激活账户',
coerce=int,
choices=[(0, u'否'), (1, u'是')],
default=0,
widget=BSListWidget())
notice= TextAreaField(label=u'请填写对学生的通知')
attachment= StringField(label=u'添加附加',
validators=[Length(min=0, max=32, message=get_len_str(min=0, max=33))], )
is_comment_teacher= RadioField(label=u'是否有评价功能',
coerce=int,
choices=[(0, u'否'), (1, u'是')],
default=0,
widget=BSListWidget())
student_name= StringField(label=u'请输入您的姓名',
description='',
validators=[DataRequired(message=notnull),
Length(min=0, max=15, message=get_len_str(0, 16)),
])
sex= RadioField(label=u'您的性别',
coerce=int,
choices=[(0, u'男'), (1, u'女')],
default=0,
widget=BSListWidget())
user_type= RadioField(label=u'您是',
coerce=str,
choices=[(u'student', u'学生'), (u'teacher', u'老师'), (u'admin', u'管理员')],
default=u'student',
widget=BSListWidget())
mark= StringField(label=u'您的分数',
default=0,
validators=[DataRequired(message=notnull),
Length(min=0, max=100, message=get_len_str(0, 101)),
])
comment= TextAreaField(label=u'请填写您对学生的评语',
validators=[
Length(min=0, max=128, message=get_len_str(0, 129)),
])
title= StringField(label=u'请填写毕业设计的题目',
validators=[
Length(min=0, max=128, message=get_len_str(0, 129)),
])
description= TextAreaField(label=u'请填写毕业设计的描述')
task_start_date= DateField(label=u'开始时间')
task_end_date= DateField(label=u'结束时间')
comment_start_date= DateField(label=u'开始时间')
comment_end_date= DateField(label=u'结束时间')classLoginForm(Form):
username=Fields.username
password=Fields.password
user_type=Fields.user_type
remember_me= BooleanField(label=u'记住我', default=u'y')
errors={}defvalidate_fields(self):
status=True
status= status andself.username.validate(self)
status= status andself.username.validate(self)
self.password.validate()#def validate(self):
self._fields
self.validate_on_submit()returnself.username.validate(self)#def validate_username(self, field):
#user = Student.get_user(field.data)
#if not user:
#print 'not user'
#self.errors['username'] = u'用户名不存在'
#raise ValidationError(message=u'该用户名已被注册')
#else:
#print 'user'
#return True
# #def validate_password(self,field):
#if g.user:
#if field.data != g.user.username:
#field.errors.append(message=u'用户密码不正确')
#return False
# #def validate_user_type(self, field):
#print field.data
# #def validate_remember_me(self,field):
#if field.data:
#print field.data
classStuInfo(Form):
username=Fields.username
student_name=Fields.student_name
sex=Fields.sex
attachment=Fields.attachment
mark=Fields.mark
comment=Fields.commentclassBaseForm(object):def __init__(self, form):
self.username= Field(label=u'用户名', type='text', validators={'min_length': 5})for field_name, data inform.items():
getattr(self, field_name).data=datadefvalidate(self):
status=Truefor field_name in self.__dict__:
status= status andgetattr(self, field_name).validate()returnstatusclassField(object):def __init__(self, label=None, type='text', validators={}, description=None, data=''):
self.data=data
self.label=label
self.type=type
self.validators=validators
self.description=descriptiondefvalidate(self):
status=Truefor method_name, param inself.validators.items():printmethod_name, param
status= status andgetattr(self, method_name)(param)returnstatusdefis_null(self, status):ifstatus:returnTrueif notself.data:returnFalseif hasattr(self.data, 'replace') and len(self.data.replace(' ', '')) >0:returnFalseif len(self.data) ==0:returnFalsereturnTruedef min_length(self, min=-1):if not self.is_null(False) and len(self.data) max:returnFalsereturnTruedefmin(self, min):try:if int(self.data) max:returnFalsereturnTrueexceptException, e:printereturnFalsedefequal_to(self, data):if self.data !=data:returnFalsereturnTruedef select_from(self, *args):if self.data not inargs:returnFalsereturnTrueif __name__ == '__main__':
b= BaseForm({'username': 'o'})printb.validate()print 'ok'