2012-11-17 01:09:20 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
from flask import Flask, request, session, flash, render_template, redirect, url_for
|
|
|
|
|
|
|
|
from web import app
|
2012-11-17 14:18:07 +00:00
|
|
|
from user_manager import UserManager
|
2012-11-17 01:09:20 +00:00
|
|
|
import db
|
|
|
|
|
|
|
|
@app.route('/user')
|
|
|
|
def user_index():
|
|
|
|
return render_template('users.html', users = db.User.query.all())
|
|
|
|
|
|
|
|
@app.route('/user/add', methods = [ 'GET', 'POST' ])
|
|
|
|
def add_user():
|
|
|
|
if request.method == 'GET':
|
|
|
|
return render_template('adduser.html')
|
|
|
|
|
|
|
|
error = False
|
|
|
|
(name, passwd, passwd_confirm, mail, admin) = map(request.form.get, [ 'name', 'passwd', 'passwd_confirm', 'mail', 'admin' ])
|
|
|
|
if name in (None, ''):
|
|
|
|
flash('The name is required.')
|
|
|
|
error = True
|
|
|
|
if passwd in (None, ''):
|
|
|
|
flash('Please provide a password.')
|
|
|
|
error = True
|
|
|
|
elif passwd != passwd_confirm:
|
|
|
|
flash("The passwords don't match.")
|
|
|
|
error = True
|
2012-11-17 14:18:07 +00:00
|
|
|
|
2012-11-17 01:09:20 +00:00
|
|
|
if admin is None:
|
|
|
|
admin = True if db.User.query.filter(db.User.admin == True).count() == 0 else False
|
|
|
|
else:
|
|
|
|
admin = True
|
|
|
|
|
2012-11-17 14:18:07 +00:00
|
|
|
if not error:
|
|
|
|
status = UserManager.add(name, passwd, mail, admin)
|
2012-11-17 17:00:14 +00:00
|
|
|
if status == UserManager.SUCCESS:
|
2012-11-17 14:18:07 +00:00
|
|
|
flash("User '%s' successfully added" % name)
|
|
|
|
return redirect(url_for('user_index'))
|
2012-11-17 17:00:14 +00:00
|
|
|
else:
|
|
|
|
flash(UserManager.error_str(status))
|
2012-11-17 14:18:07 +00:00
|
|
|
|
|
|
|
return render_template('adduser.html')
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/user/del/<uid>')
|
|
|
|
def del_user(uid):
|
|
|
|
status = UserManager.delete(uid)
|
2012-11-17 17:00:14 +00:00
|
|
|
if status == UserManager.SUCCESS:
|
2012-11-17 14:18:07 +00:00
|
|
|
flash('Deleted user')
|
|
|
|
else:
|
2012-11-17 17:00:14 +00:00
|
|
|
flash(UserManager.error_str(status))
|
2012-11-17 01:09:20 +00:00
|
|
|
|
|
|
|
return redirect(url_for('user_index'))
|
|
|
|
|
|
|
|
@app.route('/user/login', methods = [ 'GET', 'POST'])
|
|
|
|
def login():
|
|
|
|
return_url = request.args.get('returnUrl') or url_for('index')
|
|
|
|
if session.get('userid'):
|
|
|
|
flash('Already logged in')
|
|
|
|
return redirect(return_url)
|
|
|
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
return render_template('login.html')
|
|
|
|
|
2012-11-17 14:18:07 +00:00
|
|
|
name, password = map(request.form.get, [ 'user', 'password' ])
|
2012-11-17 01:09:20 +00:00
|
|
|
error = False
|
2012-11-17 14:18:07 +00:00
|
|
|
if name in ('', None):
|
2012-11-17 01:09:20 +00:00
|
|
|
flash('Missing user name')
|
|
|
|
error = True
|
|
|
|
if password in ('', None):
|
|
|
|
flash('Missing password')
|
|
|
|
error = True
|
2012-11-17 14:18:07 +00:00
|
|
|
|
2012-11-17 01:09:20 +00:00
|
|
|
if not error:
|
2012-11-17 14:18:07 +00:00
|
|
|
status, user = UserManager.try_auth(name, password)
|
2012-11-17 17:00:14 +00:00
|
|
|
if status == UserManager.SUCCESS:
|
2012-11-17 14:18:07 +00:00
|
|
|
session['userid'] = str(user.id)
|
|
|
|
flash('Logged in!')
|
|
|
|
return redirect(return_url)
|
2012-11-17 01:09:20 +00:00
|
|
|
else:
|
2012-11-17 17:00:14 +00:00
|
|
|
flash(UserManager.error_str(status))
|
2012-11-17 01:09:20 +00:00
|
|
|
|
|
|
|
return render_template('login.html')
|
|
|
|
|
|
|
|
@app.route('/user/logout')
|
|
|
|
def logout():
|
|
|
|
session.clear()
|
|
|
|
flash('Logged out!')
|
|
|
|
return redirect(url_for('login'))
|
|
|
|
|