Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, Blueprint, render_template, redirect, url_for, request, flash, jsonify
- from flask_login import login_user, logout_user, login_required, current_user
- from werkzeug.security import generate_password_hash, check_password_hash
- from .models import *
- from .tools import *
- from .association import *
- from .events import *
- from .item import *
- from . import db
- app = Flask(__name__)
- auth = Blueprint('auth', __name__)
- @auth.route('/logout')
- @login_required
- def logout():
- logout_user()
- return redirect(url_for('main.index'))
- @auth.route('/login')
- def login():
- return render_template('login.html')
- @auth.route('/login', methods=['POST'])
- def login_post():
- email = request.form.get('email')
- password = request.form.get('password')
- remember = True if request.form.get('remember') else False
- user = User.query.filter_by(email=email).first()
- # check if user actually exists
- # take the user supplied password, hash it, and compare it to the hashed password in database
- if not user or not check_password_hash(user.password, password):
- flash('Please check your login details and try again.')
- return redirect(url_for('auth.login')) # if user doesn't exist or password is wrong, reload the page
- # if the above check passes, then we know the user has the right credentials
- login_user(user, remember=remember)
- return redirect(url_for('main.profile'))
- @auth.route('/signup')
- def signup():
- return render_template('signup.html')
- @auth.route('/signup', methods=['POST'])
- def signup_post():
- email = request.form.get('email')
- name = request.form.get('name')
- password = request.form.get('password')
- # if this returns a user, then the email already exists in database
- user = User.query.filter_by(email=email).first()
- if user: # if a user is found, we want to redirect back to signup page so user can try again
- flash('Email address already exists')
- return redirect(url_for('auth.signup'))
- # create new user with the form data. Hash the password so plaintext version isn't saved.
- new_user = User(email=email, name=name,
- password=generate_password_hash(password, method='sha256'),
- swapys=0)
- # add the new user to the database
- db.session.add(new_user)
- db.session.commit()
- return redirect(url_for('auth.login'))
- @auth.route('/user/<name>/<id>')
- @login_required
- def user_page(name, id):
- user = User.query.filter_by(id=id).first()
- name = user.name
- email = user.email
- id = user.id
- swapys = user.swapys
- list = [id, name, email, swapys]
- return render_template('detail_user.html', user_info=list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement