from wtforms import Form from wtforms import StringField, PasswordField, BooleanField, SubmitField, FileField, MultipleFileField, ValidationError, RadioField, TextAreaField, HiddenField, SelectField from wtforms.validators import DataRequired, InputRequired, Email, EqualTo, AnyOf, optional from werkzeug.utils import cached_property from flask import current_app from flask_wtf.csrf import _FlaskFormCSRF class CSRFForm(Form): class Meta: csrf = True csrf_class = _FlaskFormCSRF @cached_property def csrf_secret(self): return current_app.secret_key csrf_time_limit = 3600 csrf_field_name = 'csrf_token' class LoginForm(CSRFForm): username = StringField('Username', validators=[DataRequired()]) password = PasswordField('Password', validators=[DataRequired()]) remember_me = BooleanField('Remember me') submit = SubmitField('Log In') from yadc.models import User class ResetPasswordForm(CSRFForm): email = StringField('E-mail', validators=[DataRequired(), Email()]) submit = SubmitField('Reset password') # def validate_email(form, field): # email = User.query.filter_by(email=field.data).first() # if not email: # raise ValidationError('This') class RegisterForm(CSRFForm): username = StringField('Username', validators=[DataRequired()]) email = StringField('E-mail', validators=[DataRequired(), Email()]) password = PasswordField('Password', validators=[DataRequired()]) password_again = PasswordField('Repeat password', validators=[DataRequired(), EqualTo('password')]) submit = SubmitField('Register') def validate_username(form, field): user = User.query.filter_by(username=field.data).first() if user is not None: raise ValidationError('Username already taken. Try different username.') def validate_email(form, field): email = User.query.filter_by(email=field.data).first() if email is not None: raise ValidationError('This email address is already registered. Maybe try logging in instead?') from flask import request # from magic import Magic def validate_file(form, field): file = request.files.get(field.name) if not file or file.filename == '': raise ValidationError('Please select a file') class UploadForm(CSRFForm): post_img = FileField('Image', validators=[validate_file], render_kw={'required':''}) sauce = StringField('Sauce', validators=[DataRequired()]) tags = StringField('Tags', validators=[DataRequired()]) # CUSTOM VALIDATOR (also for Post edits) rating = RadioField('Rating', choices=[('safe', 'Safe'), ('questionable', 'Questionable'), ('explicit', 'Explicit')], default='safe', validators=[DataRequired()]) submit = SubmitField('Upload') def validate_post_img(form, field): file = request.files.get(field.name) client_mimetype = file.mimetype # Not sure if safe # real_mimetype = Magic(mime=True).from_buffer(file.stream.read()) if client_mimetype not in ['image/png','image/jpeg']: raise ValidationError('Please select an image file of PNG or JPEG format') class CommentForm(CSRFForm): post_id = HiddenField(validators=[DataRequired()]) content = TextAreaField('Comment', validators=[DataRequired()]) submit = SubmitField('Send') class ChangePassForm(CSRFForm): password_current = PasswordField('Current password', validators=[DataRequired()]) password = PasswordField('Password', validators=[DataRequired()]) password_again = PasswordField('Repeat password', validators=[DataRequired(), EqualTo('password')]) submit = SubmitField('Change password') class EditUserForm(CSRFForm): user_id = HiddenField(validators=[DataRequired()]) username = StringField('Username') email = StringField('E-mail', validators=[optional(), Email()]) user_status = SelectField('User status', choices=[('active', 'Active'), ('inactive', 'Inactive'), ('banned', 'Banned')], validators=[optional()]) op_level = SelectField('Permission level', choices=[('user', 'User'), ('creator', 'Creator'), ('moderator', 'Moderator'), ('admin', 'Admin')], validators=[optional()]) edit = SubmitField('Modify') delete = SubmitField('Delete') class EditPostForm(CSRFForm): post_id = HiddenField(validators=[DataRequired()]) rating = SelectField('Rating', choices=[('safe', 'Safe'), ('questionable', 'Questionable'), ('explicit', 'Explicit')], validators=[optional()]) status = SelectField('Status', choices=[('pending', 'Pending'), ('active', 'Active'), ('deleted', 'Deleted')], validators=[optional()]) source = StringField('Source') edit = SubmitField('Modify') delete = SubmitField('Delete') class EditCommentForm(CSRFForm): comment_id = HiddenField(validators=[DataRequired()]) content = TextAreaField('Comment', validators=[DataRequired()]) submit = SubmitField('Edit')