def addadmin(username, password, roles): """ Add admin user """ if AdminUser.query.filter_by(username=username).first(): print 'duplicate username: %s' % username return admin_user = AdminUser(username=username) admin_user.set_password(password) admin_user.roles = roles db.session.add(admin_user) db.session.commit()
def login(self): form = LoginForm() if form.validate_on_submit(): admin_user = AdminUser.auth(form.username.data, form.password.data) if not admin_user: # flash('invalid username or password') form.username.errors.append(u'帐号或密码错误') form.password.errors.append(u'帐号或密码错误') return self.render("admin/login.html", form=form) session.permanent = True session[current_app.config['SESSION_KEY_ADMIN_USERNAME']] = admin_user.username admin_user.login_time = datetime.datetime.utcnow() db.session.commit() if request.args.get('next', None): return redirect(request.args['next']) else: return redirect(url_for('admin.index')) return self.render("admin/login.html", form=form)