]>
git.datanom.net - wpp.git/blob - app/views.py
1 from flask
import render_template
, flash
, redirect
, session
, url_for
, request
, g
2 from flask_login
import login_user
, logout_user
, current_user
, login_required
3 from app
import app
, db
, lm
4 from .forms
import LoginForm
, RegisterForm
5 from .models
import User
6 from werkzeug
.security
import generate_password_hash
, check_password_hash
7 from sqlalchemy
import exc
9 class DBException(Exception):
18 return User
.query
.get(int(id))
24 return render_template('index.html',
28 @app.route('/login', methods
=['GET', 'POST'])
30 if g
.user
is not None and g
.user
.is_authenticated
:
31 return redirect(url_for('index'))
33 if form
.validate_on_submit():
34 user
= User
.query
.filter_by(username
=form
.username
.data
).first()
36 flash('Username or password is wrong. Please try again.')
38 if check_password_hash(user
.password
, form
.password
.data
):
39 app
.logger
.info("Login: %s" % user
)
40 remember_me
= form
.remember_me
.data
41 if 'remember_me' in session
:
42 session
.pop('remember_me', None)
43 login_user(user
, remember
= remember_me
)
44 return redirect(request
.args
.get('next') or url_for('index'))
46 flash('Username or password is wrong. Please try again')
47 return render_template('login.html',
54 return redirect(url_for('index'))
56 @app.route('/user/<username>')
59 user
= User
.query
.filter_by(username
=username
).first()
61 flash('User %s not found.' % username
)
62 return redirect(url_for('index'))
63 app
.logger
.info("Show profile: %s" % user
)
64 return render_template('user.html',
68 @app.route('/register', methods
=['GET', 'POST'])
71 if form
.validate_on_submit():
72 if form
.password
.data
== form
.passwordchk
.data
:
74 username
=form
.username
.data
76 u
= User
.query
.filter_by(username
=username
).first()
78 raise DBException("%s: Username exist" % username
)
79 e
= User
.query
.filter_by(email
=email
).first()
81 raise DBException("%s: Email exist" % email
)
82 hashed_password
= generate_password_hash(form
.password
.data
, method
='sha256')
83 new_user
= User(name
=form
.name
.data
, username
=form
.username
.data
, email
=form
.email
.data
, password
=hashed_password
)
84 except DBException
as ex
:
87 except exc
.IntegrityError
as ex
:
89 flash('Create user failed: %s' % ex
)
92 flash('Unknown error')
94 db
.session
.add(new_user
)
96 app
.logger
.warning("Registered: %s" % new_user
)
97 flash('You have been registered with username "%s"' % form
.username
.data
)
98 return redirect(url_for('login'))
100 flash('Password did not match password check')
101 return render_template('register.html',
105 @app.route('/resetpwd')
107 return '<h1>resetpwd</h1>'
109 @app.route('/search')
111 return '<h1>search</h1>'
117 if g
.user
is not None and g
.user
.is_admin
:
118 app
.logger
.warning("Enter Admin area: %s" % g
.user
)
119 return '<h1>Admin</h1>'
120 except AttributeError:
122 app
.logger
.critical("Tried to enter Admin area: %s" % g
.user
)
123 return redirect(url_for('index'))
This page took 0.106994 seconds and 6 git commands to generate.