from WebKit.Page import Page from FunFormKit.Form import FormServlet, FormDefinition from FunFormKit import Field from FunFormKit.Validator import ValidatorConverter, InvalidField import random class IntValidator(ValidatorConverter): def convert(self, num): try: return int(num) except ValueError: raise InvalidField, "Field must be a number!" formDef = FormDefinition("CCConvertedWinner", [Field.TextField("randmax", maxLength=5, size=3, description="Upper border of random"\ "number wanted", validators=[IntValidator()]), Field.TextField("useless", maxLength=200, size=20, description="A useless field"), Field.SubmitButton("submit", description="Randomize me!", methodToInvoke="randomize_me")], ) class CCConvertedWinner(Page, FormServlet): def __init__(self): Page.__init__(self) FormServlet.__init__(self, [formDef]) def title(self): return "A random example.." def randomize_me(self, fields): maxrandnr = int(fields['randmax']) randnr = random.randrange(0, maxrandnr) self.write("Your random number between 0 and %d is %d
" % (maxrandnr, randnr)) def writeContent(self): submitted, data = self.processForm() rf = self.renderableForm() self.write(rf.htFormTable(bgcolor="#ddddff"))