-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews_user.py
62 lines (53 loc) · 1.97 KB
/
views_user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from flask import render_template, request, redirect, session, flash, url_for
from models.users import Users
from objects.userForm import UserForm, UserFormCreate
from app import app, db
from flask_bcrypt import check_password_hash, generate_password_hash
@app.route('/login')
def login():
next_page = request.args.get('next_page')
form = UserForm()
if next_page:
return render_template('login.html', next_page=next_page, form=form)
else:
return render_template('login.html', next_page=url_for('index'), form=form)
@app.route('/auth', methods=['POST', ])
def auth():
form = UserForm(request.form)
user = Users.query.filter_by(nickname=form.nickname.data).first()
password = check_password_hash(user.password, form.password.data)
if user and password:
session['user_logged'] = user.nickname
flash(user.nickname + ' Logged with sucess!!')
next_page = request.form['next_page']
return redirect(next_page)
else:
flash('Error, Fill again!!')
return redirect(url_for('login'))
@app.route('/logout')
def logout():
session['user_logged'] = None
flash('Logout with Sucess!')
return redirect(url_for('index'))
@app.route('/newUser')
def newUser():
form = UserFormCreate()
return render_template('new_user.html', title='New User', form=form)
@app.route('/userCreate', methods=['POST', ])
def userCreate():
form = UserFormCreate(request.form)
if not form.validate_on_submit():
return redirect(url_for('newUser'))
name = form.name.data
nickname = form.nickname.data
password = form.password.data
password_crypt = generate_password_hash(password).decode('utf-8')
user = Users.query.filter_by(nickname=nickname).first()
if user:
flash('user already in database!')
return redirect(url_for('newUser'))
new_user = Users(name=name, nickname=nickname, password=password_crypt)
db.session.add(new_user)
db.session.commit()
flash('User added with sucess!')
return redirect(url_for('index'))