# forms are not just about display, instead they are more of validation
# wtf forms protect our site against csrf attacks
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, TextAreaField

from wtforms.validators import (DataRequired, Regexp, ValidationError, Email,
							   Length, EqualTo)


from models import User

def name_exists(form, field):
	if User.select().where(User.username == field.data).exists():
		raise ValidationError('User with this name already exists.')

def email_exists(form, field):
	if User.select().where(User.email == field.data).exists():
		raise ValidationError('User with this email already exists.')

class RegisterForm(FlaskForm):
	username = StringField(
		'Username', # is the label
		validators=[
			DataRequired(),
			Regexp(
				r'^[a-zA-Z0-9_]+$',
				message = ("Username should be one word, letters, numbers and underscores only.")
				),
			name_exists
		])

	email = StringField(
		'Email',
		validators=[
			DataRequired(),
			Email(),
			email_exists
		])

	password = PasswordField(
		'Password',
		validators=[
			DataRequired(),
			Length(min=8),
			EqualTo('password2', message = 'Passwords must match')
		])

	password2 = PasswordField(
		'Confirm Password',
		validators=[DataRequired()
		])


class LoginForm(FlaskForm):
	email = StringField('Email', validators=[DataRequired(), Email()])
	password = PasswordField('Password', validators=[DataRequired()])

class PostForm(FlaskForm):
	content = TextAreaField("What's Up?", validators = [DataRequired()])

[(14, 'wtforms.validators.ValidationError', 'ValidationError', False, 'from wtforms.validators import DataRequired, Regexp, ValidationError, Email, Length, EqualTo\n'), (18, 'wtforms.validators.ValidationError', 'ValidationError', False, 'from wtforms.validators import DataRequired, Regexp, ValidationError, Email, Length, EqualTo\n'), (24, 'wtforms.validators.DataRequired', 'DataRequired', False, 'from wtforms.validators import DataRequired, Regexp, ValidationError, Email, Length, EqualTo\n'), (25, 'wtforms.validators.Regexp', 'Regexp', False, 'from wtforms.validators import DataRequired, Regexp, ValidationError, Email, Length, EqualTo\n'), (35, 'wtforms.validators.DataRequired', 'DataRequired', False, 'from wtforms.validators import DataRequired, Regexp, ValidationError, Email, Length, EqualTo\n'), (36, 'wtforms.validators.Email', 'Email', False, 'from wtforms.validators import DataRequired, Regexp, ValidationError, Email, Length, EqualTo\n'), (43, 'wtforms.validators.DataRequired', 'DataRequired', False, 'from wtforms.validators import DataRequired, Regexp, ValidationError, Email, Length, EqualTo\n'), (44, 'wtforms.validators.Length', 'Length', False, 'from wtforms.validators import DataRequired, Regexp, ValidationError, Email, Length, EqualTo\n'), (45, 'wtforms.validators.EqualTo', 'EqualTo', False, 'from wtforms.validators import DataRequired, Regexp, ValidationError, Email, Length, EqualTo\n'), (50, 'wtforms.validators.DataRequired', 'DataRequired', False, 'from wtforms.validators import DataRequired, Regexp, ValidationError, Email, Length, EqualTo\n'), (55, 'wtforms.validators.DataRequired', 'DataRequired', False, 'from wtforms.validators import DataRequired, Regexp, ValidationError, Email, Length, EqualTo\n'), (55, 'wtforms.validators.Email', 'Email', False, 'from wtforms.validators import DataRequired, Regexp, ValidationError, Email, Length, EqualTo\n'), (56, 'wtforms.validators.DataRequired', 'DataRequired', False, 'from wtforms.validators import DataRequired, Regexp, ValidationError, Email, Length, EqualTo\n'), (59, 'wtforms.validators.DataRequired', 'DataRequired', False, 'from wtforms.validators import DataRequired, Regexp, ValidationError, Email, Length, EqualTo\n'), (13, 'models.User.select', 'User.select', False, 'from models import User\n'), (17, 'models.User.select', 'User.select', False, 'from models import User\n')]