Spaces:
Sleeping
Sleeping
File size: 6,519 Bytes
7644eac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField, SelectField, HiddenField
from wtforms.validators import DataRequired, Email, EqualTo, Length, ValidationError, Regexp
from web_app.models import User # Assuming models.py is in web_app
import random
import string
from datetime import datetime
class LoginForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired()])
remember_me = BooleanField('Remember Me')
login_method = HiddenField(default='password')
login_time = HiddenField(default=lambda: datetime.utcnow().strftime('%H:%M'))
submit = SubmitField('Sign In')
def get_greeting(self):
"""Returns a time-appropriate greeting"""
hour = datetime.utcnow().hour
if hour < 12:
return "Good morning!"
elif hour < 18:
return "Good afternoon!"
else:
return "Good evening!"
def get_motivation(self):
"""Returns a random motivational message"""
messages = [
"Ready to continue your learning journey?",
"Your AI learning path awaits!",
"Welcome back, knowledge seeker!",
"Let's build your skills today!",
"Time to level up your expertise!"
]
return random.choice(messages)
class RegistrationForm(FlaskForm):
username = StringField('Username',
validators=[DataRequired(),
Length(min=3, max=64),
Regexp('^[A-Za-z0-9_.-]+$',
message='Username can only contain letters, numbers, dots, underscores and dashes')])
email = StringField('Email', validators=[DataRequired(), Email(), Length(max=120)])
password = PasswordField('Password', validators=[DataRequired(), Length(min=8)])
password2 = PasswordField('Repeat Password',
validators=[DataRequired(),
EqualTo('password', message='Passwords must match.')])
password_strength = HiddenField(default='0')
suggested_username = HiddenField()
submit = SubmitField('Join the Learning Community')
def __init__(self, *args, **kwargs):
super(RegistrationForm, self).__init__(*args, **kwargs)
# Generate username suggestions based on email if provided
if 'email' in kwargs.get('data', {}) and kwargs['data']['email']:
email = kwargs['data']['email']
username_base = email.split('@')[0]
self.suggested_username.data = self._generate_username_suggestions(username_base)
def _generate_username_suggestions(self, base):
"""Generate creative username suggestions"""
suggestions = []
# Basic username from email
suggestions.append(base)
# Add a learning-related suffix
learning_suffixes = ['learner', 'student', 'scholar', 'genius', 'explorer']
suggestions.append(f"{base}_{random.choice(learning_suffixes)}")
# Add year
current_year = datetime.utcnow().year
suggestions.append(f"{base}{current_year}")
# Random suffix
random_suffix = ''.join(random.choices(string.digits, k=3))
suggestions.append(f"{base}{random_suffix}")
return suggestions
def get_password_feedback(self, password):
"""Returns helpful feedback about password strength"""
strength = 0
feedback = []
if len(password) >= 12:
strength += 2
feedback.append("Good length!")
elif len(password) >= 8:
strength += 1
if any(c.isupper() for c in password):
strength += 1
feedback.append("Has uppercase")
if any(c.islower() for c in password):
strength += 1
if any(c.isdigit() for c in password):
strength += 1
feedback.append("Has numbers")
if any(c in string.punctuation for c in password):
strength += 1
feedback.append("Has special characters")
strength_labels = {
0: "Very weak",
1: "Weak",
2: "Fair",
3: "Good",
4: "Strong",
5: "Very strong",
6: "Excellent!"
}
return {
"score": strength,
"label": strength_labels.get(strength, "Unknown"),
"feedback": feedback
}
def validate_username(self, username):
# Check if username exists
user = User.query.filter_by(username=username.data).first()
if user is not None:
# Generate alternative suggestions
base = username.data
suggestions = []
# Add random numbers
suggestions.append(f"{base}{random.randint(1, 999)}")
# Add learning-related prefix
prefixes = ['awesome', 'brilliant', 'clever', 'eager']
suggestions.append(f"{random.choice(prefixes)}_{base}")
# Add random suffix
suffixes = ['learner', 'mind', 'thinker', 'pro']
suggestions.append(f"{base}_{random.choice(suffixes)}")
# Format suggestions as a string
suggestion_text = ", ".join(suggestions)
raise ValidationError(f'This username is already taken. How about: {suggestion_text}?')
def validate_email(self, email):
user = User.query.filter_by(email=email.data).first()
if user is not None:
raise ValidationError('This email is already registered. Did you mean to log in instead?')
# Email domain validation with friendly messages
domain = email.data.split('@')[-1].lower()
disposable_domains = ['mailinator.com', 'tempmail.com', 'fakeinbox.com', 'guerrillamail.com']
if domain in disposable_domains:
raise ValidationError('Please use your regular email instead of a temporary one. We promise not to spam you!')
# Generate username suggestions based on email
username_base = email.data.split('@')[0]
self.suggested_username.data = self._generate_username_suggestions(username_base)
|