def validate_username(self, field): """ FlaskWTF automatically uses this validator on the form field that has the same name as the word after validate_ in the function name. :param field: :return: """ if user.get_user_by_username(field.data): raise ValidationError("Username already in use")
def login(): """ This route contains the login page and the login form. When submitted, we verify if the user exists in the database and his credentials are correct. :return: """ form = LoginForm() if request.method == 'POST' and form.validate_on_submit(): user = user_model.get_user_by_username(form.username.data) if user is not None and user.verify_password(form.password.data): login_user(user, form.remember_me.data) # If the user wanted to access from a different page instead of the home page, we can keep track of # of this url so as to redirect the user back next_url = request.args.get('next') if next_url is None or not next_url.startswith('/'): # check that the next_url is a relative URL next_url = url_for('index') return redirect(next_url) flash("Invalid username or password", "error") return render_template('login.html', form=form)
def user_profile(username): listing = listing_model.get_listings_by_owner_name(username) user = user_model.get_user_by_username(username) most_common_tag = listing_tag_model.get_most_common_tag_of_owner(username) return render_template('user_profile.html', user=user, listing=listing, most_common_tag=most_common_tag)