microblog2/app/forms.py

37 lines
1.5 KiB
Python
Raw Normal View History

2012-12-16 08:31:21 +00:00
from flask.ext.wtf import Form, TextField, BooleanField, TextAreaField
from flask.ext.wtf import Required, Length
2013-02-01 05:48:20 +00:00
from flask.ext.babel import gettext
2012-12-16 08:32:38 +00:00
from app.models import User
2012-12-16 08:28:52 +00:00
class LoginForm(Form):
openid = TextField('openid', validators = [Required()])
remember_me = BooleanField('remember_me', default = False)
2012-12-16 08:31:21 +00:00
class EditForm(Form):
nickname = TextField('nickname', validators = [Required()])
about_me = TextAreaField('about_me', validators = [Length(min = 0, max = 140)])
2012-12-16 08:32:38 +00:00
def __init__(self, original_nickname, *args, **kwargs):
Form.__init__(self, *args, **kwargs)
self.original_nickname = original_nickname
def validate(self):
if not Form.validate(self):
return False
if self.nickname.data == self.original_nickname:
return True
2013-02-01 05:48:20 +00:00
if self.nickname.data != User.make_valid_nickname(self.nickname.data):
self.nickname.errors.append(gettext('This nickname has invalid characters. Please use letters, numbers, dots and underscores only.'))
return False
2012-12-16 08:32:38 +00:00
user = User.query.filter_by(nickname = self.nickname.data).first()
if user != None:
2013-02-01 05:48:20 +00:00
self.nickname.errors.append(gettext('This nickname is already in use. Please choose another one.'))
2012-12-16 08:32:38 +00:00
return False
return True
2012-12-16 08:34:46 +00:00
class PostForm(Form):
post = TextField('post', validators = [Required()])
2012-12-16 08:35:16 +00:00
class SearchForm(Form):
search = TextField('search', validators = [Required()])