def reg_verify(): name = request.args.get('name') value = request.args.get('value') if name == 'username': return validate_username(value) elif name == 'email': return validate_email(value) return 'Invalid Request', 400
def post(self): args = self.parser.parse_args() fullnames = args.get("fullnames", "") username = args.get("username", "") email = args.get("email", "") password = args.get("password", "") confirm_pass = args.get("confirm_pass", "") if empty(fullnames) or empty(username) or empty(email) or empty( password) or empty(confirm_pass): return {"message": "All the fields are required"}, 400 if not validate_fullname(fullnames): return { "message": "Please enter a valid full name" " it should contain first and last name which" " start with capital letters" }, 400 if not validate_username(username): return { "message": "Please specify a valid username " " a username should contain lowercase" " letters only and be between 6 to 12 letters" }, 400 if not validate_email(email): return {"message": "Please enter a valid email address"}, 400 if not validate_password(password): return { "message": "Please enter a valid password" " it must contain atleast one" " lowercase, uppercase special character and a" " number" }, 400 user = User(fullnames, username, password, email) if not check_password_hash(user.password, confirm_pass): return { "message": "The password and the verifications don't" " match" }, 400 saved = user.save() if not saved: return {"message": "The username or email is already in use"}, 400 return { "message": "You successfully signed up you can now login", "data": user.json1 }
def signup(): if request.method == 'GET': return render_template('signup.html', menu_items=menu_items, errors=[]) elif request.method == 'POST': errors = [] valid = True username = request.form['userField'] password = request.form['passField'] if len(username) > 80: errors.append('Username must not be greater than 80 characters.') valid = False if len(password) > 80: errors.append('Password must not be greater than 80 characters.') valid = False if not validate_username(username): errors.append('Username can only contain letters, numbers, or underscores.') valid = False user = User.query.filter_by(username=username).first() if user is not None: errors.append('Username is already taken') valid = False if password != request.form['repeatField']: errors.append('Passwords do not match') valid = False if not valid: return render_template('signup.html', menu_items=menu_items, errors=errors) pw_hash = bcrypt.generate_password_hash(password).decode('utf-8') new_user = User(username=username, password=pw_hash) db.session.add(new_user) db.session.commit() session['LOGGED_IN'] = True session['USERNAME'] = username return redirect('/') else: abort(404)
def validate_username(form, field): res = validate_username(field.data) if res == 'OK': return True else: raise ValidationError(res)