Advertisement
Guest User

Untitled

a guest
Aug 5th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. def check_if_username_or_email_is_in_db(form, field):
  2.     '''
  3.    if the username and email is in the db the code works,
  4.    if not it raises an ValidationError.
  5.    The if statement checks if the query is empty/has no values in db.
  6.    This runs in the LoginForm in auth/forms.py
  7.    '''
  8.     username_or_email_form = field.data
  9.     flash(f'username_or_email_form={username_or_email_form}')
  10.     username_db = db.session.execute(db.select(User).filter_by(username=username_or_email_form)).scalar_one_or_none()
  11.     email_db = db.session.execute(db.select(User).filter_by(email=username_or_email_form)).scalar_one_or_none()
  12.     flash(f'username_db={username_db}')
  13.     flash(f'email_db={email_db}')
  14.     # if empty list [] return True
  15.     # I want the username and email to to both be negative because I am using an username or an email to login
  16.     if not db.session.execute(db.select(User).filter_by(username=username_or_email_form)).scalar_one_or_none() and \
  17.         not db.session.execute(db.select(User).filter_by(email=username_or_email_form)).scalar_one_or_none():
  18.         raise ValidationError('The username or email or password do not exist. Please retype your username or email or password.')  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement