Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. def login():
  2.  
  3. ### this is if the user is already logged in and tries to access this page. it redirects to the homepage
  4. ### NOTICE this works fine.
  5.     if current_user.is_authenticated:
  6.         if current_user.full_name == Employee.query.filter_by(full_name=current_user.full_name).first():
  7.             return redirect(url_for('employee'))
  8.         if current_user.full_name == Administration.query.filter_by(full_name=current_user.full_name).first():
  9.             return redirect(url_for('administrator'))
  10.  
  11.     form = LoginForm()
  12.     if form.validate_on_submit():
  13.  
  14. ### variables for Employee access and Administration access
  15.         user = Employee.query.filter_by(full_name=form.username.data).first()
  16.         admin = Administration.query.filter_by(full_name=form.username.data).first()
  17.  
  18. ### this will redirect to fresh login page if incorrect user or pass
  19. ### NOTICE this works fine for both tables. correct username with incorrect pass included
  20.         if user is None or not user.check_password(form.password.data):
  21.             if admin is None or not admin.check_password(form.password.data):
  22.                 return redirect(url_for('login'))
  23.  
  24.  
  25.         user_log = Employee(full_name=form.username.data)
  26.  
  27. ### This takes the username from Employee table and checks with username in form
  28. ### This then take the password from Employee table and checks with password form
  29. ### NOTICE this will work fine for valid Employee table and password but when try to
  30. ### run a valid username/password from the Administration table it will
  31. ### throw a syntax for 'user.check_password(form.password.data)' because
  32. ### it should be using the below if statement using the 'admin.check_password(form.password.data)'
  33.         if user_log.full_name == form.username.data and user.check_password(form.password.data):
  34.             login_user(user, remember=form.remember_me.data)
  35.             next_page_user = request.args.get('next')
  36.             if not next_page_user or url_parse(next_page_user).netloc != '':
  37.                 next_page_user = url_for('employee')
  38.             return redirect(next_page_user)
  39.  
  40.         admin_log = Administration(full_name=form.username.data)
  41.  
  42.  
  43.  
  44.  
  45. ### This takes the username from Administration table and checks with username in form
  46. ### This then take the password from Administration table and checks with password form
  47. ### NOTICE this does not get to run yet because the above function is where the error is.
  48.         if admin_log.full_name == form.username.data and admin.check_password(form.password.data):
  49.             login_user(admin, remember=form.remember_me.data)
  50.             next_page_admin = request.args.get('next')
  51.             if not next_page_admin or url_parse(next_page_admin).netloc != '':
  52.                 next_page_admin = url_for('administrator')
  53.             return redirect(next_page_admin)
  54.  
  55.     return render_template('login.html', title='Sign In', form=form)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement