def setUp(self): melange.database.drop_db() melange.database.init_db() user = User('api') user.password = '******' user.save()
def test_user(self): admin = User('admin') admin.password = '******' db_session.add(admin) db_session.commit() admin = User.query.filter(User.name=='admin').one() assert admin.authenticate('123456') assert admin.authenticate('12345') == False
def login(): if request.method == 'POST': user_name = request.form['user-name'] user_password = request.form['user-password'] user = User.find(user_name) if user and user.authenticate(user_password): session['username'] = user_name return redirect(session['url']) return render_template('login.html')
def show_user(name): user = User.find(name) if request.method == 'POST': if 'remove-user' in request.form: user.remove() return redirect(url_for('.list_users')) elif 'change-password' in request.form: user_password = request.form['user-password'] user.password = user_password user.save() return render_template('user.html', user=user)
def list_users(): if request.method == 'POST': if 'add-user' in request.form: user_name = request.form['user-name'] user_password = request.form['user-password'] user = User.find(user_name) if not user: user = User(user_name) user.password = user_password user.save() users = User.find_all() return render_template('users.html', users=users)
def check_auth(auth): user = User.find(auth.username) if not user or not user.authenticate(auth.password): return False return True
import os from optparse import OptionParser parser = OptionParser() parser.add_option('-i', '--initdb', default=None, action='store_true', dest='initdb') parser.add_option('-d', '--dropdb', default=None, action='store_true', dest='dropdb') options, args = parser.parse_args() if options.initdb: from melange.database import init_db print('Initializing database') init_db() from melange import User admin = User('admin') admin.password = '******' admin.save() elif options.dropdb: from melange.database import drop_db print('Dropping database') drop_db() else: from melange import app app.run()