下面是一个测试的 sqlalchemy model
def
fieldtypes(
self
) 是附加的方法 定义了要生成form 的字段 包括验证信息
使用起来如下
- class Servicelog(Base):
- __tablename__ = 'k_servicelog'
- id = Column(Integer, primary_key=True)
- question = Column(String(360),index=True)
- answer = Column(String(300))
- solution = Column(String(300))
- createime = Column(DATETIME,default=datetime.datetime.now(),index=True)
- endtime= Column(DATETIME,index=True)
- feedback = Column(BOOLEAN,index=True)
- servicestatus = Column(Integer,index=True)
- def __init__(self,question='',answer='',feedback=0,servicestatus=0):
- self.question = answer
- self.answer = answer
- self.solution = ''
- self.feedback = feedback
- self.servicestatus = servicestatus
- def fieldtypes(self):
- list = {'question':FormTextarea('question',label='Question',need=True),
- 'answer':FormTextarea('answer',label='Answer'),
- 'solution':FormTextarea('solution',label='Solution'),
- 'feedback':FormCheckbox('feedback',label='Service status'),
- 'servicestatus':FormRadio('servicestatus',{'none':'0','need':'1','tk':'2'}),
- }
- return list
- class toform:
- fields = {}
- param = {}
- types = {}
- def __init__(self,model):
- self.param = model.__dict__
- self.types = model.fieldtypes()
- try:
- for f in self.types:
- if self.types.has_key(f):
- self.types[f].value = self.param[f]
- self.fields[f] = self.types[f]
- else:
- self.fields[f] = '<input type="text" name="%s" value="%s">'%(f,self.param[f])
- except Exception,error:
- print error
- def as_l(self):
- aslabel = ''
- for k in self.fields:
- #print type(self.fields[k]),type(self.types[k].label)
- aslabel += '<label><b>%s:</b> %s<br/></label>'%(''.join(self.types[k].label),self.fields[k])
- return aslabel
- def valid(self):
- ok = True
- for k in self.fields:
- self.fields[k].valid()
- if self.fields[k].error:
- ok = False
- return ok
- class FormText:
- name = ''
- value = ''
- label = ''
- error = ''
- kwargs = {}
- def __init__(self,name,label = '',**kwargs):
- self.name = name
- self.label = label
- self.kwargs = kwargs
- def render(self):
- return u'<input type="text" name="%s" value="%s"> '%(self.name,self.value or '')
- def __unicode__(self):
- return self.render()
- def withlabel(self):
- return u'<label for="%s"><b>%s:</b><input type="text" name="%s" value="%s"><br/> %s</label>'%(self.name,self.label,self.name,self.value or '',self.error)
- def valid(self):
- if self.kwargs.get('need',False) and self.value=='':
- self.error = _('This field is required.')
- return
- if self.kwargs.has_key('minlength'):
- if(len(self.value)<self.kwargs.get('minlength')):
- self.error = _('This field min length is.')
- return
- if self.kwargs.has_key('maxlength'):
- if(len(self.value)<self.kwargs.get('maxlength')):
- self.error = _('This field min length is.')
- return
- class FormTextarea:
- name = ''
- value = ''
- label = ''
- error = ''
- cols = 10
- rows = 5
- kwargs = {}
- def __init__(self,name,cols=10,rows=5,label = '',**kwargs):
- self.name = name
- self.cols = cols
- self.rows = rows
- self.label = label
- self.kwargs = kwargs
- def render(self):
- return u'<textarea name="%s" cols="%s" rows="%s">%s</textarea>'%(self.name,self.cols,self.rows,self.value)
- def valid(self):
- if self.kwargs.get('need',False) and self.value=='':
- self.error = _('This field is required.')
- return
- if self.kwargs.has_key('minlength'):
- if(len(self.value)<self.kwargs.get('minlength')):
- self.error = _('This field min length is.')
- return
- if self.kwargs.has_key('maxlength'):
- if(len(self.value)<self.kwargs.get('maxlength')):
- self.error = _('This field min length is.')
- return
- def __unicode__(self):
- return self.render()
- def withlabel(self):
- return u'<label for="%s"><b>%s:</b>%s<br/> %s</label>'%(self.name,self.label,self.render(),self.error)
- class FormCheckbox:
- name = ''
- value = ''
- error = ''
- kwargs = {}
- def __init__(self,name,label = '',**kwargs):
- self.name = name
- self.label = label
- self.kwargs = kwargs
- def render(self):
- check = ''
- if int(self.value) == 1:
- check = 'checked'
- return '<input type="checkbox" name="%s" value="1" %s>'%(self.name,check)
- def valid(self):
- if self.kwargs.get('need',False) and self.value=='':
- self.error = _('This field is required.')
- def __unicode__(self):
- return self.render()
- class FormRadio:
- name = ''
- value = ''
- label = ''
- error = ''
- option = {}
- kwargs = {}
- def __init__(self,name,option,label = '',**kwargs):
- self.name = name
- self.option = option
- self.label = label
- self.kwargs = kwargs
- def render(self):
- check = ''
- html = ''
- for k in self.option:
- if self.value == self.option[k]:
- check = 'checked'
- else:
- check = ''
- html += '<label><input class="f-radio" type="radio" name="%s" value="%s" %s> %s</label>'%(self.name,self.option[k],check,k)
- return html
- def valid(self):
- if self.kwargs.get('need',False) and self.value=='':
- self.error = _('This field is required.')
- def __unicode__(self):
- return self.render()
- form = toform(Servicelog())
- form.valie() #验证数据
- form.as_l #输出form内各元素
- form.fields.question #输出单个字段 并带 label
- form.fields.question.withlabel #输出单个字段 并带 label,错误提示