Advertisement
Guest User

Untitled

a guest
Mar 16th, 2020
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. from flask import Flask, Blueprint, render_template, redirect, url_for, request, flash, jsonify
  2. from flask_login import login_user, logout_user, login_required, current_user
  3. from werkzeug.security import generate_password_hash, check_password_hash
  4. from .models import *
  5. from .tools import *
  6. from .association import *
  7. from .events import *
  8. from .item import *
  9.  
  10. from . import db
  11.  
  12. app = Flask(__name__)
  13.  
  14. auth = Blueprint('auth', __name__)
  15.  
  16.  
  17. @auth.route('/logout')
  18. @login_required
  19. def logout():
  20. logout_user()
  21. return redirect(url_for('main.index'))
  22.  
  23. @auth.route('/login')
  24. def login():
  25. return render_template('login.html')
  26.  
  27. @auth.route('/login', methods=['POST'])
  28. def login_post():
  29. email = request.form.get('email')
  30. password = request.form.get('password')
  31. remember = True if request.form.get('remember') else False
  32.  
  33. user = User.query.filter_by(email=email).first()
  34.  
  35. # check if user actually exists
  36. # take the user supplied password, hash it, and compare it to the hashed password in database
  37. if not user or not check_password_hash(user.password, password):
  38. flash('Please check your login details and try again.')
  39. return redirect(url_for('auth.login')) # if user doesn't exist or password is wrong, reload the page
  40.  
  41. # if the above check passes, then we know the user has the right credentials
  42. login_user(user, remember=remember)
  43. return redirect(url_for('main.profile'))
  44.  
  45.  
  46. @auth.route('/signup')
  47. def signup():
  48. return render_template('signup.html')
  49.  
  50.  
  51. @auth.route('/signup', methods=['POST'])
  52. def signup_post():
  53. email = request.form.get('email')
  54. name = request.form.get('name')
  55. password = request.form.get('password')
  56. # if this returns a user, then the email already exists in database
  57. user = User.query.filter_by(email=email).first()
  58.  
  59. if user: # if a user is found, we want to redirect back to signup page so user can try again
  60. flash('Email address already exists')
  61. return redirect(url_for('auth.signup'))
  62. # create new user with the form data. Hash the password so plaintext version isn't saved.
  63. new_user = User(email=email, name=name,
  64. password=generate_password_hash(password, method='sha256'),
  65. swapys=0)
  66. # add the new user to the database
  67. db.session.add(new_user)
  68. db.session.commit()
  69.  
  70. return redirect(url_for('auth.login'))
  71.  
  72. @auth.route('/user/<name>/<id>')
  73. @login_required
  74. def user_page(name, id):
  75. user = User.query.filter_by(id=id).first()
  76. name = user.name
  77. email = user.email
  78. id = user.id
  79. swapys = user.swapys
  80. list = [id, name, email, swapys]
  81. return render_template('detail_user.html', user_info=list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement