2023-05-26 09:40:23 +00:00
|
|
|
import base64
|
|
|
|
|
2023-04-20 13:10:16 +00:00
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import (
|
|
|
|
StringField,
|
2023-05-04 11:18:49 +00:00
|
|
|
FileField,
|
2023-04-20 13:10:16 +00:00
|
|
|
PasswordField,
|
|
|
|
SubmitField,
|
|
|
|
ValidationError,
|
|
|
|
BooleanField,
|
|
|
|
)
|
2023-04-21 12:58:47 +00:00
|
|
|
from wtforms.validators import DataRequired, Length, EqualTo
|
2023-05-04 11:18:49 +00:00
|
|
|
from flask_login import current_user
|
2023-04-20 13:10:16 +00:00
|
|
|
|
|
|
|
from app import models as m
|
|
|
|
|
|
|
|
|
|
|
|
class UserForm(FlaskForm):
|
|
|
|
next_url = StringField("next_url")
|
|
|
|
user_id = StringField("user_id", [DataRequired()])
|
|
|
|
activated = BooleanField("activated")
|
|
|
|
username = StringField("Username", [DataRequired()])
|
|
|
|
password = PasswordField("Password", validators=[DataRequired(), Length(6, 30)])
|
|
|
|
password_confirmation = PasswordField(
|
|
|
|
"Confirm Password",
|
|
|
|
validators=[
|
|
|
|
DataRequired(),
|
|
|
|
EqualTo("password", message="Password do not match."),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
submit = SubmitField("Save")
|
|
|
|
|
|
|
|
def validate_username(self, field):
|
|
|
|
if (
|
|
|
|
m.User.query.filter_by(username=field.data)
|
|
|
|
.filter(m.User.id != int(self.user_id.data))
|
|
|
|
.first()
|
|
|
|
is not None
|
|
|
|
):
|
|
|
|
raise ValidationError("This username is taken.")
|
2023-06-07 13:10:43 +00:00
|
|
|
elif " " in field.data:
|
|
|
|
raise ValidationError("User name couldn't have spaces.")
|
2023-04-20 13:10:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NewUserForm(FlaskForm):
|
|
|
|
activated = BooleanField("activated")
|
|
|
|
username = StringField("Username", [DataRequired()])
|
|
|
|
password = PasswordField("Password", validators=[DataRequired(), Length(6, 30)])
|
|
|
|
password_confirmation = PasswordField(
|
|
|
|
"Confirm Password",
|
|
|
|
validators=[
|
|
|
|
DataRequired(),
|
|
|
|
EqualTo("password", message="Password do not match."),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
submit = SubmitField("Save")
|
|
|
|
|
|
|
|
def validate_username(self, field):
|
|
|
|
if m.User.query.filter_by(username=field.data).first() is not None:
|
|
|
|
raise ValidationError("This username is taken.")
|
2023-05-04 11:18:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EditUserForm(FlaskForm):
|
2023-06-07 13:10:43 +00:00
|
|
|
username = StringField("Username", [DataRequired()])
|
2023-05-26 12:37:43 +00:00
|
|
|
avatar_img = FileField("Avatar file (max 1mb, formats: jpg,jpeg,png)")
|
2023-05-04 11:18:49 +00:00
|
|
|
submit = SubmitField("Save")
|
|
|
|
|
|
|
|
def validate_username(self, field):
|
|
|
|
if (
|
|
|
|
m.User.query.filter_by(username=field.data)
|
|
|
|
.filter(m.User.id != current_user.id)
|
|
|
|
.first()
|
|
|
|
):
|
|
|
|
raise ValidationError("This username is taken.")
|
2023-06-07 13:10:43 +00:00
|
|
|
elif " " in field.data:
|
|
|
|
raise ValidationError("User name couldn't have spaces.")
|
2023-05-15 09:14:49 +00:00
|
|
|
|
2023-05-26 09:40:23 +00:00
|
|
|
def validate_avatar_img(self, field):
|
|
|
|
if field.data:
|
|
|
|
img_data = field.data.read()
|
|
|
|
img_data = base64.b64encode(img_data)
|
|
|
|
img_data = img_data.decode("utf-8")
|
|
|
|
field.data = img_data
|
|
|
|
size = len(img_data) / 1000000
|
|
|
|
if size > 1:
|
|
|
|
raise ValidationError("Avatar file size too large")
|
|
|
|
|
2023-05-15 09:14:49 +00:00
|
|
|
|
|
|
|
class ReactivateUserForm(FlaskForm):
|
|
|
|
submit = SubmitField("Save")
|