0

the following code is supposed to be the view of an edit profile page , when the form is submitted the app checks and finds if the username or email already exists , if so it will raise an Integrity exception. but the program doesn't work as expected. when the form is submitted it always flashes "username/email already registered" even when there is no such email or username in the database , why so ?

@App.route("/edit_profile", methods=('GET', 'POST'))
def edit_profile():
    global current_user
    user_email = current_user
    user = User.get(email=user_email.email)
    username = user.username
    email = user.email
    form = LoginForm(username=user.username, email=user.email)
    if request.method == "POST" :
        if form.username.data != user.username :
            try:
                User.update(username=form.username.data).where(email==user.email).execute()
                current_user = user
                current_user.username = user.username
                current_user.save()
            except peewee.IntegrityError:
                flash("Username already registered")
        if form.email.data != user.email:
            try :
                User.update(email=form.email.data).where(username==user.username).execute()
                current_user = user
                current_user.email = user.email
                current_user.save()
            except peewee.IntegrityError :
                flash("Email already registered")
        if form.password.data != '':
            User.update(password=security.generate_password_hash(form.password.data, method='pbkdf2:sha1', salt_length=8)).where(username==username).execute()
    return render_template("edit_profile.html", form=form)
A. S.
  • 143
  • 2
  • 12

0 Answers0