first
This commit is contained in:
50
forms.py
Normal file
50
forms.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, TextAreaField, BooleanField, SubmitField, PasswordField
|
||||
from wtforms.validators import DataRequired, Email, Length, Optional, ValidationError
|
||||
from models import User
|
||||
from flask_login import current_user
|
||||
from flask_wtf.file import FileField, FileAllowed
|
||||
|
||||
class UserForm(FlaskForm):
|
||||
first_name = StringField('First Name', validators=[DataRequired(), Length(min=2, max=100)])
|
||||
last_name = StringField('Last Name', validators=[DataRequired(), Length(min=2, max=100)])
|
||||
email = StringField('Email', validators=[DataRequired(), Email(), Length(max=150)])
|
||||
phone = StringField('Phone (Optional)', validators=[Optional(), Length(max=20)])
|
||||
company = StringField('Company (Optional)', validators=[Optional(), Length(max=100)])
|
||||
position = StringField('Position (Optional)', validators=[Optional(), Length(max=100)])
|
||||
notes = TextAreaField('Notes (Optional)', validators=[Optional()])
|
||||
is_active = BooleanField('Active', default=True)
|
||||
is_admin = BooleanField('Admin Role', default=False)
|
||||
new_password = PasswordField('New Password (Optional)')
|
||||
confirm_password = PasswordField('Confirm Password (Optional)')
|
||||
profile_picture = FileField('Profile Picture (Optional)', validators=[FileAllowed(['jpg', 'jpeg', 'png', 'gif'], 'Images only!')])
|
||||
submit = SubmitField('Save Contact')
|
||||
|
||||
def validate_is_admin(self, field):
|
||||
# Only validate when trying to uncheck admin status
|
||||
if not field.data:
|
||||
# Get the user being edited (either current user or a new user)
|
||||
user = User.query.filter_by(email=self.email.data).first()
|
||||
# If this is an existing admin user and they're the only admin
|
||||
if user and user.is_admin:
|
||||
total_admins = User.query.filter_by(is_admin=True).count()
|
||||
if total_admins <= 1:
|
||||
raise ValidationError('There must be at least one admin user in the system.')
|
||||
|
||||
def validate(self, extra_validators=None):
|
||||
rv = super().validate(extra_validators=extra_validators)
|
||||
if not rv:
|
||||
return False
|
||||
if self.new_password.data or self.confirm_password.data:
|
||||
if not self.new_password.data or not self.confirm_password.data:
|
||||
self.confirm_password.errors.append('Both password fields are required.')
|
||||
return False
|
||||
if self.new_password.data != self.confirm_password.data:
|
||||
self.confirm_password.errors.append('Passwords must match.')
|
||||
return False
|
||||
return True
|
||||
|
||||
class RoomForm(FlaskForm):
|
||||
name = StringField('Room Name', validators=[DataRequired(), Length(min=3, max=100)])
|
||||
description = TextAreaField('Description', validators=[Optional(), Length(max=500)])
|
||||
submit = SubmitField('Create Room')
|
||||
Reference in New Issue
Block a user