def validate(cls, data): errors = [] user_test = cls.query.filter_by(email=data['email']).first() if user_test: errors.append('Email already in use') if len(data['fname']) < 1: errors.append('First Name left blank') elif not data['fname'].isalpha(): errors.append('First Name can only contain letters') if len(data['lname']) < 1: errors.append('Last Name left blank') elif not data['lname'].isalpha(): errors.append('Last Name can only contain letters') if len(data['email']) < 1: errors.append('Email left blank') elif not EMAIL_REGEX.match(data['email']): errors.append('Email must be valid') if not PASS_REGEX.match(data['password']): errors.append( 'Password must be at least 5 characters, and contain at least one letter and number' ) if not data['password'] == data['pass_conf']: errors.append('Passwords do not match') if errors: for each_error in errors: flash(each_error, 'register') return True else: return False
def validate_new(cls, form): errors = [] if len(form['first_name']) == 0 or len(form['last_name']) == 0: errors.append( ("Please enter your first and last names.", 'registration')) if not (form['first_name'].isalpha() and form['last_name'].isalpha()): errors.append( ("Names must be alphabet characters only!", 'registration')) existing_users = User.query.filter_by( first_name=form['first_name'], last_name=form['last_name']).count() if (existing_users) > 0: errors.append( ("This user's first and last name is already registered!", 'registration')) if len(form['password']) < 5: errors.append(("Password must be at least 5 characters long!", 'registration')) if form['password'] != form['confirm_password']: errors.append(("Passwords don't match!", 'registration')) if not EMAIL_REGEX.match(form['email_address'] ): # test whether a field matches the pattern errors.append(("Invalid email address!", 'registration')) existing_users = User.query.filter_by( email=form['email_address']).count() if (existing_users) > 0: errors.append( ("This email address is currently in use by another user!", 'registration')) return errors
def addUser(user_name, email, password, confirm): user_added = False if len(user_name) == 0: flash("Please enter user name.") elif not EMAIL_REGEX.match( email): # test whether a field matches the pattern flash("Invalid email address.") elif not re.search(PWD_REGEX, password): flash( "Password must be 6-20 characters and contain one or more of each of: a number, uppercase letter, lower case letter, and special symbol." ) elif password != confirm: flash("Password confirmation does not match.") else: # check if email address already exists result = User.query.filter_by(email=email).first() if result: flash("Account already exists.") else: # add new member new_user = User(user_name=user_name, email=email, password=bcrypt.generate_password_hash(password), balance=starting_balance, current_game_id=None) db.session.add(new_user) db.session.commit() flash("New user added.") user_added = True return user_added
def editprofile(): id = request.form["user_id"] if int(id) != session["user_id"]["id"]: return redirect("/logout") user = User.query.get(id) if request.form["email"] != session["user_id"]["email"]: if not EMAIL_REGEX.match(request.form["email"]): flash("Invalid Email Address", "update_profile") else: user.email = request.form["email"] print("email changed") session["user_id"] = { "first": user.f_name, "last": user.l_name, "email": request.form["email"], "id": user.user_id } flash("Email Updated", "update_profile") if request.form["profile"] and request.form["profile"] != user.profile: if len(request.form["profile"]) > 1600: flash("Profile must be less than 1600 characters", "update_profile") else: user.profile = request.form["profile"] print("profile changed") flash("Profile Updated", "update_profile") db.session.commit() return redirect(f"/userprofile/{id}")
def login_user(): is_valid = True if not request.form['em']: is_valid = False flash("Please enter an email.") if not EMAIL_REGEX.match(request.form['em']): is_valid = False flash("Please enter a valid email.") if not is_valid: return redirect("/") else: user_list = User.query.filter_by(email=request.form['em']).all() if not user_list: flash("Email is not valid") return redirect("/") else: user = user_list[0] if not request.form['pw']: is_valid = False flash("Please enter a password") if not bcrypt.check_password_hash(user.password, request.form['pw']): is_valid = False flash("Password is not valid") if is_valid: session['user_id'] = user.id return redirect("/tweets_landing") else: return redirect("/")
def validate(sub_info): try: is_valid = True if len(sub_info['fn']) < 1: is_valid = False flash("First name cannot be blank.") if len(sub_info['ln']) < 1: is_valid = False flash("Last name cannot be blank.") if len(sub_info['pwasdasd']) < 8: is_valid = False flash("Password must be at least 7 characters") if sub_info['pw'] != sub_info['c_pw']: is_valid = False flash("Passwords must match") if not EMAIL_REGEX.match(sub_info['em']): is_valid = False flash("Please use a valid email.") return is_valid except HTTPException as exc: print(f"There has been an except registering a user: {exc}")
def find_registration_errors(cls, form_data): errors = [] if len(form_data['first_name']) < 1: errors.append("first name must be at least 1 character long") if len(form_data['last_name']) < 1: errors.append("last name must be at least 1 character long") if not EMAIL_REGEX.match(form_data['email']): errors.append("invalid email") if form_data['password'] != request.form['confirm']: errors.append("passwords dont match") if len(form_data['password']) < 8: errors.append("password isn't long enough") return errors
def reg_email_check(): email_format = True if not EMAIL_REGEX.match(request.form["email"]): email_format = False return render_template("partials/reg_error.html", email_format=email_format) email_exists = False user = User.query.filter_by(email=request.form["email"]).all() if user: email_exists = True return render_template("partials/reg_error.html", email_exists=email_exists) return render_template("partials/reg_error.html", email_format=email_format, email_exists=email_exists)
def new_acc(): is_valid = True name_err = 0 if not request.form['fname'].isalpha() or not len( request.form['fname']) >= 2: is_valid = False name_err = 1 flash("Names can contain only letters and be at least two characters", "registration") if not request.form['lname'].isalpha() or not len( request.form['lname']) >= 2: is_valid = False if name_err == 1: flash( "Names can contain only letters and be at least two characters", "registration") if not password_reg.match(request.form["pass"]): is_valid = False flash( "Password should be at least 5 characters, have one number, one uppercase and one lowercase letter, and one symbol", "registration") if not EMAIL_REGEX.match(request.form["email"]): is_valid = False flash("Invalid Email Address", "registration") if request.form["pass"] != request.form["confirmpass"]: is_valid = False flash("Passwords do not match", "registration") if is_valid: pw_hash = bcrypt.generate_password_hash(request.form["pass"]) new_user = User(f_name=request.form["fname"], l_name=request.form["lname"], email=request.form["email"], admin_status=0, password=pw_hash) db.session.add(new_user) db.session.commit() session["user_id"] = { "first": new_user.f_name, "last": new_user.l_name, "email": new_user.email, "id": new_user.user_id } print(session["user_id"]) print("Account creation successful!") return redirect("/userpage") return redirect("/")
def validate_user(cls, user_data): is_valid = True if len(user_data['first_name']) < 1: is_valid = False flash("Please provide a first name!") if len(user_data['last_name']) < 1: is_valid = False flash("Please provide a last name!") if not EMAIL_REGEX.match(user_data['email']): is_valid = False flash("Please provide a valid email address!") if len(user_data['password']) < 5: is_valid = False flash("Password must be at least 5 characters!") if user_data['password'] != user_data['cpassword']: is_valid = False flash("Passwords do not match!") return is_valid
def validate_user(cls, user_data): is_valid = True if len(user_data['first_name']) < 1: is_valid = False flash("Enter a First Name") if len(user_data['last_name']) < 1: is_valid = False flash("Enter a Last Name") if len(user_data['username']) < 1: is_valid = False flash("Enter a Username") if not EMAIL_REGEX.match(user_data['email']): is_valid = False flash("Not a Valid Email Address") if len(user_data['password']) < 5: is_valid = False flash("Password Must Be at Least Five Characters") if user_data['password'] != user_data['cpassword']: is_valid = False flash("Password's Do Not Match") return is_valid
def register(): switch = request.form["switch"] print(switch) is_valid = True if switch == "org": email = Organization.query.filter_by(email=request.form["email"]).all() email2 = Developer.query.filter_by(email=request.form["email"]).all() if len(request.form["orgname"]) == 0: is_valid = False flash("Organization name cannot be blank", "reg") elif switch == "dev": email = Developer.query.filter_by(email=request.form["email"]).all() email2 = Organization.query.filter_by( email=request.form["email"]).all() if email: is_valid = False flash("Email in use") if email2: is_valid = False if switch == "dev": flash("Email registered as an Organization") if switch == "org": flash("Email registered as a Developer") if not request.form["fname"].isalpha() or not len( request.form["fname"]) >= 2: is_valid = False flash( "First name can only contain letters and must be at least 2 characters long", "reg") if not request.form['lname'].isalpha() or not len( request.form['lname']) >= 2: is_valid = False flash( "Last name can only contain letters and must be at least 2 characters long", "reg") if not EMAIL_REGEX.match(request.form["email"]): is_valid = False flash("Invalid Email Address") if not len(request.form['address']) > 5: is_valid = False flash("Address must be longer than 5 characters", "reg") if not len(request.form['city']) >= 2: is_valid = False flash("City name must be at least 3 characters long", "reg") if not password_reg.match(request.form["password"]): is_valid = False flash( "Password should be at least 5 characters, have one number, one uppercase and one lowercase letter, and one symbol" ) if request.form["password"] != request.form["confirmpass"]: is_valid = False flash("Passwords do not match", "reg") if is_valid: pw_hash = bcrypt.generate_password_hash(request.form["password"]) if switch == "dev": user = Developer(fname=request.form["fname"], lname=request.form["lname"], email=request.form["email"], address=request.form["address"], city=request.form["city"], state=request.form["state"], password=pw_hash) elif switch == "org": user = Organization(orgname=request.form["orgname"], fname=request.form["fname"], lname=request.form["lname"], email=request.form["email"], address=request.form["address"], city=request.form["city"], state=request.form["state"], password=pw_hash) db.session.add(user) db.session.commit() session["user_id"] = { "fname": user.fname, "lname": user.lname, "email": user.email, "id": user.id, "role": switch } return redirect("/{}_landing".format(switch)) return redirect("/#{}_reg".format(switch))